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');
|
2018-03-14 22:59:58 +00:00
|
|
|
|
|
|
|
// type Context :: {
|
2018-04-04 01:06:29 +00:00
|
|
|
// writeNewAttachmentData :: ArrayBuffer -> Promise (IO Path)
|
2018-03-14 22:59:58 +00:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// migrateDataToFileSystem :: Attachment ->
|
|
|
|
// Context ->
|
|
|
|
// Promise Attachment
|
2018-04-27 21:25:04 +00:00
|
|
|
exports.migrateDataToFileSystem = async (
|
|
|
|
attachment,
|
2019-01-30 20:15:07 +00:00
|
|
|
{ writeNewAttachmentData } = {}
|
2018-04-27 21:25:04 +00:00
|
|
|
) => {
|
2018-04-04 01:06:29 +00:00
|
|
|
if (!isFunction(writeNewAttachmentData)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'writeNewAttachmentData' must be a function");
|
2018-03-14 22:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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}`
|
|
|
|
);
|
2018-03-14 22:59:58 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 01:06:29 +00:00
|
|
|
const path = await writeNewAttachmentData(data);
|
2018-03-14 22:59:58 +00:00
|
|
|
|
2020-09-09 00:46:29 +00:00
|
|
|
const attachmentWithoutData = omit({ ...attachment, path }, ['data']);
|
2018-03-14 22:59:58 +00:00
|
|
|
return attachmentWithoutData;
|
|
|
|
};
|