mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2025-12-20 10:19:08 +00:00
Add Attachment type
Defines various functions on attachments, e.g. normalization (auto-orient JPEGs, etc.)
This commit is contained in:
68
js/modules/types/attachment.js
Normal file
68
js/modules/types/attachment.js
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
/* jshint ignore:start */
|
||||||
|
|
||||||
|
const {autoOrientImage} = require('../auto_orient_image');
|
||||||
|
|
||||||
|
// // Fields
|
||||||
|
// {
|
||||||
|
// "id": "7494241695219027913",
|
||||||
|
// "contentType": "image/jpeg",
|
||||||
|
// "key": {},
|
||||||
|
// "size": 352727,
|
||||||
|
// "thumbnail": null,
|
||||||
|
// "digest": {},
|
||||||
|
// "fileName": "Landscape_6.jpg",
|
||||||
|
// "flags": null,
|
||||||
|
// "data": {}
|
||||||
|
// }
|
||||||
|
|
||||||
|
const isJPEG = mimeType =>
|
||||||
|
mimeType === 'image/jpeg' || mimeType === 'image/jpg';
|
||||||
|
|
||||||
|
// Data type conversion
|
||||||
|
const blobToArrayBuffer = blob => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const fileReader = new FileReader();
|
||||||
|
|
||||||
|
fileReader.onload = event =>
|
||||||
|
resolve(event.target.result);
|
||||||
|
|
||||||
|
fileReader.readAsArrayBuffer(blob);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const arrayBufferToBlob = (arrayBuffer, mimeType) =>
|
||||||
|
new Blob([arrayBuffer], {type: mimeType});
|
||||||
|
|
||||||
|
// Processing steps:
|
||||||
|
const withLastNormalizedDate = attachment => Object.assign(
|
||||||
|
{},
|
||||||
|
attachment,
|
||||||
|
{lastNormalized: new Date().toISOString()},
|
||||||
|
);
|
||||||
|
|
||||||
|
const autoOrientJPEGs = async attachment => {
|
||||||
|
if (!isJPEG(attachment.contentType)) {
|
||||||
|
return attachment;
|
||||||
|
}
|
||||||
|
|
||||||
|
const dataBlob = arrayBufferToBlob(attachment.data, attachment.contentType);
|
||||||
|
const newDataBlob = dataURLtoBlob(await autoOrientImage(dataBlob));
|
||||||
|
const newDataArrayBuffer = await blobToArrayBuffer(newDataBlob);
|
||||||
|
const newAttachment = Object.assign({}, attachment, {
|
||||||
|
data: newDataArrayBuffer,
|
||||||
|
size: newDataArrayBuffer.byteLength,
|
||||||
|
lastNormalized: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
|
||||||
|
// `digest` is no longer valid for auto-oriented image data, so we discard it:
|
||||||
|
delete newAttachment.digest;
|
||||||
|
|
||||||
|
return newAttachment;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Public API
|
||||||
|
|
||||||
|
// Attachment -> Promise<Attachment>
|
||||||
|
exports.normalize = async attachment =>
|
||||||
|
withLastNormalizedDate(await autoOrientJPEGs(attachment));
|
||||||
|
|
||||||
Reference in New Issue
Block a user