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 { arrayBufferToBlob, blobToArrayBuffer, dataURLToBlob } = require('blob-util');
|
||||
const { autoOrientImage } = require('../auto_orient_image');
|
||||
const { migrateDataToFileSystem } = require('./attachment/migrate_data_to_file_system');
|
||||
|
||||
// // Incoming message attachment fields
|
||||
// {
|
||||
|
@ -107,3 +108,5 @@ exports.removeSchemaVersion = (attachment) => {
|
|||
delete attachmentWithoutSchemaVersion.schemaVersion;
|
||||
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;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue