Stop preemptively generating screenshots for video stories

This commit is contained in:
trevor-signal 2023-07-17 21:00:02 -04:00 committed by Fedor Indutnyy
parent f798bc999c
commit 54d4734f05
6 changed files with 57 additions and 19 deletions

View file

@ -191,6 +191,30 @@ export function _cleanMessageData(data: MessageType): MessageType {
return omit(attachment, ['data']);
}
if (attachment.screenshotData) {
assertDev(
false,
`_cleanMessageData/${logId}: Attachment ${index} had screenshotData field; deleting`
);
return omit(attachment, ['screenshotData']);
}
if (attachment.screenshot?.data) {
assertDev(
false,
`_cleanMessageData/${logId}: Attachment ${index} had screenshot.data field; deleting`
);
return omit(attachment, ['screenshot.data']);
}
if (attachment.thumbnail?.data) {
assertDev(
false,
`_cleanMessageData/${logId}: Attachment ${index} had thumbnail.data field; deleting`
);
return omit(attachment, ['thumbnail.data']);
}
return attachment;
});
}

View file

@ -1918,8 +1918,14 @@ function saveMessageSync(
if (attachments) {
strictAssert(
attachments.every(attachment => !attachment.data),
'Attempting to save a hydrated message'
attachments.every(
attachment =>
!attachment.data &&
!attachment.screenshotData &&
!attachment.screenshot?.data &&
!attachment.thumbnail?.data
),
'Attempting to save a message with binary attachment data'
);
}

View file

@ -1079,7 +1079,9 @@ function processAttachments({
await Promise.all(
filesToProcess.map(async file => {
try {
const attachment = await processAttachment(file);
const attachment = await processAttachment(file, {
generateScreenshot: true,
});
if (!attachment) {
removeAttachment(conversationId, file.path)(
dispatch,

View file

@ -130,7 +130,6 @@ export type BaseAttachmentDraftType = {
blurHash?: string;
contentType: MIME.MIMEType;
screenshotContentType?: string;
screenshotSize?: number;
size: number;
flags?: number;
};

View file

@ -10,33 +10,39 @@ import type { InMemoryAttachmentDraftType } from '../types/Attachment';
import { fileToBytes } from './fileToBytes';
export async function handleVideoAttachment(
file: File
file: File,
options?: { generateScreenshot: boolean }
): Promise<InMemoryAttachmentDraftType> {
const objectUrl = URL.createObjectURL(file);
if (!objectUrl) {
throw new Error('Failed to create object url for video!');
}
try {
const screenshotContentType = IMAGE_PNG;
const screenshotBlob = await makeVideoScreenshot({
objectUrl,
contentType: screenshotContentType,
logger: log,
});
const screenshotData = await blobToArrayBuffer(screenshotBlob);
const data = await fileToBytes(file);
return {
const attachment: InMemoryAttachmentDraftType = {
contentType: stringToMIMEType(file.type),
data,
fileName: file.name,
path: file.name,
pending: false,
screenshotContentType,
screenshotData: new Uint8Array(screenshotData),
screenshotSize: screenshotData.byteLength,
size: data.byteLength,
};
if (options?.generateScreenshot) {
const screenshotContentType = IMAGE_PNG;
const screenshotBlob = await makeVideoScreenshot({
objectUrl,
contentType: screenshotContentType,
logger: log,
});
attachment.screenshotData = new Uint8Array(
await blobToArrayBuffer(screenshotBlob)
);
attachment.screenshotContentType = screenshotContentType;
}
return attachment;
} finally {
URL.revokeObjectURL(objectUrl);
}

View file

@ -22,7 +22,8 @@ import { showToast } from './showToast';
import { ToastFileSize } from '../components/ToastFileSize';
export async function processAttachment(
file: File
file: File,
options?: { generateScreenshot: boolean }
): Promise<InMemoryAttachmentDraftType | void> {
const fileType = stringToMIMEType(file.type);
@ -31,7 +32,7 @@ export async function processAttachment(
if (isImageTypeSupported(fileType) || isHeic(fileType, file.name)) {
attachment = await handleImageAttachment(file);
} else if (isVideoTypeSupported(fileType)) {
attachment = await handleVideoAttachment(file);
attachment = await handleVideoAttachment(file, options);
} else {
const data = await fileToBytes(file);
attachment = {