From af8eccd688c47bebc7b24e63edd7a405a95e5df7 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Thu, 20 Jul 2023 11:54:20 -0700 Subject: [PATCH] avoid scaling gif for sealed topics --- net/web/src/context/useUploadContext.hook.js | 25 +++++++++++++++---- .../conversation/addTopic/useAddTopic.hook.js | 15 ++++++++--- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/net/web/src/context/useUploadContext.hook.js b/net/web/src/context/useUploadContext.hook.js index 91f0e4c0..fd1d9ab0 100644 --- a/net/web/src/context/useUploadContext.hook.js +++ b/net/web/src/context/useUploadContext.hook.js @@ -3,6 +3,9 @@ import axios from 'axios'; import Resizer from "react-image-file-resizer"; const ENCRYPTED_BLOCK_SIZE = (1024 * 1024); +const IMAGE_SCALE_SIZE = (128 * 1024 * 1024); +const GIF_TYPE = 'image/gif'; +const WEBP_TYPE = 'image/webp'; export function useUploadContext() { @@ -151,11 +154,23 @@ export function useUploadContext() { } function getImageThumb(data) { - return new Promise(resolve => { - Resizer.imageFileResizer(data, 192, 192, 'JPEG', 50, 0, - uri => { - resolve(uri); - }, 'base64', 128, 128 ); + return new Promise((resolve, reject) => { + if ((data.type === GIF_TYPE || data.type === WEBP_TYPE) && data.size < IMAGE_SCALE_SIZE) { + const reader = new FileReader(); + reader.readAsDataURL(data); + reader.onload = function () { + resolve(reader.result); + }; + reader.onerror = function (error) { + reject(); + }; + } + else { + Resizer.imageFileResizer(data, 192, 192, 'JPEG', 50, 0, + uri => { + resolve(uri); + }, 'base64', 128, 128 ); + } }); } diff --git a/net/web/src/session/conversation/addTopic/useAddTopic.hook.js b/net/web/src/session/conversation/addTopic/useAddTopic.hook.js index b5c63553..98255b17 100644 --- a/net/web/src/session/conversation/addTopic/useAddTopic.hook.js +++ b/net/web/src/session/conversation/addTopic/useAddTopic.hook.js @@ -104,10 +104,17 @@ export function useAddTopic(contentKey) { const actions = { addImage: async (image) => { - const scaled = await getResizedImage(image); - const asset = await setUrl(scaled); - asset.image = image; - addAsset(asset); + if (image.type === 'image/gif' || image.type === 'image/webp') { + const asset = await setUrl(image); + asset.image = image; + addAsset(asset); + } + else { + const scaled = await getResizedImage(image); + const asset = await setUrl(scaled); + asset.image = image; + addAsset(asset); + } }, addVideo: async (video) => { const asset = await setUrl(video);