Signal-Desktop/js/modules/types/attachment/migrate_data_to_file_system.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2018-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2018-04-27 21:25:04 +00:00
const { isArrayBuffer, isFunction, isUndefined, omit } = require('lodash');
// type Context :: {
// writeNewAttachmentData :: ArrayBuffer -> Promise (IO Path)
// }
//
// migrateDataToFileSystem :: Attachment ->
// Context ->
// Promise Attachment
2018-04-27 21:25:04 +00:00
exports.migrateDataToFileSystem = async (
attachment,
{ writeNewAttachmentData } = {}
2018-04-27 21:25:04 +00:00
) => {
if (!isFunction(writeNewAttachmentData)) {
2018-04-11 19:44:52 +00:00
throw new TypeError("'writeNewAttachmentData' must be a function");
}
const { data } = attachment;
const hasData = !isUndefined(data);
const shouldSkipSchemaUpgrade = !hasData;
if (shouldSkipSchemaUpgrade) {
return attachment;
}
const isValidData = isArrayBuffer(data);
if (!isValidData) {
2018-04-27 21:25:04 +00:00
throw new TypeError(
'Expected `attachment.data` to be an array buffer;' +
` got: ${typeof attachment.data}`
);
}
const path = await writeNewAttachmentData(data);
2020-09-09 00:46:29 +00:00
const attachmentWithoutData = omit({ ...attachment, path }, ['data']);
return attachmentWithoutData;
};