From e0fb24944fea8dbde7e109ffd64b10e824807b8d Mon Sep 17 00:00:00 2001 From: automated-signal <37887102+automated-signal@users.noreply.github.com> Date: Mon, 26 Aug 2024 21:33:01 -0500 Subject: [PATCH] Improve octetstream fallback in attachment channel Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> --- app/attachment_channel.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/app/attachment_channel.ts b/app/attachment_channel.ts index 7979b81c7b..e362831926 100644 --- a/app/attachment_channel.ts +++ b/app/attachment_channel.ts @@ -37,6 +37,11 @@ import { import type { MainSQL } from '../ts/sql/main'; import type { MessageAttachmentsCursorType } from '../ts/sql/Interface'; import * as Errors from '../ts/types/errors'; +import { + APPLICATION_OCTET_STREAM, + MIMETypeToString, + stringToMIMEType, +} from '../ts/types/MIME'; import { sleep } from '../ts/util/sleep'; import { isPathInside } from '../ts/util/isPathInside'; import { missingCaseError } from '../ts/util/missingCaseError'; @@ -46,6 +51,10 @@ import { drop } from '../ts/util/drop'; import { strictAssert } from '../ts/util/assert'; import { ValidatingPassThrough } from '../ts/util/ValidatingPassThrough'; import { toWebStream } from '../ts/util/toWebStream'; +import { + isImageTypeSupported, + isVideoTypeSupported, +} from '../ts/util/GoogleChrome'; import { decryptAttachmentV2ToSink } from '../ts/AttachmentCrypto'; let initialized = false; @@ -550,11 +559,18 @@ function handleRangeRequest({ const url = new URL(request.url); // Get content-type - const contentType = url.searchParams.get('contentType'); + const contentTypeParam = url.searchParams.get('contentType'); + let contentType = MIMETypeToString(APPLICATION_OCTET_STREAM); + if (contentTypeParam) { + const mime = stringToMIMEType(contentTypeParam); + if (isImageTypeSupported(mime) || isVideoTypeSupported(mime)) { + contentType = MIMETypeToString(mime); + } + } const headers: HeadersInit = { 'cache-control': 'no-cache, no-store', - 'content-type': contentType || 'application/octet-stream', + 'content-type': contentType, }; if (size != null) {