2024-05-29 23:46:43 +00:00
|
|
|
// Copyright 2024 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataReader } from '../../../sql/Client';
|
2024-05-29 23:46:43 +00:00
|
|
|
import * as Bytes from '../../../Bytes';
|
|
|
|
import { getBackupKey } from '../crypto';
|
|
|
|
import type { AttachmentType } from '../../../types/Attachment';
|
|
|
|
import { deriveMediaIdFromMediaName } from '../../../Crypto';
|
|
|
|
import { strictAssert } from '../../../util/assert';
|
|
|
|
|
|
|
|
export function getMediaIdFromMediaName(mediaName: string): {
|
|
|
|
string: string;
|
|
|
|
bytes: Uint8Array;
|
|
|
|
} {
|
|
|
|
const mediaIdBytes = deriveMediaIdFromMediaName(getBackupKey(), mediaName);
|
|
|
|
return {
|
|
|
|
string: Bytes.toBase64url(mediaIdBytes),
|
|
|
|
bytes: mediaIdBytes,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getMediaIdForAttachment(attachment: AttachmentType): {
|
|
|
|
string: string;
|
|
|
|
bytes: Uint8Array;
|
|
|
|
} {
|
|
|
|
const mediaName = getMediaNameForAttachment(attachment);
|
|
|
|
return getMediaIdFromMediaName(mediaName);
|
|
|
|
}
|
|
|
|
|
2024-07-16 20:39:56 +00:00
|
|
|
export function getMediaIdForAttachmentThumbnail(attachment: AttachmentType): {
|
|
|
|
string: string;
|
|
|
|
bytes: Uint8Array;
|
|
|
|
} {
|
|
|
|
const mediaName = getMediaNameForAttachmentThumbnail(
|
|
|
|
getMediaNameForAttachment(attachment)
|
|
|
|
);
|
|
|
|
return getMediaIdFromMediaName(mediaName);
|
|
|
|
}
|
|
|
|
|
2024-05-29 23:46:43 +00:00
|
|
|
export function getMediaNameForAttachment(attachment: AttachmentType): string {
|
|
|
|
if (attachment.backupLocator) {
|
|
|
|
return attachment.backupLocator.mediaName;
|
|
|
|
}
|
|
|
|
strictAssert(attachment.digest, 'Digest must be present');
|
2024-08-01 06:13:38 +00:00
|
|
|
return Bytes.toHex(Bytes.fromBase64(attachment.digest));
|
2024-05-29 23:46:43 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 20:39:56 +00:00
|
|
|
export function getMediaNameForAttachmentThumbnail(
|
|
|
|
fullsizeMediaName: string
|
|
|
|
): string {
|
2024-08-01 06:13:38 +00:00
|
|
|
return `${fullsizeMediaName}_thumbnail`;
|
2024-07-16 20:39:56 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 23:46:43 +00:00
|
|
|
export function getBytesFromMediaIdString(mediaId: string): Uint8Array {
|
|
|
|
return Bytes.fromBase64url(mediaId);
|
|
|
|
}
|
|
|
|
|
|
|
|
export type GetBackupCdnInfoType = (
|
|
|
|
mediaId: string
|
|
|
|
) => Promise<
|
|
|
|
{ isInBackupTier: true; cdnNumber: number } | { isInBackupTier: false }
|
|
|
|
>;
|
|
|
|
|
|
|
|
export const getBackupCdnInfo: GetBackupCdnInfoType = async (
|
|
|
|
mediaId: string
|
|
|
|
) => {
|
2024-07-22 18:16:33 +00:00
|
|
|
const savedInfo = await DataReader.getBackupCdnObjectMetadata(mediaId);
|
2024-05-29 23:46:43 +00:00
|
|
|
if (!savedInfo) {
|
|
|
|
return { isInBackupTier: false };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { isInBackupTier: true, cdnNumber: savedInfo.cdnNumber };
|
|
|
|
};
|