From 43f134d9f9f48db33c140fe9f06461381ccce456 Mon Sep 17 00:00:00 2001 From: awsr <43862868+awsr@users.noreply.github.com> Date: Thu, 12 Feb 2026 13:38:04 -0800 Subject: [PATCH] Keep error handling within GalleryFile --- javascript/gallery.js | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/javascript/gallery.js b/javascript/gallery.js index efa7aa4c3..a818af54f 100644 --- a/javascript/gallery.js +++ b/javascript/gallery.js @@ -26,6 +26,17 @@ const el = { const SUPPORTED_EXTENSIONS = ['jpg', 'jpeg', 'png', 'webp', 'tiff', 'jp2', 'jxl', 'gif', 'mp4', 'mkv', 'avi', 'mjpeg', 'mpg', 'avr']; +async function getHash(str) { + let hex = ''; + const strBuf = new TextEncoder().encode(str); + let hashBuf; + if (crypto?.subtle?.digest) hashBuf = await crypto.subtle.digest('SHA-256', strBuf); + else hashBuf = hash(strBuf).buffer; // from sha256.js + const view = new DataView(hashBuf); + for (let i = 0; i < hashBuf.byteLength; i += 4) hex += (`00000000${view.getUint32(i).toString(16)}`).slice(-8); + return hex; +} + function getVisibleGalleryFiles() { if (!el.files) return []; return Array.from(el.files.children).filter((node) => node.name && node.offsetParent); @@ -449,7 +460,12 @@ class GalleryFile extends HTMLElement { } } - this.hash = await getHash(`${this.src}/${this.size}/${this.mtime}`); // eslint-disable-line + this.hash = await getHash(`${this.src}/${this.size}/${this.mtime}`) + .catch((err) => { + error('getHash:', err); + galleryProgressBar.error('File hash error'); + return null; + }); const cachedData = (this.hash && opts.browser_cache) ? await idbGet(this.hash).catch(() => undefined) : undefined; const img = document.createElement('img'); img.className = 'gallery-file'; @@ -659,23 +675,6 @@ async function addSeparators() { const gallerySendImage = (_images) => [currentImage]; // invoked by gradio button -async function getHash(str) { - try { - let hex = ''; - const strBuf = new TextEncoder().encode(str); - let hashBuf; - if (crypto?.subtle?.digest) hashBuf = await crypto.subtle.digest('SHA-256', strBuf); - else hashBuf = await hash(strBuf).buffer; // from sha256.js - const view = new DataView(hashBuf); - for (let i = 0; i < hashBuf.byteLength; i += 4) hex += (`00000000${view.getUint32(i).toString(16)}`).slice(-8); - return hex; - } catch (err) { - error('getHash:', err); - galleryProgressBar.error('File hash error'); - return undefined; - } -} - /** * Helper function to update status with sort mode * @param {...string|[string, string]} messages - Each can be either a string to use as-is, or an array of a string label and value