Add `Attachment.migrateDataToFileSystem`
parent
0fc2868f0e
commit
ebe2a769c9
@ -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…
Reference in New Issue