Use v4/attachments endpoint for attachment upload forms

This commit is contained in:
trevor-signal 2024-07-16 16:25:43 -04:00 committed by GitHub
parent 826b361757
commit 451ee56c92
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 21 additions and 19 deletions

View file

@ -537,7 +537,7 @@ function makeHTTPError(
const URL_CALLS = {
accountExistence: 'v1/accounts/account',
attachmentUploadForm: 'v3/attachments/form/upload',
attachmentUploadForm: 'v4/attachments/form/upload',
attestation: 'v1/attestation',
batchIdentityCheck: 'v1/profile/identity_check/batch',
challenge: 'v1/challenge',
@ -982,14 +982,16 @@ export type ReportMessageOptionsType = Readonly<{
token?: string;
}>;
const attachmentV3Response = z.object({
const attachmentUploadFormResponse = z.object({
cdn: z.literal(2).or(z.literal(3)),
key: z.string(),
headers: z.record(z.string()),
signedUploadLocation: z.string(),
});
export type AttachmentV3ResponseType = z.infer<typeof attachmentV3Response>;
export type AttachmentUploadFormResponseType = z.infer<
typeof attachmentUploadFormResponse
>;
export type ServerKeyCountType = {
count: number;
@ -1214,7 +1216,7 @@ export type WebAPIType = {
timeout?: number;
};
}) => Promise<Readable>;
getAttachmentUploadForm: () => Promise<AttachmentV3ResponseType>;
getAttachmentUploadForm: () => Promise<AttachmentUploadFormResponseType>;
getAvatar: (path: string) => Promise<Uint8Array>;
getHasSubscription: (subscriberId: Uint8Array) => Promise<boolean>;
getGroup: (options: GroupCredentialsType) => Promise<Proto.IGroupResponse>;
@ -1304,7 +1306,7 @@ export type WebAPIType = {
) => Promise<VerifyServiceIdResponseType>;
putEncryptedAttachment: (
encryptedBin: Uint8Array | (() => Readable),
uploadForm: AttachmentV3ResponseType
uploadForm: AttachmentUploadFormResponseType
) => Promise<void>;
putProfile: (
jsonData: ProfileRequestDataType
@ -1368,17 +1370,17 @@ export type WebAPIType = {
}
) => Promise<MultiRecipient200ResponseType>;
createFetchForAttachmentUpload(
attachment: AttachmentV3ResponseType
attachment: AttachmentUploadFormResponseType
): FetchFunctionType;
getBackupInfo: (
headers: BackupPresentationHeadersType
) => Promise<GetBackupInfoResponseType>;
getBackupUploadForm: (
headers: BackupPresentationHeadersType
) => Promise<AttachmentV3ResponseType>;
) => Promise<AttachmentUploadFormResponseType>;
getBackupMediaUploadForm: (
headers: BackupPresentationHeadersType
) => Promise<AttachmentV3ResponseType>;
) => Promise<AttachmentUploadFormResponseType>;
refreshBackup: (headers: BackupPresentationHeadersType) => Promise<void>;
getBackupCredentials: (
options: GetBackupCredentialsOptionsType
@ -2769,14 +2771,14 @@ export function initialize({
responseType: 'json',
});
return attachmentV3Response.parse(res);
return attachmentUploadFormResponse.parse(res);
}
function createFetchForAttachmentUpload({
signedUploadLocation,
headers: uploadHeaders,
cdn,
}: AttachmentV3ResponseType): FetchFunctionType {
}: AttachmentUploadFormResponseType): FetchFunctionType {
strictAssert(cdn === 3, 'Fetch can only be created for CDN 3');
const { origin: expectedOrigin } = new URL(signedUploadLocation);
@ -2820,7 +2822,7 @@ export function initialize({
responseType: 'json',
});
return attachmentV3Response.parse(res);
return attachmentUploadFormResponse.parse(res);
}
async function refreshBackup(headers: BackupPresentationHeadersType) {
@ -3520,7 +3522,7 @@ export function initialize({
}
async function getAttachmentUploadForm() {
return attachmentV3Response.parse(
return attachmentUploadFormResponse.parse(
await _ajax({
call: 'attachmentUploadForm',
httpType: 'GET',
@ -3531,7 +3533,7 @@ export function initialize({
async function putEncryptedAttachment(
encryptedBin: Uint8Array | (() => Readable),
uploadForm: AttachmentV3ResponseType
uploadForm: AttachmentUploadFormResponseType
) {
const { signedUploadLocation, headers } = uploadForm;
@ -3558,7 +3560,7 @@ export function initialize({
const uploadLocation = uploadResponse.headers.get('location');
strictAssert(
uploadLocation,
'attachment v3 response header has no location'
'attachment upload form header has no location'
);
// This is going to the CDN, not the service, so we use _outerAjax