|
|
@ -5,7 +5,7 @@ import { sendDataExtractionNotification } from '../session/messages/outgoing/con
|
|
|
|
import { AttachmentType, save } from '../types/Attachment';
|
|
|
|
import { AttachmentType, save } from '../types/Attachment';
|
|
|
|
import { StagedAttachmentType } from '../components/conversation/composition/CompositionBox';
|
|
|
|
import { StagedAttachmentType } from '../components/conversation/composition/CompositionBox';
|
|
|
|
import { getAbsoluteAttachmentPath, processNewAttachment } from '../types/MessageAttachment';
|
|
|
|
import { getAbsoluteAttachmentPath, processNewAttachment } from '../types/MessageAttachment';
|
|
|
|
import { arrayBufferToBlob, dataURLToBlob } from 'blob-util';
|
|
|
|
import { arrayBufferToBlob } from 'blob-util';
|
|
|
|
import { IMAGE_GIF, IMAGE_JPEG, IMAGE_PNG, IMAGE_TIFF, IMAGE_UNKNOWN } from '../types/MIME';
|
|
|
|
import { IMAGE_GIF, IMAGE_JPEG, IMAGE_PNG, IMAGE_TIFF, IMAGE_UNKNOWN } from '../types/MIME';
|
|
|
|
import { THUMBNAIL_SIDE } from '../types/attachments/VisualAttachment';
|
|
|
|
import { THUMBNAIL_SIDE } from '../types/attachments/VisualAttachment';
|
|
|
|
|
|
|
|
|
|
|
@ -118,6 +118,22 @@ export async function autoScaleForThumbnail<T extends { contentType: string; blo
|
|
|
|
return autoScale(attachment, maxMeasurements);
|
|
|
|
return autoScale(attachment, maxMeasurements);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function canvasToBlob(
|
|
|
|
|
|
|
|
canvas: HTMLCanvasElement,
|
|
|
|
|
|
|
|
type: string,
|
|
|
|
|
|
|
|
quality: number
|
|
|
|
|
|
|
|
): Promise<Blob | null> {
|
|
|
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
|
|
|
canvas.toBlob(
|
|
|
|
|
|
|
|
blob => {
|
|
|
|
|
|
|
|
resolve(blob);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
type,
|
|
|
|
|
|
|
|
quality
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Scale down an image to fit in the required dimension.
|
|
|
|
* Scale down an image to fit in the required dimension.
|
|
|
|
* Note: This method won't crop if needed,
|
|
|
|
* Note: This method won't crop if needed,
|
|
|
@ -171,6 +187,8 @@ export async function autoScale<T extends { contentType: string; blob: Blob }>(
|
|
|
|
maxWidth: makeSquare ? maxMeasurements?.maxSide : maxWidth,
|
|
|
|
maxWidth: makeSquare ? maxMeasurements?.maxSide : maxWidth,
|
|
|
|
maxHeight: makeSquare ? maxMeasurements?.maxSide : maxHeight,
|
|
|
|
maxHeight: makeSquare ? maxMeasurements?.maxSide : maxHeight,
|
|
|
|
...crop,
|
|
|
|
...crop,
|
|
|
|
|
|
|
|
orientation: 1,
|
|
|
|
|
|
|
|
aspectRatio: makeSquare ? 1 : undefined,
|
|
|
|
canvas: true,
|
|
|
|
canvas: true,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -198,16 +216,34 @@ export async function autoScale<T extends { contentType: string; blob: Blob }>(
|
|
|
|
blob,
|
|
|
|
blob,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.log.debug('canvas.originalWidth', {
|
|
|
|
|
|
|
|
canvasOriginalWidth: canvas.originalWidth,
|
|
|
|
|
|
|
|
canvasOriginalHeight: canvas.originalHeight,
|
|
|
|
|
|
|
|
maxWidth,
|
|
|
|
|
|
|
|
maxHeight,
|
|
|
|
|
|
|
|
blobsize: blob.size,
|
|
|
|
|
|
|
|
maxSize,
|
|
|
|
|
|
|
|
makeSquare,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
let quality = 0.95;
|
|
|
|
let quality = 0.95;
|
|
|
|
let i = 4;
|
|
|
|
let i = 4;
|
|
|
|
|
|
|
|
const start = Date.now();
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
i -= 1;
|
|
|
|
i -= 1;
|
|
|
|
window.log.info('autoscale of ', attachment, i);
|
|
|
|
window.log.info(`autoscale iteration: [${i}] for:`, attachment);
|
|
|
|
readAndResizedBlob = dataURLToBlob(
|
|
|
|
|
|
|
|
(canvas.image as HTMLCanvasElement).toDataURL('image/jpeg', quality)
|
|
|
|
perfStart(`autoscale-canvasToBlob-${attachment.blob.size}`);
|
|
|
|
|
|
|
|
const tempBlob = await canvasToBlob(canvas.image as HTMLCanvasElement, 'image/jpeg', quality);
|
|
|
|
|
|
|
|
perfEnd(
|
|
|
|
|
|
|
|
`autoscale-canvasToBlob-${attachment.blob.size}`,
|
|
|
|
|
|
|
|
`autoscale-canvasToBlob-${attachment.blob.size}`
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!tempBlob) {
|
|
|
|
|
|
|
|
throw new Error('Failed to get blob during canvasToBlob.');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
readAndResizedBlob = tempBlob;
|
|
|
|
quality = (quality * maxSize) / readAndResizedBlob.size;
|
|
|
|
quality = (quality * maxSize) / readAndResizedBlob.size;
|
|
|
|
|
|
|
|
|
|
|
|
if (quality > 1) {
|
|
|
|
if (quality > 1) {
|
|
|
@ -218,6 +254,8 @@ export async function autoScale<T extends { contentType: string; blob: Blob }>(
|
|
|
|
if (readAndResizedBlob.size > maxSize) {
|
|
|
|
if (readAndResizedBlob.size > maxSize) {
|
|
|
|
throw new Error('Cannot add this attachment even after trying to scale it down.');
|
|
|
|
throw new Error('Cannot add this attachment even after trying to scale it down.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.log.debug(`autoscale took ${Date.now() - start}ms `);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
contentType: attachment.contentType,
|
|
|
|
contentType: attachment.contentType,
|
|
|
|
blob: readAndResizedBlob,
|
|
|
|
blob: readAndResizedBlob,
|
|
|
|