|
|
|
@ -1,16 +1,18 @@
|
|
|
|
|
import { StagedAttachmentType } from '../components/session/conversation/SessionCompositionBox';
|
|
|
|
|
import { SignalService } from '../protobuf';
|
|
|
|
|
import { Constants } from '../session';
|
|
|
|
|
|
|
|
|
|
export interface MaxScaleSize {
|
|
|
|
|
maxSize: number;
|
|
|
|
|
maxHeight: number;
|
|
|
|
|
maxWidth: number;
|
|
|
|
|
maxSize?: number;
|
|
|
|
|
maxHeight?: number;
|
|
|
|
|
maxWidth?: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function autoScale<
|
|
|
|
|
T extends { contentType: string; file: any; maxMeasurements?: MaxScaleSize }
|
|
|
|
|
>(attachment: T): Promise<T> {
|
|
|
|
|
const { contentType, file, maxMeasurements } = attachment;
|
|
|
|
|
export async function autoScale<T extends { contentType: string; file: any }>(
|
|
|
|
|
attachment: T,
|
|
|
|
|
maxMeasurements?: MaxScaleSize
|
|
|
|
|
): Promise<T> {
|
|
|
|
|
const { contentType, file } = attachment;
|
|
|
|
|
if (contentType.split('/')[0] !== 'image' || contentType === 'image/tiff') {
|
|
|
|
|
// nothing to do
|
|
|
|
|
return Promise.resolve(attachment);
|
|
|
|
@ -23,7 +25,9 @@ export async function autoScale<
|
|
|
|
|
img.onload = () => {
|
|
|
|
|
URL.revokeObjectURL(url);
|
|
|
|
|
|
|
|
|
|
const maxSize = maxMeasurements?.maxSize || 6000 * 1024;
|
|
|
|
|
const maxSize =
|
|
|
|
|
maxMeasurements?.maxSize ||
|
|
|
|
|
Constants.CONVERSATION.MAX_ATTACHMENT_FILESIZE_BYTES;
|
|
|
|
|
const maxHeight = maxMeasurements?.maxHeight || 4096;
|
|
|
|
|
const maxWidth = maxMeasurements?.maxWidth || 4096;
|
|
|
|
|
|
|
|
|
@ -36,7 +40,7 @@ export async function autoScale<
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const gifMaxSize = 25000 * 1024;
|
|
|
|
|
const gifMaxSize = Constants.CONVERSATION.MAX_ATTACHMENT_FILESIZE_BYTES;
|
|
|
|
|
if (file.type === 'image/gif' && file.size <= gifMaxSize) {
|
|
|
|
|
resolve(attachment);
|
|
|
|
|
return;
|
|
|
|
@ -62,11 +66,15 @@ export async function autoScale<
|
|
|
|
|
canvas.toDataURL('image/jpeg', quality)
|
|
|
|
|
);
|
|
|
|
|
quality = (quality * maxSize) / blob.size;
|
|
|
|
|
// Should we disallow the algo drop the quality too low?
|
|
|
|
|
// if (quality < 0.5) {
|
|
|
|
|
// quality = 0.5;
|
|
|
|
|
// }
|
|
|
|
|
// NOTE: During testing with a large image, we observed the
|
|
|
|
|
// `quality` value being > 1. Should we clamp it to [0.5, 1.0]?
|
|
|
|
|
// See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob#Syntax
|
|
|
|
|
if (quality < 0.5) {
|
|
|
|
|
quality = 0.5;
|
|
|
|
|
if (quality > 1) {
|
|
|
|
|
quality = 1;
|
|
|
|
|
}
|
|
|
|
|
} while (i > 0 && blob.size > maxSize);
|
|
|
|
|
|
|
|
|
@ -79,7 +87,10 @@ export async function autoScale<
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getFile(attachment: StagedAttachmentType) {
|
|
|
|
|
export async function getFile(
|
|
|
|
|
attachment: StagedAttachmentType,
|
|
|
|
|
maxMeasurements?: MaxScaleSize
|
|
|
|
|
) {
|
|
|
|
|
if (!attachment) {
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
}
|
|
|
|
@ -88,7 +99,7 @@ export async function getFile(attachment: StagedAttachmentType) {
|
|
|
|
|
? SignalService.AttachmentPointer.Flags.VOICE_MESSAGE
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
const scaled = await autoScale(attachment);
|
|
|
|
|
const scaled = await autoScale(attachment, maxMeasurements);
|
|
|
|
|
const fileRead = await readFile(scaled);
|
|
|
|
|
return {
|
|
|
|
|
...fileRead,
|
|
|
|
|