Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// If we got passed the doc itself, we can check whether the attachment exists.
(doc && !get(['_attachments', attachmentId, 'digest'])(doc))
) {
return undefined
}
let blobOrBuffer
try {
blobOrBuffer = await db.getAttachment(docId, attachmentId)
} catch (err) {
return undefined
}
const contentType = get(['_attachments', attachmentId, 'content_type'])(doc)
const isBrowser = typeof window !== 'undefined' && !window.process
const blob = isBrowser
? blobOrBuffer
: await arrayBufferToBlob(blobOrBuffer, contentType)
const base64 = await blobToBase64String(blob)
const dataUrl = `data:${blob.type};base64,${base64}`
return dataUrl
}
exports.autoOrientJPEG = async attachment => {
if (!MIME.isJPEG(attachment.contentType)) {
return attachment;
}
// If we haven't downloaded the attachment yet, we won't have the data
if (!attachment.data) {
return attachment;
}
const dataBlob = await arrayBufferToBlob(
attachment.data,
attachment.contentType
);
const newDataBlob = await dataURLToBlob(await autoOrientImage(dataBlob));
const newDataArrayBuffer = await blobToArrayBuffer(newDataBlob);
// IMPORTANT: We overwrite the existing `data` `ArrayBuffer` losing the original
// image data. Ideally, we’d preserve the original image data for users who want to
// retain it but due to reports of data loss, we don’t want to overburden IndexedDB
// by potentially doubling stored image data.
// See: https://github.com/signalapp/Signal-Desktop/issues/1589
const newAttachment = Object.assign({}, attachment, {
data: newDataArrayBuffer,
size: newDataArrayBuffer.byteLength,
});