Use new attachments API endpoint
This commit is contained in:
parent
d7bd4eb156
commit
f1624705a7
3 changed files with 51 additions and 29 deletions
|
@ -472,7 +472,7 @@ function makeHTTPError(
|
||||||
const URL_CALLS = {
|
const URL_CALLS = {
|
||||||
accounts: 'v1/accounts',
|
accounts: 'v1/accounts',
|
||||||
accountExistence: 'v1/accounts/account',
|
accountExistence: 'v1/accounts/account',
|
||||||
attachmentId: 'v2/attachments/form/upload',
|
attachmentId: 'v3/attachments/form/upload',
|
||||||
attestation: 'v1/attestation',
|
attestation: 'v1/attestation',
|
||||||
batchIdentityCheck: 'v1/profile/identity_check/batch',
|
batchIdentityCheck: 'v1/profile/identity_check/batch',
|
||||||
boostBadges: 'v1/subscription/boost/badges',
|
boostBadges: 'v1/subscription/boost/badges',
|
||||||
|
@ -841,6 +841,15 @@ const artAuthZod = z.object({
|
||||||
|
|
||||||
export type ArtAuthType = z.infer<typeof artAuthZod>;
|
export type ArtAuthType = z.infer<typeof artAuthZod>;
|
||||||
|
|
||||||
|
const attachmentV3Response = z.object({
|
||||||
|
cdn: z.literal(2),
|
||||||
|
key: z.string(),
|
||||||
|
headers: z.record(z.string()),
|
||||||
|
signedUploadLocation: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type AttachmentV3ResponseType = z.infer<typeof attachmentV3Response>;
|
||||||
|
|
||||||
export type WebAPIType = {
|
export type WebAPIType = {
|
||||||
startRegistration(): unknown;
|
startRegistration(): unknown;
|
||||||
finishRegistration(baton: unknown): void;
|
finishRegistration(baton: unknown): void;
|
||||||
|
@ -2334,7 +2343,7 @@ export function initialize({
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerAttachmentType = {
|
type ServerV2AttachmentType = {
|
||||||
key: string;
|
key: string;
|
||||||
credential: string;
|
credential: string;
|
||||||
acl: string;
|
acl: string;
|
||||||
|
@ -2353,7 +2362,7 @@ export function initialize({
|
||||||
date,
|
date,
|
||||||
policy,
|
policy,
|
||||||
signature,
|
signature,
|
||||||
}: ServerAttachmentType,
|
}: ServerV2AttachmentType,
|
||||||
encryptedBin: Uint8Array
|
encryptedBin: Uint8Array
|
||||||
) {
|
) {
|
||||||
// Note: when using the boundary string in the POST body, it needs to be prefixed by
|
// Note: when using the boundary string in the POST body, it needs to be prefixed by
|
||||||
|
@ -2416,8 +2425,8 @@ export function initialize({
|
||||||
urlParameters: `/${encryptedStickers.length}`,
|
urlParameters: `/${encryptedStickers.length}`,
|
||||||
})) as {
|
})) as {
|
||||||
packId: string;
|
packId: string;
|
||||||
manifest: ServerAttachmentType;
|
manifest: ServerV2AttachmentType;
|
||||||
stickers: ReadonlyArray<ServerAttachmentType>;
|
stickers: ReadonlyArray<ServerV2AttachmentType>;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Upload manifest
|
// Upload manifest
|
||||||
|
@ -2439,7 +2448,7 @@ export function initialize({
|
||||||
throwOnTimeout: true,
|
throwOnTimeout: true,
|
||||||
});
|
});
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
stickers.map(async (sticker: ServerAttachmentType, index: number) => {
|
stickers.map(async (sticker: ServerV2AttachmentType, index: number) => {
|
||||||
const stickerParams = makePutParams(
|
const stickerParams = makePutParams(
|
||||||
sticker,
|
sticker,
|
||||||
encryptedStickers[index]
|
encryptedStickers[index]
|
||||||
|
@ -2503,32 +2512,48 @@ export function initialize({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type PutAttachmentResponseType = ServerAttachmentType & {
|
|
||||||
attachmentIdString: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
async function putEncryptedAttachment(encryptedBin: Uint8Array) {
|
async function putEncryptedAttachment(encryptedBin: Uint8Array) {
|
||||||
const response = (await _ajax({
|
const response = attachmentV3Response.parse(
|
||||||
|
await _ajax({
|
||||||
call: 'attachmentId',
|
call: 'attachmentId',
|
||||||
httpType: 'GET',
|
httpType: 'GET',
|
||||||
responseType: 'json',
|
responseType: 'json',
|
||||||
})) as PutAttachmentResponseType;
|
})
|
||||||
|
);
|
||||||
|
|
||||||
const { attachmentIdString } = response;
|
const { signedUploadLocation, key: cdnKey, headers } = response;
|
||||||
|
|
||||||
const params = makePutParams(response, encryptedBin);
|
|
||||||
|
|
||||||
// This is going to the CDN, not the service, so we use _outerAjax
|
// This is going to the CDN, not the service, so we use _outerAjax
|
||||||
await _outerAjax(`${cdnUrlObject['0']}/attachments/`, {
|
const { response: uploadResponse } = await _outerAjax(
|
||||||
...params,
|
signedUploadLocation,
|
||||||
|
{
|
||||||
|
responseType: 'byteswithdetails',
|
||||||
certificateAuthority,
|
certificateAuthority,
|
||||||
proxyUrl,
|
proxyUrl,
|
||||||
timeout: 0,
|
timeout: 0,
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
version,
|
version,
|
||||||
|
headers,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const uploadLocation = uploadResponse.headers.get('location');
|
||||||
|
strictAssert(
|
||||||
|
uploadLocation,
|
||||||
|
'attachment v3 response header has no location'
|
||||||
|
);
|
||||||
|
|
||||||
|
// This is going to the CDN, not the service, so we use _outerAjax
|
||||||
|
await _outerAjax(uploadLocation, {
|
||||||
|
certificateAuthority,
|
||||||
|
proxyUrl,
|
||||||
|
timeout: 0,
|
||||||
|
type: 'PUT',
|
||||||
|
version,
|
||||||
|
data: encryptedBin,
|
||||||
});
|
});
|
||||||
|
|
||||||
return attachmentIdString;
|
return cdnKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getHeaderPadding() {
|
function getHeaderPadding() {
|
||||||
|
|
|
@ -88,7 +88,7 @@ export type AttachmentType = {
|
||||||
export type UploadedAttachmentType = Proto.IAttachmentPointer &
|
export type UploadedAttachmentType = Proto.IAttachmentPointer &
|
||||||
Readonly<{
|
Readonly<{
|
||||||
// Required fields
|
// Required fields
|
||||||
cdnId: Long;
|
cdnKey: string;
|
||||||
key: Uint8Array;
|
key: Uint8Array;
|
||||||
size: number;
|
size: number;
|
||||||
digest: Uint8Array;
|
digest: Uint8Array;
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
// Copyright 2023 Signal Messenger, LLC
|
// Copyright 2023 Signal Messenger, LLC
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
import Long from 'long';
|
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
AttachmentWithHydratedData,
|
AttachmentWithHydratedData,
|
||||||
UploadedAttachmentType,
|
UploadedAttachmentType,
|
||||||
|
@ -20,12 +18,11 @@ export async function uploadAttachment(
|
||||||
const { server } = window.textsecure;
|
const { server } = window.textsecure;
|
||||||
strictAssert(server, 'WebAPI must be initialized');
|
strictAssert(server, 'WebAPI must be initialized');
|
||||||
|
|
||||||
const attachmentIdString = await server.putEncryptedAttachment(
|
const cdnKey = await server.putEncryptedAttachment(encrypted.ciphertext);
|
||||||
encrypted.ciphertext
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
cdnId: Long.fromString(attachmentIdString),
|
cdnKey,
|
||||||
|
cdnNumber: 2,
|
||||||
key: keys,
|
key: keys,
|
||||||
size: attachment.data.byteLength,
|
size: attachment.data.byteLength,
|
||||||
digest: encrypted.digest,
|
digest: encrypted.digest,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue