From 54d4734f054aace581212ba8da11a65880e8b4b1 Mon Sep 17 00:00:00 2001 From: trevor-signal <131492920+trevor-signal@users.noreply.github.com> Date: Mon, 17 Jul 2023 21:00:02 -0400 Subject: [PATCH] Stop preemptively generating screenshots for video stories --- ts/sql/Client.ts | 24 ++++++++++++++++++++++++ ts/sql/Server.ts | 10 ++++++++-- ts/state/ducks/composer.ts | 4 +++- ts/types/Attachment.ts | 1 - ts/util/handleVideoAttachment.ts | 32 +++++++++++++++++++------------- ts/util/processAttachment.ts | 5 +++-- 6 files changed, 57 insertions(+), 19 deletions(-) diff --git a/ts/sql/Client.ts b/ts/sql/Client.ts index 318bbcff1..9c8e346c9 100644 --- a/ts/sql/Client.ts +++ b/ts/sql/Client.ts @@ -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; }); } diff --git a/ts/sql/Server.ts b/ts/sql/Server.ts index 9a4300036..8e60b877d 100644 --- a/ts/sql/Server.ts +++ b/ts/sql/Server.ts @@ -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' ); } diff --git a/ts/state/ducks/composer.ts b/ts/state/ducks/composer.ts index 436d0d29a..8057daa81 100644 --- a/ts/state/ducks/composer.ts +++ b/ts/state/ducks/composer.ts @@ -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, diff --git a/ts/types/Attachment.ts b/ts/types/Attachment.ts index 0483cb72f..53d38f132 100644 --- a/ts/types/Attachment.ts +++ b/ts/types/Attachment.ts @@ -130,7 +130,6 @@ export type BaseAttachmentDraftType = { blurHash?: string; contentType: MIME.MIMEType; screenshotContentType?: string; - screenshotSize?: number; size: number; flags?: number; }; diff --git a/ts/util/handleVideoAttachment.ts b/ts/util/handleVideoAttachment.ts index 3f7b1143a..da792759b 100644 --- a/ts/util/handleVideoAttachment.ts +++ b/ts/util/handleVideoAttachment.ts @@ -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 { 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); } diff --git a/ts/util/processAttachment.ts b/ts/util/processAttachment.ts index 78b993184..4e86fa8cd 100644 --- a/ts/util/processAttachment.ts +++ b/ts/util/processAttachment.ts @@ -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 { 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 = {