Fix incorrect contentType/extension for outgoing resized image attachments

* Use contentType from conversion when resizing outgoing images

* Update outgoing filename with proper extension after resize
This commit is contained in:
Scott Nonnenberg 2019-06-19 08:51:23 -07:00 committed by Ken Powers
parent dcf6a5f59c
commit 6c8bce7b9f

View file

@ -468,6 +468,7 @@
return; return;
} }
const targetContentType = 'image/jpeg';
const canvas = loadImage.scale(img, { const canvas = loadImage.scale(img, {
canvas: true, canvas: true,
maxWidth, maxWidth,
@ -480,7 +481,7 @@
do { do {
i -= 1; i -= 1;
blob = window.dataURLToBlobSync( blob = window.dataURLToBlobSync(
canvas.toDataURL('image/jpeg', quality) canvas.toDataURL(targetContentType, quality)
); );
quality = quality * maxSize / blob.size; quality = quality * maxSize / blob.size;
// NOTE: During testing with a large image, we observed the // NOTE: During testing with a large image, we observed the
@ -493,6 +494,8 @@
resolve({ resolve({
...attachment, ...attachment,
fileName: this.fixExtension(attachment.fileName, targetContentType),
contentType: targetContentType,
file: blob, file: blob,
}); });
}; };
@ -500,6 +503,39 @@
}); });
}, },
getFileName(fileName) {
if (!fileName) {
return '';
}
if (!fileName.includes('.')) {
return fileName;
}
return fileName
.split('.')
.slice(0, -1)
.join('.');
},
getType(contentType) {
if (!contentType) {
return '';
}
if (!contentType.includes('/')) {
return contentType;
}
return contentType.split('/')[1];
},
fixExtension(fileName, contentType) {
const extension = this.getType(contentType);
const name = this.getFileName(fileName);
return `${name}.${extension}`;
},
async getFile(attachment) { async getFile(attachment) {
if (!attachment) { if (!attachment) {
return Promise.resolve(); return Promise.resolve();