2021-09-24 20:02:30 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { omit } from 'lodash';
|
2021-11-15 21:54:33 +00:00
|
|
|
import type {
|
|
|
|
InMemoryAttachmentDraftType,
|
|
|
|
AttachmentDraftType,
|
|
|
|
} from '../types/Attachment';
|
2021-11-30 23:32:55 +00:00
|
|
|
import { isImageAttachment } from '../types/Attachment';
|
|
|
|
import { getImageDimensions } from '../types/VisualAttachment';
|
|
|
|
import * as Errors from '../types/errors';
|
|
|
|
import * as logger from '../logging/log';
|
2021-09-24 20:02:30 +00:00
|
|
|
|
|
|
|
export async function writeDraftAttachment(
|
2021-11-15 21:54:33 +00:00
|
|
|
attachment: InMemoryAttachmentDraftType
|
|
|
|
): Promise<AttachmentDraftType> {
|
2021-09-24 20:02:30 +00:00
|
|
|
if (attachment.pending) {
|
|
|
|
throw new Error('writeDraftAttachment: Cannot write pending attachment');
|
|
|
|
}
|
|
|
|
|
2021-11-15 21:54:33 +00:00
|
|
|
const path = await window.Signal.Migrations.writeNewDraftData(
|
|
|
|
attachment.data
|
|
|
|
);
|
|
|
|
|
|
|
|
const screenshotPath = attachment.screenshotData
|
|
|
|
? await window.Signal.Migrations.writeNewDraftData(
|
|
|
|
attachment.screenshotData
|
|
|
|
)
|
|
|
|
: undefined;
|
|
|
|
|
2021-11-30 23:32:55 +00:00
|
|
|
let dimensions: { width?: number; height?: number } = {};
|
|
|
|
if (isImageAttachment(attachment)) {
|
|
|
|
const url = window.Signal.Migrations.getAbsoluteDraftPath(path);
|
|
|
|
|
|
|
|
try {
|
|
|
|
dimensions = await getImageDimensions({
|
|
|
|
objectUrl: url,
|
|
|
|
logger,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
'writeDraftAttachment: failed to capture image dimensions',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 21:54:33 +00:00
|
|
|
return {
|
2021-09-24 20:02:30 +00:00
|
|
|
...omit(attachment, ['data', 'screenshotData']),
|
2021-11-15 21:54:33 +00:00
|
|
|
path,
|
|
|
|
screenshotPath,
|
2021-09-24 20:02:30 +00:00
|
|
|
pending: false,
|
2021-11-30 23:32:55 +00:00
|
|
|
...dimensions,
|
2021-09-24 20:02:30 +00:00
|
|
|
};
|
|
|
|
}
|