scale down avatars before upload to 512x512

pull/1387/head
Audric Ackermann 5 years ago
parent 876247b5ad
commit d218e00bd3
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -722,6 +722,24 @@
let profileKey = null; let profileKey = null;
if (avatar) { if (avatar) {
const data = await readFile({ file: avatar }); const data = await readFile({ file: avatar });
// Ensure that this file is either small enough or is resized to meet our
// requirements for attachments
const withBlob = await window.Signal.Util.AttachmentUtil.autoScale(
{
contentType: avatar.type,
file: new Blob([data.data], {
type: avatar.contentType,
}),
maxMeasurements: {
maxSize: 1000 * 1024,
maxHeight: 512,
maxWidth: 512,
},
}
);
const dataResized = await window.Signal.Types.Attachment.arrayBufferFromFile(
withBlob.file
);
// For simplicity we use the same attachment pointer that would send to // For simplicity we use the same attachment pointer that would send to
// others, which means we need to wait for the database response. // others, which means we need to wait for the database response.
@ -734,13 +752,13 @@
// Encrypt with a new key every time // Encrypt with a new key every time
profileKey = libsignal.crypto.getRandomBytes(32); profileKey = libsignal.crypto.getRandomBytes(32);
const encryptedData = await textsecure.crypto.encryptProfile( const encryptedData = await textsecure.crypto.encryptProfile(
data.data, dataResized,
profileKey profileKey
); );
const avatarPointer = await libsession.Utils.AttachmentUtils.uploadAvatar( const avatarPointer = await libsession.Utils.AttachmentUtils.uploadAvatar(
{ {
...data, ...dataResized,
data: encryptedData, data: encryptedData,
size: encryptedData.byteLength, size: encryptedData.byteLength,
} }

@ -1,10 +1,16 @@
import { StagedAttachmentType } from '../components/session/conversation/SessionCompositionBox'; import { StagedAttachmentType } from '../components/session/conversation/SessionCompositionBox';
import { SignalService } from '../protobuf'; import { SignalService } from '../protobuf';
export async function autoScale<T extends { contentType: string; file: any }>( export interface MaxScaleSize {
attachment: T maxSize: number;
): Promise<T> { maxHeight: number;
const { contentType, file } = attachment; maxWidth: number;
}
export async function autoScale<
T extends { contentType: string; file: any; maxMeasurements?: MaxScaleSize }
>(attachment: T): Promise<T> {
const { contentType, file, maxMeasurements } = attachment;
if (contentType.split('/')[0] !== 'image' || contentType === 'image/tiff') { if (contentType.split('/')[0] !== 'image' || contentType === 'image/tiff') {
// nothing to do // nothing to do
return Promise.resolve(attachment); return Promise.resolve(attachment);
@ -17,9 +23,10 @@ export async function autoScale<T extends { contentType: string; file: any }>(
img.onload = () => { img.onload = () => {
URL.revokeObjectURL(url); URL.revokeObjectURL(url);
const maxSize = 6000 * 1024; const maxSize = maxMeasurements?.maxSize || 6000 * 1024;
const maxHeight = 4096; const maxHeight = maxMeasurements?.maxHeight || 4096;
const maxWidth = 4096; const maxWidth = maxMeasurements?.maxWidth || 4096;
if ( if (
img.naturalWidth <= maxWidth && img.naturalWidth <= maxWidth &&
img.naturalHeight <= maxHeight && img.naturalHeight <= maxHeight &&

Loading…
Cancel
Save