Update mediaName derivation to hex encoding

This commit is contained in:
trevor-signal 2024-08-01 02:13:38 -04:00 committed by GitHub
parent 5173b3d01a
commit 0433264eed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 44 additions and 24 deletions

View file

@ -271,7 +271,7 @@ export function deriveMediaIdFromMediaName(
BACKUP_MEDIA_ID_LEN, BACKUP_MEDIA_ID_LEN,
Buffer.from(backupKey), Buffer.from(backupKey),
Buffer.from(BACKUP_MEDIA_ID_INFO), Buffer.from(BACKUP_MEDIA_ID_INFO),
Buffer.from(Bytes.fromBase64(mediaName)) Buffer.from(mediaName, 'utf8')
); );
} }

View file

@ -42,13 +42,13 @@ export function getMediaNameForAttachment(attachment: AttachmentType): string {
return attachment.backupLocator.mediaName; return attachment.backupLocator.mediaName;
} }
strictAssert(attachment.digest, 'Digest must be present'); strictAssert(attachment.digest, 'Digest must be present');
return attachment.digest; return Bytes.toHex(Bytes.fromBase64(attachment.digest));
} }
export function getMediaNameForAttachmentThumbnail( export function getMediaNameForAttachmentThumbnail(
fullsizeMediaName: string fullsizeMediaName: string
): string { ): string {
return Bytes.toBase64(Bytes.fromString(`${fullsizeMediaName}_thumbnail`)); return `${fullsizeMediaName}_thumbnail`;
} }
export function getBytesFromMediaIdString(mediaId: string): Uint8Array { export function getBytesFromMediaIdString(mediaId: string): Uint8Array {

View file

@ -77,6 +77,10 @@ describe('backup/attachments', () => {
return Bytes.toBase64(Bytes.fromString(str)); return Bytes.toBase64(Bytes.fromString(str));
} }
function digestToMediaName(digestBase64: string): string {
return Bytes.toHex(Bytes.fromBase64(digestBase64));
}
function composeAttachment( function composeAttachment(
index: number, index: number,
overrides?: Partial<AttachmentType> overrides?: Partial<AttachmentType>
@ -169,7 +173,9 @@ describe('backup/attachments', () => {
'thumbnail', 'thumbnail',
'uploadTimestamp', 'uploadTimestamp',
]), ]),
backupLocator: { mediaName: attachment.digest }, backupLocator: {
mediaName: digestToMediaName(attachment.digest),
},
}, },
], ],
}), }),
@ -201,7 +207,9 @@ describe('backup/attachments', () => {
'thumbnail', 'thumbnail',
'uploadTimestamp', 'uploadTimestamp',
]), ]),
backupLocator: { mediaName: attachment.digest }, backupLocator: {
mediaName: digestToMediaName(attachment.digest),
},
}, },
], ],
}), }),
@ -271,7 +279,9 @@ describe('backup/attachments', () => {
'thumbnail', 'thumbnail',
'uploadTimestamp', 'uploadTimestamp',
]), ]),
backupLocator: { mediaName: attachment.digest }, backupLocator: {
mediaName: digestToMediaName(attachment.digest),
},
}, },
}, },
], ],
@ -332,7 +342,9 @@ describe('backup/attachments', () => {
'thumbnail', 'thumbnail',
'uploadTimestamp', 'uploadTimestamp',
]), ]),
backupLocator: { mediaName: attachment.digest }, backupLocator: {
mediaName: digestToMediaName(attachment.digest),
},
}, },
isProfile: false, isProfile: false,
}, },
@ -417,7 +429,9 @@ describe('backup/attachments', () => {
'uploadTimestamp', 'uploadTimestamp',
'thumbnail', 'thumbnail',
]), ]),
backupLocator: { mediaName: attachment.digest }, backupLocator: {
mediaName: digestToMediaName(attachment.digest),
},
}, },
contentType: VIDEO_MP4, contentType: VIDEO_MP4,
}, },
@ -468,7 +482,9 @@ describe('backup/attachments', () => {
'uploadTimestamp', 'uploadTimestamp',
'thumbnail', 'thumbnail',
]), ]),
backupLocator: { mediaName: existingAttachment.digest }, backupLocator: {
mediaName: digestToMediaName(existingAttachment.digest),
},
}, },
], ],
}, },
@ -483,7 +499,9 @@ describe('backup/attachments', () => {
// been downloaded // been downloaded
thumbnail: { thumbnail: {
...omit(quoteAttachment, ['iv', 'path', 'uploadTimestamp']), ...omit(quoteAttachment, ['iv', 'path', 'uploadTimestamp']),
backupLocator: { mediaName: quoteAttachment.digest }, backupLocator: {
mediaName: digestToMediaName(quoteAttachment.digest),
},
}, },
contentType: VIDEO_MP4, contentType: VIDEO_MP4,
}, },
@ -563,7 +581,7 @@ describe('backup/attachments', () => {
key, key,
digest, digest,
backupLocator: { backupLocator: {
mediaName: digest, mediaName: digestToMediaName(digest),
}, },
}); });
}, },
@ -635,7 +653,7 @@ describe('backup/attachments', () => {
key, key,
digest, digest,
backupLocator: { backupLocator: {
mediaName: digest, mediaName: digestToMediaName(digest),
}, },
}); });
}, },
@ -716,7 +734,9 @@ describe('backup/attachments', () => {
'thumbnail', 'thumbnail',
'uploadTimestamp', 'uploadTimestamp',
]), ]),
backupLocator: { mediaName: attachment.digest }, backupLocator: {
mediaName: digestToMediaName(attachment.digest),
},
}, },
}, },
}), }),

View file

@ -161,6 +161,9 @@ describe('convertFilePointerToAttachment', () => {
}); });
}); });
const defaultDigest = Bytes.fromBase64('digest');
const defaultMediaName = Bytes.toHex(defaultDigest);
function composeAttachment( function composeAttachment(
overrides: Partial<AttachmentType> = {} overrides: Partial<AttachmentType> = {}
): AttachmentType { ): AttachmentType {
@ -171,7 +174,7 @@ function composeAttachment(
cdnNumber: 2, cdnNumber: 2,
path: 'path/to/file.png', path: 'path/to/file.png',
key: 'key', key: 'key',
digest: 'digest', digest: Bytes.toBase64(defaultDigest),
iv: 'iv', iv: 'iv',
width: 100, width: 100,
height: 100, height: 100,
@ -202,17 +205,16 @@ const defaultAttachmentLocator = new Backups.FilePointer.AttachmentLocator({
cdnKey: 'cdnKey', cdnKey: 'cdnKey',
cdnNumber: 2, cdnNumber: 2,
key: Bytes.fromBase64('key'), key: Bytes.fromBase64('key'),
digest: Bytes.fromBase64('digest'), digest: defaultDigest,
size: 100, size: 100,
uploadTimestamp: Long.fromNumber(1234), uploadTimestamp: Long.fromNumber(1234),
}); });
const defaultMediaName = 'digest';
const defaultBackupLocator = new Backups.FilePointer.BackupLocator({ const defaultBackupLocator = new Backups.FilePointer.BackupLocator({
mediaName: defaultMediaName, mediaName: defaultMediaName,
cdnNumber: null, cdnNumber: null,
key: Bytes.fromBase64('key'), key: Bytes.fromBase64('key'),
digest: Bytes.fromBase64('digest'), digest: defaultDigest,
size: Long.fromNumber(100), size: Long.fromNumber(100),
transitCdnKey: 'cdnKey', transitCdnKey: 'cdnKey',
transitCdnNumber: 2, transitCdnNumber: 2,
@ -455,7 +457,7 @@ describe('getFilePointerForAttachment', () => {
...defaultBackupLocator, ...defaultBackupLocator,
key: newKey, key: newKey,
digest: newDigest, digest: newDigest,
mediaName: Bytes.toBase64(newDigest), mediaName: Bytes.toHex(newDigest),
transitCdnKey: undefined, transitCdnKey: undefined,
transitCdnNumber: undefined, transitCdnNumber: undefined,
}), }),
@ -497,7 +499,7 @@ describe('getFilePointerForAttachment', () => {
...defaultBackupLocator, ...defaultBackupLocator,
key: newKey, key: newKey,
digest: newDigest, digest: newDigest,
mediaName: Bytes.toBase64(newDigest), mediaName: Bytes.toHex(newDigest),
transitCdnKey: undefined, transitCdnKey: undefined,
transitCdnNumber: undefined, transitCdnNumber: undefined,
}), }),
@ -590,14 +592,14 @@ describe('getBackupJobForAttachmentAndFilePointer', async () => {
getBackupCdnInfo: notInBackupCdn, getBackupCdnInfo: notInBackupCdn,
}), }),
{ {
mediaName: 'digest', mediaName: Bytes.toHex(defaultDigest),
receivedAt: 100, receivedAt: 100,
type: 'standard', type: 'standard',
data: { data: {
path: 'path/to/file.png', path: 'path/to/file.png',
contentType: IMAGE_PNG, contentType: IMAGE_PNG,
keys: 'key', keys: 'key',
digest: 'digest', digest: Bytes.toBase64(defaultDigest),
iv: 'iv', iv: 'iv',
size: 100, size: 100,
localKey: attachment.localKey, localKey: attachment.localKey,

View file

@ -343,9 +343,7 @@ describe('AttachmentBackupManager/JobManager', function attachmentBackupManager(
jobForNonVisualAttachment jobForNonVisualAttachment
); );
const thumbnailMediaName = Bytes.toBase64( const thumbnailMediaName = `${jobForVisualAttachment.mediaName}_thumbnail`;
Bytes.fromString(`${jobForVisualAttachment.mediaName}_thumbnail`)
);
const allJobs = await getAllSavedJobs(); const allJobs = await getAllSavedJobs();
assert.strictEqual(allJobs.length, 3); assert.strictEqual(allJobs.length, 3);
assert.sameMembers( assert.sameMembers(