Remove autoOrientJPEG and consolidate downscaling logic
This commit is contained in:
parent
3eed6cb350
commit
09b5e6ef50
10 changed files with 105 additions and 144 deletions
|
@ -3,6 +3,7 @@
|
|||
|
||||
import { blobToArrayBuffer } from 'blob-util';
|
||||
|
||||
import * as log from '../logging/log';
|
||||
import { scaleImageToLevel } from './scaleImageToLevel';
|
||||
import { dropNull } from './dropNull';
|
||||
import type {
|
||||
|
@ -10,49 +11,23 @@ import type {
|
|||
UploadedAttachmentType,
|
||||
} from '../types/Attachment';
|
||||
import { canBeTranscoded } from '../types/Attachment';
|
||||
import type { LoggerType } from '../types/Logging';
|
||||
import * as MIME from '../types/MIME';
|
||||
import * as Errors from '../types/errors';
|
||||
import * as Bytes from '../Bytes';
|
||||
|
||||
// Upgrade steps NOTE: This step strips all EXIF metadata from JPEG images as part of
|
||||
// re-encoding the image:
|
||||
|
||||
// When sending an image:
|
||||
// 1. During composition, images are passed through handleImageAttachment. If needed, this
|
||||
// scales them down to high-quality (level 3).
|
||||
// 2. Draft images are then written to disk as a draft image (so there is a `path`)
|
||||
// 3. On send, the message schema is upgraded, triggering this function
|
||||
|
||||
export async function autoOrientJPEG(
|
||||
attachment: AttachmentType,
|
||||
{ logger }: { logger: LoggerType },
|
||||
{
|
||||
sendHQImages = false,
|
||||
isIncoming = false,
|
||||
}: {
|
||||
sendHQImages?: boolean;
|
||||
isIncoming?: boolean;
|
||||
} = {}
|
||||
): Promise<AttachmentType> {
|
||||
if (isIncoming && !MIME.isJPEG(attachment.contentType)) {
|
||||
return attachment;
|
||||
}
|
||||
|
||||
// All outgoing images go through handleImageAttachment before being sent and thus have
|
||||
// already been scaled to high-quality level, stripped of exif data, and saved. This
|
||||
// should be called just before message send to downscale the attachment further if
|
||||
// needed.
|
||||
export const downscaleOutgoingAttachment = async (
|
||||
attachment: AttachmentType
|
||||
): Promise<AttachmentType> => {
|
||||
if (!canBeTranscoded(attachment)) {
|
||||
return attachment;
|
||||
}
|
||||
// If we haven't downloaded the attachment yet, we won't have the data.
|
||||
// All images go through handleImageAttachment before being sent and thus have
|
||||
// already been scaled to level, oriented, stripped of exif data, and saved
|
||||
// in high quality format. If we want to send the image in HQ we can return
|
||||
// the attachment as-is. Otherwise we'll have to further scale it down.
|
||||
|
||||
let scaleTarget: string | Blob;
|
||||
const { data, path, size } = attachment;
|
||||
|
||||
if (sendHQImages) {
|
||||
return attachment;
|
||||
}
|
||||
let scaleTarget: string | Blob;
|
||||
if (data) {
|
||||
scaleTarget = new Blob([data], {
|
||||
type: attachment.contentType,
|
||||
|
@ -65,12 +40,12 @@ export async function autoOrientJPEG(
|
|||
}
|
||||
|
||||
try {
|
||||
const { blob: xcodedDataBlob } = await scaleImageToLevel(
|
||||
scaleTarget,
|
||||
attachment.contentType,
|
||||
const { blob: xcodedDataBlob } = await scaleImageToLevel({
|
||||
fileOrBlobOrURL: scaleTarget,
|
||||
contentType: attachment.contentType,
|
||||
size,
|
||||
isIncoming
|
||||
);
|
||||
highQuality: false,
|
||||
});
|
||||
const xcodedDataArrayBuffer = await blobToArrayBuffer(xcodedDataBlob);
|
||||
|
||||
// IMPORTANT: We overwrite the existing `data` `Uint8Array` losing the original
|
||||
|
@ -91,14 +66,14 @@ export async function autoOrientJPEG(
|
|||
return xcodedAttachment;
|
||||
} catch (error: unknown) {
|
||||
const errorString = Errors.toLogFormat(error);
|
||||
logger.error(
|
||||
'autoOrientJPEG: Failed to rotate/scale attachment',
|
||||
log.error(
|
||||
'downscaleOutgoingAttachment: Failed to scale attachment',
|
||||
errorString
|
||||
);
|
||||
|
||||
return attachment;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export type CdnFieldsType = Pick<
|
||||
AttachmentType,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue