2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
|
|
|
AttachmentType,
|
|
|
|
DownloadedAttachmentType,
|
|
|
|
} from '../types/Attachment';
|
2021-07-28 21:37:09 +00:00
|
|
|
import { downloadAttachment as doDownloadAttachment } from '../textsecure/downloadAttachment';
|
2020-07-07 00:39:55 +00:00
|
|
|
|
|
|
|
export async function downloadAttachment(
|
2021-07-09 19:36:10 +00:00
|
|
|
attachmentData: AttachmentType
|
2021-07-14 23:39:52 +00:00
|
|
|
): Promise<DownloadedAttachmentType | null> {
|
2021-07-09 19:36:10 +00:00
|
|
|
let migratedAttachment: AttachmentType;
|
|
|
|
|
|
|
|
const { id: legacyId } = attachmentData;
|
|
|
|
if (legacyId === undefined) {
|
|
|
|
migratedAttachment = attachmentData;
|
|
|
|
} else {
|
|
|
|
migratedAttachment = {
|
|
|
|
...attachmentData,
|
|
|
|
cdnId: String(legacyId),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-07 00:39:55 +00:00
|
|
|
let downloaded;
|
|
|
|
try {
|
2021-07-28 21:37:09 +00:00
|
|
|
downloaded = await doDownloadAttachment(
|
|
|
|
window.textsecure.server,
|
2021-07-09 19:36:10 +00:00
|
|
|
migratedAttachment
|
2020-07-07 00:39:55 +00:00
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
// Attachments on the server expire after 30 days, then start returning 404
|
|
|
|
if (error && error.code === 404) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return downloaded;
|
|
|
|
}
|