Use TUS for attachment uploads

This commit is contained in:
trevor-signal 2024-05-20 15:29:20 -04:00 committed by GitHub
parent 4253bed0bd
commit a992546cda
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 312 additions and 169 deletions

View file

@ -539,7 +539,7 @@ function makeHTTPError(
const URL_CALLS = {
accountExistence: 'v1/accounts/account',
attachmentId: 'v3/attachments/form/upload',
attachmentUploadForm: 'v3/attachments/form/upload',
attestation: 'v1/attestation',
batchIdentityCheck: 'v1/profile/identity_check/batch',
challenge: 'v1/challenge',
@ -604,7 +604,7 @@ const WEBSOCKET_CALLS = new Set<keyof typeof URL_CALLS>([
'profile',
// AttachmentControllerV3
'attachmentId',
'attachmentUploadForm',
// RemoteConfigController
'config',
@ -1200,6 +1200,7 @@ export type WebAPIType = {
timeout?: number;
};
}) => Promise<Readable>;
getAttachmentUploadForm: () => Promise<AttachmentV3ResponseType>;
getAvatar: (path: string) => Promise<Uint8Array>;
getHasSubscription: (subscriberId: Uint8Array) => Promise<boolean>;
getGroup: (options: GroupCredentialsType) => Promise<Proto.IGroupResponse>;
@ -1287,7 +1288,10 @@ export type WebAPIType = {
postBatchIdentityCheck: (
elements: VerifyServiceIdRequestType
) => Promise<VerifyServiceIdResponseType>;
putEncryptedAttachment: (encryptedBin: Uint8Array) => Promise<string>;
putEncryptedAttachment: (
encryptedBin: Uint8Array | Readable,
uploadForm: AttachmentV3ResponseType
) => Promise<void>;
putProfile: (
jsonData: ProfileRequestDataType
) => Promise<UploadAvatarHeadersType | undefined>;
@ -1677,6 +1681,7 @@ export function initialize({
getAccountForUsername,
getAttachment,
getAttachmentFromBackupTier,
getAttachmentUploadForm,
getAvatar,
getBackupCredentials,
getBackupCDNCredentials,
@ -3480,16 +3485,21 @@ export function initialize({
return combinedStream;
}
async function putEncryptedAttachment(encryptedBin: Uint8Array) {
const response = attachmentV3Response.parse(
async function getAttachmentUploadForm() {
return attachmentV3Response.parse(
await _ajax({
call: 'attachmentId',
call: 'attachmentUploadForm',
httpType: 'GET',
responseType: 'json',
})
);
}
const { signedUploadLocation, key: cdnKey, headers } = response;
async function putEncryptedAttachment(
encryptedBin: Uint8Array | Readable,
uploadForm: AttachmentV3ResponseType
) {
const { signedUploadLocation, headers } = uploadForm;
// This is going to the CDN, not the service, so we use _outerAjax
const { response: uploadResponse } = await _outerAjax(
@ -3532,8 +3542,6 @@ export function initialize({
return `${tmp}[REDACTED]`;
},
});
return cdnKey;
}
function getHeaderPadding() {