From 6481256f5eeecbfaaec7984415c24c1d075ba50f Mon Sep 17 00:00:00 2001 From: Jerome Wu Date: Thu, 10 Sep 2020 16:05:38 +0800 Subject: [PATCH] Fix lint and test error --- src/worker/browser/loadImage.js | 28 +++++++++++++++++++++------- src/worker/node/loadImage.js | 5 ++--- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/worker/browser/loadImage.js b/src/worker/browser/loadImage.js index 8cdc050..b1cc14f 100644 --- a/src/worker/browser/loadImage.js +++ b/src/worker/browser/loadImage.js @@ -1,5 +1,5 @@ const resolveURL = require('resolve-url'); -const blueimp = require('blueimp-load-image'); +const blueimpLoadImage = require('blueimp-load-image'); /** * readFromBlobOrFile @@ -23,9 +23,16 @@ const readFromBlobOrFile = blob => ( const fixOrientationFromUrlOrBlobOrFile = blob => ( new Promise((resolve) => { - blueimp(blob, (img) => img.toBlob(resolve, 'image/jpeg'), {orientation: true}) + blueimpLoadImage( + blob, + img => img.toBlob(resolve), + { + orientation: true, + canvas: true, + }, + ); }) -) +); /** * loadImage @@ -46,9 +53,13 @@ const loadImage = async (image) => { data = atob(image.split(',')[1]) .split('') .map(c => c.charCodeAt(0)); + } else if (image.endsWith('.pbm')) { + const resp = await fetch(resolveURL(image)); + data = await resp.arrayBuffer(); } else { - image = await fixOrientationFromUrlOrBlobOrFile(resolveURL(image)); - data = await readFromBlobOrFile(image); + data = await readFromBlobOrFile( + await fixOrientationFromUrlOrBlobOrFile(resolveURL(image)), + ); } } else if (image instanceof HTMLElement) { if (image.tagName === 'IMG') { @@ -66,8 +77,11 @@ const loadImage = async (image) => { }); } } else if (image instanceof File || image instanceof Blob) { - image = await fixOrientationFromUrlOrBlobOrFile(image); - data = await readFromBlobOrFile(image); + let img = image; + if (!image.name.endsWith('.pbm')) { + img = await fixOrientationFromUrlOrBlobOrFile(img); + } + data = await readFromBlobOrFile(img); } return new Uint8Array(data); diff --git a/src/worker/node/loadImage.js b/src/worker/node/loadImage.js index af9fdc8..11eff72 100644 --- a/src/worker/node/loadImage.js +++ b/src/worker/node/loadImage.js @@ -33,9 +33,8 @@ module.exports = async (image) => { } try { - data = (await jo.rotate(data, {quality: 100})).buffer - } catch (_) { - } + data = (await jo.rotate(data, { quality: 100 })).buffer; + } catch (_) {} /* eslint-disable-line */ return new Uint8Array(data); };