Improve octetstream fallback in attachment channel

This commit is contained in:
Fedor Indutny 2024-08-26 17:14:09 -04:00 committed by GitHub
parent c251867699
commit 3d5d12c80b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -37,6 +37,11 @@ import {
import type { MainSQL } from '../ts/sql/main'; import type { MainSQL } from '../ts/sql/main';
import type { MessageAttachmentsCursorType } from '../ts/sql/Interface'; import type { MessageAttachmentsCursorType } from '../ts/sql/Interface';
import * as Errors from '../ts/types/errors'; import * as Errors from '../ts/types/errors';
import {
APPLICATION_OCTET_STREAM,
MIMETypeToString,
stringToMIMEType,
} from '../ts/types/MIME';
import { sleep } from '../ts/util/sleep'; import { sleep } from '../ts/util/sleep';
import { isPathInside } from '../ts/util/isPathInside'; import { isPathInside } from '../ts/util/isPathInside';
import { missingCaseError } from '../ts/util/missingCaseError'; import { missingCaseError } from '../ts/util/missingCaseError';
@ -46,6 +51,10 @@ import { drop } from '../ts/util/drop';
import { strictAssert } from '../ts/util/assert'; import { strictAssert } from '../ts/util/assert';
import { ValidatingPassThrough } from '../ts/util/ValidatingPassThrough'; import { ValidatingPassThrough } from '../ts/util/ValidatingPassThrough';
import { toWebStream } from '../ts/util/toWebStream'; import { toWebStream } from '../ts/util/toWebStream';
import {
isImageTypeSupported,
isVideoTypeSupported,
} from '../ts/util/GoogleChrome';
import { decryptAttachmentV2ToSink } from '../ts/AttachmentCrypto'; import { decryptAttachmentV2ToSink } from '../ts/AttachmentCrypto';
let initialized = false; let initialized = false;
@ -550,11 +559,18 @@ function handleRangeRequest({
const url = new URL(request.url); const url = new URL(request.url);
// Get content-type // 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 = { const headers: HeadersInit = {
'cache-control': 'no-cache, no-store', 'cache-control': 'no-cache, no-store',
'content-type': contentType || 'application/octet-stream', 'content-type': contentType,
}; };
if (size != null) { if (size != null) {