2023-04-20 16:31:59 +00:00
|
|
|
// Copyright 2023 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import type {
|
|
|
|
AttachmentWithHydratedData,
|
|
|
|
UploadedAttachmentType,
|
|
|
|
} from '../types/Attachment';
|
|
|
|
import { MIMETypeToString } from '../types/MIME';
|
|
|
|
import { padAndEncryptAttachment, getRandomBytes } from '../Crypto';
|
|
|
|
import { strictAssert } from './assert';
|
|
|
|
|
|
|
|
export async function uploadAttachment(
|
|
|
|
attachment: AttachmentWithHydratedData
|
|
|
|
): Promise<UploadedAttachmentType> {
|
|
|
|
const keys = getRandomBytes(64);
|
|
|
|
const encrypted = padAndEncryptAttachment(attachment.data, keys);
|
|
|
|
|
|
|
|
const { server } = window.textsecure;
|
|
|
|
strictAssert(server, 'WebAPI must be initialized');
|
|
|
|
|
2023-05-04 20:58:53 +00:00
|
|
|
const cdnKey = await server.putEncryptedAttachment(encrypted.ciphertext);
|
2023-04-20 16:31:59 +00:00
|
|
|
|
|
|
|
return {
|
2023-05-04 20:58:53 +00:00
|
|
|
cdnKey,
|
|
|
|
cdnNumber: 2,
|
2023-04-20 16:31:59 +00:00
|
|
|
key: keys,
|
|
|
|
size: attachment.data.byteLength,
|
|
|
|
digest: encrypted.digest,
|
|
|
|
|
|
|
|
contentType: MIMETypeToString(attachment.contentType),
|
|
|
|
fileName: attachment.fileName,
|
|
|
|
flags: attachment.flags,
|
|
|
|
width: attachment.width,
|
|
|
|
height: attachment.height,
|
|
|
|
caption: attachment.caption,
|
|
|
|
blurHash: attachment.blurHash,
|
|
|
|
};
|
|
|
|
}
|