signal-desktop/ts/util/downloadAttachment.ts

42 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { AttachmentType } from '../types/Attachment';
import { downloadAttachment as doDownloadAttachment } from '../textsecure/downloadAttachment';
export class AttachmentNotFoundOnCdnError extends Error {}
export async function downloadAttachment(
2021-07-09 19:36:10 +00:00
attachmentData: AttachmentType
): Promise<AttachmentType> {
2021-07-09 19:36:10 +00:00
let migratedAttachment: AttachmentType;
const { server } = window.textsecure;
if (!server) {
throw new Error('window.textsecure.server is not available!');
}
2021-07-09 19:36:10 +00:00
const { id: legacyId } = attachmentData;
if (legacyId === undefined) {
migratedAttachment = attachmentData;
} else {
migratedAttachment = {
...attachmentData,
cdnId: String(legacyId),
};
}
let downloaded;
try {
downloaded = await doDownloadAttachment(server, migratedAttachment);
} catch (error) {
2024-02-09 02:17:52 +00:00
// Attachments on the server expire after 30 days, then start returning 404 or 403
if (error && (error.code === 404 || error.code === 403)) {
throw new AttachmentNotFoundOnCdnError(error.code);
}
throw error;
}
return downloaded;
}