signal-desktop/ts/util/writeDraftAttachment.ts

73 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-09-24 20:02:30 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { omit } from 'lodash';
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';
2024-07-11 19:44:09 +00:00
import { IMAGE_PNG } from '../types/MIME';
2021-11-30 23:32:55 +00:00
import * as Errors from '../types/errors';
2024-07-11 19:44:09 +00:00
import {
getLocalAttachmentUrl,
AttachmentDisposition,
} from './getLocalAttachmentUrl';
2021-11-30 23:32:55 +00:00
import * as logger from '../logging/log';
2021-09-24 20:02:30 +00:00
export async function writeDraftAttachment(
attachment: InMemoryAttachmentDraftType
): Promise<AttachmentDraftType> {
2021-09-24 20:02:30 +00:00
if (attachment.pending) {
throw new Error('writeDraftAttachment: Cannot write pending attachment');
}
2024-07-11 19:44:09 +00:00
const local = await window.Signal.Migrations.writeNewDraftData(
attachment.data
);
2024-07-11 19:44:09 +00:00
const localScreenshot = 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)) {
2024-07-11 19:44:09 +00:00
const objectUrl = getLocalAttachmentUrl(local, {
disposition: AttachmentDisposition.Draft,
});
2021-11-30 23:32:55 +00:00
try {
dimensions = await getImageDimensions({
2024-07-11 19:44:09 +00:00
objectUrl,
2021-11-30 23:32:55 +00:00
logger,
});
} catch (error) {
logger.error(
'writeDraftAttachment: failed to capture image dimensions',
Errors.toLogFormat(error)
);
}
}
return {
2021-09-24 20:02:30 +00:00
...omit(attachment, ['data', 'screenshotData']),
2024-07-11 19:44:09 +00:00
...local,
screenshot: localScreenshot
? {
...localScreenshot,
contentType: IMAGE_PNG,
// Unused
width: 0,
height: 0,
}
: undefined,
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
};
}