about summary refs log tree commit diff
path: root/app/javascript/flavours
diff options
context:
space:
mode:
authorSasha Sorokin <dafri.nochiterov8@gmail.com>2020-07-10 03:32:36 +0700
committerThibaut Girka <thib@sitedethib.com>2020-07-10 17:08:31 +0200
commit66c0953c33d6d872e720eb8dedc5ab94bb3cd69a (patch)
treeb2e64f065128f5df5d910d7ea393ebc2575be54b /app/javascript/flavours
parent052027357321768bf39b2a62cb3f585b5d08b64e (diff)
[Glitch] Improve safety of Blurhash component
Port 3ef94c00444f2b72a6f68e0fd9cff1b3f783c555 to glitch-soc

Signed-off-by: Thibaut Girka <thib@sitedethib.com>
Diffstat (limited to 'app/javascript/flavours')
-rw-r--r--app/javascript/flavours/glitch/components/blurhash.js16
1 files changed, 10 insertions, 6 deletions
diff --git a/app/javascript/flavours/glitch/components/blurhash.js b/app/javascript/flavours/glitch/components/blurhash.js
index 172f8c2f5..2af5cfc56 100644
--- a/app/javascript/flavours/glitch/components/blurhash.js
+++ b/app/javascript/flavours/glitch/components/blurhash.js
@@ -6,7 +6,7 @@ import PropTypes from 'prop-types';
 
 /**
  * @typedef BlurhashPropsBase
- * @property {string} hash Hash to render
+ * @property {string?} hash Hash to render
  * @property {number} width
  * Width of the blurred region in pixels. Defaults to 32
  * @property {number} [height]
@@ -37,13 +37,17 @@ function Blurhash({
     const { current: canvas } = canvasRef;
     canvas.width = canvas.width; // resets canvas
 
-    if (dummy) return;
+    if (dummy || !hash) return;
 
-    const pixels = decode(hash, width, height);
-    const ctx = canvas.getContext('2d');
-    const imageData = new ImageData(pixels, width, height);
+    try {
+      const pixels = decode(hash, width, height);
+      const ctx = canvas.getContext('2d');
+      const imageData = new ImageData(pixels, width, height);
 
-    ctx.putImageData(imageData, 0, 0);
+      ctx.putImageData(imageData, 0, 0);
+    } catch (err) {
+      console.error('Blurhash decoding failure', { err, hash });
+    }
   }, [dummy, hash, width, height]);
 
   return (