Retain cdn ids for attachments

This commit is contained in:
Fedor Indutny 2023-10-04 02:09:31 +02:00 committed by GitHub
parent 94a5968764
commit ba1a8aad09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 14 deletions

View file

@ -31,6 +31,7 @@ import type {
UploadedAttachmentType, UploadedAttachmentType,
AttachmentWithHydratedData, AttachmentWithHydratedData,
} from '../../types/Attachment'; } from '../../types/Attachment';
import { copyCdnFields } from '../../util/attachments';
import { LONG_MESSAGE, MIMETypeToString } from '../../types/MIME'; import { LONG_MESSAGE, MIMETypeToString } from '../../types/MIME';
import type { RawBodyRange } from '../../types/BodyRange'; import type { RawBodyRange } from '../../types/BodyRange';
import type { import type {
@ -628,7 +629,10 @@ async function uploadSingleAttachment(
); );
const newAttachments = [...oldAttachments]; const newAttachments = [...oldAttachments];
newAttachments[index].digest = Bytes.toBase64(uploaded.digest); newAttachments[index] = {
...newAttachments[index],
...copyCdnFields(uploaded),
};
message.set('attachments', newAttachments); message.set('attachments', newAttachments);
@ -698,14 +702,11 @@ async function uploadMessageQuote(
const attachmentAfterThumbnailUpload = const attachmentAfterThumbnailUpload =
attachmentsAfterThumbnailUpload[index]; attachmentsAfterThumbnailUpload[index];
const digest = attachmentAfterThumbnailUpload.thumbnail
? Bytes.toBase64(attachmentAfterThumbnailUpload.thumbnail.digest)
: undefined;
return { return {
...attachment, ...attachment,
thumbnail: { thumbnail: {
...attachment.thumbnail, ...attachment.thumbnail,
digest, ...copyCdnFields(attachmentAfterThumbnailUpload.thumbnail),
}, },
}; };
}), }),
@ -783,7 +784,7 @@ async function uploadMessagePreviews(
...preview, ...preview,
image: { image: {
...preview.image, ...preview.image,
digest: Bytes.toBase64(uploaded.image.digest), ...copyCdnFields(uploaded.image),
}, },
}; };
}); });
@ -829,7 +830,7 @@ async function uploadMessageSticker(
...existingSticker, ...existingSticker,
data: { data: {
...existingSticker.data, ...existingSticker.data,
digest: Bytes.toBase64(uploaded.digest), ...copyCdnFields(uploaded),
}, },
}); });
@ -917,7 +918,7 @@ async function uploadMessageContacts(
...contact.avatar, ...contact.avatar,
avatar: { avatar: {
...contact.avatar.avatar, ...contact.avatar.avatar,
digest: Bytes.toBase64(uploaded.avatar.avatar.digest), ...copyCdnFields(uploaded.avatar.avatar),
}, },
}, },
}; };

View file

@ -42,6 +42,7 @@ import { SendMessageProtoError } from '../textsecure/Errors';
import * as expirationTimer from '../util/expirationTimer'; import * as expirationTimer from '../util/expirationTimer';
import { getUserLanguages } from '../util/userLanguages'; import { getUserLanguages } from '../util/userLanguages';
import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp'; import { getMessageSentTimestamp } from '../util/getMessageSentTimestamp';
import { copyCdnFields } from '../util/attachments';
import type { ReactionType } from '../types/Reactions'; import type { ReactionType } from '../types/Reactions';
import type { ServiceIdString } from '../types/ServiceId'; import type { ServiceIdString } from '../types/ServiceId';
@ -2007,6 +2008,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
): Promise<void> { ): Promise<void> {
const { attachments } = quote; const { attachments } = quote;
const firstAttachment = attachments ? attachments[0] : undefined; const firstAttachment = attachments ? attachments[0] : undefined;
const firstThumbnailCdnFields = copyCdnFields(firstAttachment?.thumbnail);
if (messageHasPaymentEvent(originalMessage.attributes)) { if (messageHasPaymentEvent(originalMessage.attributes)) {
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
@ -2100,6 +2102,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
if (thumbnail && thumbnail.path) { if (thumbnail && thumbnail.path) {
firstAttachment.thumbnail = { firstAttachment.thumbnail = {
...firstThumbnailCdnFields,
...thumbnail, ...thumbnail,
copied: true, copied: true,
}; };
@ -2113,6 +2116,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
if (image && image.path) { if (image && image.path) {
firstAttachment.thumbnail = { firstAttachment.thumbnail = {
...firstThumbnailCdnFields,
...image, ...image,
copied: true, copied: true,
}; };
@ -2122,6 +2126,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
const sticker = originalMessage.get('sticker'); const sticker = originalMessage.get('sticker');
if (sticker && sticker.data && sticker.data.path) { if (sticker && sticker.data && sticker.data.path) {
firstAttachment.thumbnail = { firstAttachment.thumbnail = {
...firstThumbnailCdnFields,
...sticker.data, ...sticker.data,
copied: true, copied: true,
}; };

View file

@ -1,7 +1,7 @@
// Copyright 2020 Signal Messenger, LLC // Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
import { isNumber, omit } from 'lodash'; import { isNumber } from 'lodash';
import { strictAssert } from '../util/assert'; import { strictAssert } from '../util/assert';
import { dropNull } from '../util/dropNull'; import { dropNull } from '../util/dropNull';
@ -58,7 +58,7 @@ export async function downloadAttachment(
const data = getFirstBytes(paddedData, size); const data = getFirstBytes(paddedData, size);
return { return {
...omit(attachment, 'key'), ...attachment,
size, size,
contentType: contentType contentType: contentType

View file

@ -72,6 +72,7 @@ export type AttachmentType = {
cdnNumber?: number; cdnNumber?: number;
cdnId?: string; cdnId?: string;
cdnKey?: string; cdnKey?: string;
key?: string;
data?: Uint8Array; data?: Uint8Array;
textAttachment?: TextAttachmentType; textAttachment?: TextAttachmentType;
@ -80,9 +81,6 @@ export type AttachmentType = {
/** Legacy field, used long ago for migrating attachments to disk. */ /** Legacy field, used long ago for migrating attachments to disk. */
schemaVersion?: number; schemaVersion?: number;
/** Removed once we download the attachment */
key?: string;
}; };
export type UploadedAttachmentType = Proto.IAttachmentPointer & export type UploadedAttachmentType = Proto.IAttachmentPointer &

View file

@ -4,11 +4,16 @@
import { blobToArrayBuffer } from 'blob-util'; import { blobToArrayBuffer } from 'blob-util';
import { scaleImageToLevel } from './scaleImageToLevel'; import { scaleImageToLevel } from './scaleImageToLevel';
import type { AttachmentType } from '../types/Attachment'; import { dropNull } from './dropNull';
import type {
AttachmentType,
UploadedAttachmentType,
} from '../types/Attachment';
import { canBeTranscoded } from '../types/Attachment'; import { canBeTranscoded } from '../types/Attachment';
import type { LoggerType } from '../types/Logging'; import type { LoggerType } from '../types/Logging';
import * as MIME from '../types/MIME'; import * as MIME from '../types/MIME';
import * as Errors from '../types/errors'; import * as Errors from '../types/errors';
import * as Bytes from '../Bytes';
// Upgrade steps // Upgrade steps
// NOTE: This step strips all EXIF metadata from JPEG images as // NOTE: This step strips all EXIF metadata from JPEG images as
@ -74,3 +79,23 @@ export async function autoOrientJPEG(
return attachment; return attachment;
} }
} }
export type CdnFieldsType = Pick<
AttachmentType,
'cdnId' | 'cdnKey' | 'cdnNumber' | 'key' | 'digest'
>;
export function copyCdnFields(
uploaded?: UploadedAttachmentType
): CdnFieldsType {
if (!uploaded) {
return {};
}
return {
cdnId: dropNull(uploaded.cdnId)?.toString(),
cdnKey: uploaded.cdnKey,
cdnNumber: dropNull(uploaded.cdnNumber),
key: Bytes.toBase64(uploaded.key),
digest: Bytes.toBase64(uploaded.digest),
};
}