Add Attachment.migrateDataToFileSystem
This commit is contained in:
parent
0fc2868f0e
commit
ebe2a769c9
3 changed files with 118 additions and 0 deletions
|
@ -3,6 +3,7 @@ const isString = require('lodash/isString');
|
||||||
const MIME = require('./mime');
|
const MIME = require('./mime');
|
||||||
const { arrayBufferToBlob, blobToArrayBuffer, dataURLToBlob } = require('blob-util');
|
const { arrayBufferToBlob, blobToArrayBuffer, dataURLToBlob } = require('blob-util');
|
||||||
const { autoOrientImage } = require('../auto_orient_image');
|
const { autoOrientImage } = require('../auto_orient_image');
|
||||||
|
const { migrateDataToFileSystem } = require('./attachment/migrate_data_to_file_system');
|
||||||
|
|
||||||
// // Incoming message attachment fields
|
// // Incoming message attachment fields
|
||||||
// {
|
// {
|
||||||
|
@ -107,3 +108,5 @@ exports.removeSchemaVersion = (attachment) => {
|
||||||
delete attachmentWithoutSchemaVersion.schemaVersion;
|
delete attachmentWithoutSchemaVersion.schemaVersion;
|
||||||
return attachmentWithoutSchemaVersion;
|
return attachmentWithoutSchemaVersion;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.migrateDataToFileSystem = migrateDataToFileSystem;
|
||||||
|
|
37
js/modules/types/attachment/migrate_data_to_file_system.js
Normal file
37
js/modules/types/attachment/migrate_data_to_file_system.js
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
const isArrayBuffer = require('lodash/isArrayBuffer');
|
||||||
|
const isFunction = require('lodash/isFunction');
|
||||||
|
const isUndefined = require('lodash/isUndefined');
|
||||||
|
|
||||||
|
|
||||||
|
// type Context :: {
|
||||||
|
// writeAttachmentData :: ArrayBuffer -> Promise (IO Path)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// migrateDataToFileSystem :: Attachment ->
|
||||||
|
// Context ->
|
||||||
|
// Promise Attachment
|
||||||
|
exports.migrateDataToFileSystem = async (attachment, { writeAttachmentData } = {}) => {
|
||||||
|
if (!isFunction(writeAttachmentData)) {
|
||||||
|
throw new TypeError('`writeAttachmentData` must be a function');
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = attachment;
|
||||||
|
const hasData = !isUndefined(data);
|
||||||
|
const shouldSkipSchemaUpgrade = !hasData;
|
||||||
|
if (shouldSkipSchemaUpgrade) {
|
||||||
|
console.log('WARNING: `attachment.data` is `undefined`');
|
||||||
|
return attachment;
|
||||||
|
}
|
||||||
|
|
||||||
|
const isValidData = isArrayBuffer(data);
|
||||||
|
if (!isValidData) {
|
||||||
|
throw new TypeError('Expected `attachment.data` to be an array buffer;' +
|
||||||
|
` got: ${typeof attachment.data}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const path = await writeAttachmentData(data);
|
||||||
|
|
||||||
|
const attachmentWithoutData = Object.assign({}, attachment, { path });
|
||||||
|
delete attachmentWithoutData.data;
|
||||||
|
return attachmentWithoutData;
|
||||||
|
};
|
|
@ -1,5 +1,6 @@
|
||||||
require('mocha-testcheck').install();
|
require('mocha-testcheck').install();
|
||||||
|
|
||||||
|
const stringToArrayBuffer = require('string-to-arraybuffer');
|
||||||
const { assert } = require('chai');
|
const { assert } = require('chai');
|
||||||
|
|
||||||
const Attachment = require('../../../js/modules/types/attachment');
|
const Attachment = require('../../../js/modules/types/attachment');
|
||||||
|
@ -101,4 +102,81 @@ describe('Attachment', () => {
|
||||||
assert.deepEqual(actual, expected);
|
assert.deepEqual(actual, expected);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('migrateDataToFileSystem', () => {
|
||||||
|
it('should write data to disk and store relative path to it', async () => {
|
||||||
|
const input = {
|
||||||
|
contentType: 'image/jpeg',
|
||||||
|
data: stringToArrayBuffer('Above us only sky'),
|
||||||
|
fileName: 'foo.jpg',
|
||||||
|
size: 1111,
|
||||||
|
};
|
||||||
|
|
||||||
|
const expected = {
|
||||||
|
contentType: 'image/jpeg',
|
||||||
|
path: 'abc/abcdefgh123456789',
|
||||||
|
fileName: 'foo.jpg',
|
||||||
|
size: 1111,
|
||||||
|
};
|
||||||
|
|
||||||
|
const expectedAttachmentData = stringToArrayBuffer('Above us only sky');
|
||||||
|
const writeAttachmentData = async (attachmentData) => {
|
||||||
|
assert.deepEqual(attachmentData, expectedAttachmentData);
|
||||||
|
return 'abc/abcdefgh123456789';
|
||||||
|
};
|
||||||
|
|
||||||
|
const actual = await Attachment.migrateDataToFileSystem(
|
||||||
|
input,
|
||||||
|
{ writeAttachmentData }
|
||||||
|
);
|
||||||
|
assert.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should skip over (invalid) attachments without data', async () => {
|
||||||
|
const input = {
|
||||||
|
contentType: 'image/jpeg',
|
||||||
|
fileName: 'foo.jpg',
|
||||||
|
size: 1111,
|
||||||
|
};
|
||||||
|
|
||||||
|
const expected = {
|
||||||
|
contentType: 'image/jpeg',
|
||||||
|
fileName: 'foo.jpg',
|
||||||
|
size: 1111,
|
||||||
|
};
|
||||||
|
|
||||||
|
const writeAttachmentData = async () =>
|
||||||
|
'abc/abcdefgh123456789';
|
||||||
|
|
||||||
|
const actual = await Attachment.migrateDataToFileSystem(
|
||||||
|
input,
|
||||||
|
{ writeAttachmentData }
|
||||||
|
);
|
||||||
|
assert.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw error if data is not valid', async () => {
|
||||||
|
const input = {
|
||||||
|
contentType: 'image/jpeg',
|
||||||
|
data: 42,
|
||||||
|
fileName: 'foo.jpg',
|
||||||
|
size: 1111,
|
||||||
|
};
|
||||||
|
|
||||||
|
const writeAttachmentData = async () =>
|
||||||
|
'abc/abcdefgh123456789';
|
||||||
|
|
||||||
|
try {
|
||||||
|
await Attachment.migrateDataToFileSystem(input, { writeAttachmentData });
|
||||||
|
} catch (error) {
|
||||||
|
assert.strictEqual(
|
||||||
|
error.message,
|
||||||
|
'Expected `attachment.data` to be an array buffer; got: number'
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.fail('Unreachable');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue