From 5c39df09def8a9b3fbaf4319a60ef56fd76d0878 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 16 Sep 2023 20:14:24 -0500 Subject: [PATCH] Swap out exif-js for exifr --- app/soapbox/utils/resize-image.ts | 31 ++++++++++++++++--------------- package.json | 2 +- yarn.lock | 8 ++++---- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/app/soapbox/utils/resize-image.ts b/app/soapbox/utils/resize-image.ts index 62c2d839b..e4f9001ad 100644 --- a/app/soapbox/utils/resize-image.ts +++ b/app/soapbox/utils/resize-image.ts @@ -118,24 +118,25 @@ const loadImage = (inputFile: File) => new Promise((resolve, r }); /** Get the exif orientation for the image. */ -const getOrientation = (img: HTMLImageElement, type = 'image/png') => new Promise(resolve => { +async function getOrientation(img: HTMLImageElement, type = 'image/png'): Promise { if (!['image/jpeg', 'image/webp'].includes(type)) { - resolve(1); - return; + return 1; } - import('exif-js').then(({ default: EXIF }) => { - // @ts-ignore: The TypeScript definition is wrong. - EXIF.getData(img, () => { - const orientation = EXIF.getTag(img, 'Orientation'); - if (orientation !== 1) { - dropOrientationIfNeeded(orientation).then(resolve).catch(() => resolve(orientation)); - } else { - resolve(orientation); - } - }); - }).catch(() => {}); -}); + try { + const exifr = await import('exifr'); + const orientation = await exifr.orientation(img) ?? 1; + + if (orientation !== 1) { + return await dropOrientationIfNeeded(orientation); + } else { + return orientation; + } + } catch (error) { + console.error('Failed to get orientation:', error); + return 1; + } +} const processImage = ( img: HTMLImageElement, diff --git a/package.json b/package.json index 5ef9f2b9b..faab23994 100644 --- a/package.json +++ b/package.json @@ -105,7 +105,7 @@ "emoji-datasource": "14.0.0", "emoji-mart": "^5.5.2", "escape-html": "^1.0.3", - "exif-js": "^2.3.0", + "exifr": "^7.1.3", "graphemesplit": "^2.4.4", "http-link-header": "^1.0.2", "immer": "^9.0.19", diff --git a/yarn.lock b/yarn.lock index d401460cd..d417e038f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4778,10 +4778,10 @@ exenv@^1.2.2: resolved "https://registry.yarnpkg.com/exenv/-/exenv-1.2.2.tgz#2ae78e85d9894158670b03d47bec1f03bd91bb9d" integrity sha1-KueOhdmJQVhnCwPUe+wfA72Ru50= -exif-js@^2.3.0: - version "2.3.0" - resolved "https://registry.yarnpkg.com/exif-js/-/exif-js-2.3.0.tgz#9d10819bf571f873813e7640241255ab9ce1a814" - integrity sha1-nRCBm/Vx+HOBPnZAJBJVq5zhqBQ= +exifr@^7.1.3: + version "7.1.3" + resolved "https://registry.yarnpkg.com/exifr/-/exifr-7.1.3.tgz#f6218012c36dbb7d843222011b27f065fddbab6f" + integrity sha512-g/aje2noHivrRSLbAUtBPWFbxKdKhgj/xr1vATDdUXPOFYJlQ62Ft0oy+72V6XLIpDJfHs6gXLbBLAolqOXYRw== expand-tilde@^2.0.2: version "2.0.2"