From aac8fc99d6a06e9e3b4c0689c1fff3d379dd0672 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Sat, 28 Oct 2023 12:24:50 -0400 Subject: [PATCH] Cleanup webp import code a bit. --- web/scripts/pnginfo.js | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/web/scripts/pnginfo.js b/web/scripts/pnginfo.js index 4dc3a032c3c..491caed79f5 100644 --- a/web/scripts/pnginfo.js +++ b/web/scripts/pnginfo.js @@ -108,29 +108,25 @@ export function getWebpMetadata(file) { return new Promise((r) => { const reader = new FileReader(); reader.onload = (event) => { - // Get the PNG data as a Uint8Array - const pngData = new Uint8Array(event.target.result); - const dataView = new DataView(pngData.buffer); + const webp = new Uint8Array(event.target.result); + const dataView = new DataView(webp.buffer); - // Check that the PNG signature is present + // Check that the WEBP signature is present if (dataView.getUint32(0) !== 0x52494646 || dataView.getUint32(8) !== 0x57454250) { console.error("Not a valid WEBP file"); r(); return; } - // Start searching for chunks after the PNG signature + // Start searching for chunks after the WEBP signature let offset = 12; let txt_chunks = {}; - // Loop through the chunks in the PNG file - while (offset < pngData.length) { - // Get the length of the chunk - const length = dataView.getUint32(offset + 4, true); - // Get the chunk type - const type = String.fromCharCode(...pngData.slice(offset, offset + 4)); - if (type === "EXIF") { - // Get the keyword - let data = parseExifData(pngData.slice(offset + 8, offset + 8 + length)); + // Loop through the chunks in the WEBP file + while (offset < webp.length) { + const chunk_length = dataView.getUint32(offset + 4, true); + const chunk_type = String.fromCharCode(...webp.slice(offset, offset + 4)); + if (chunk_type === "EXIF") { + let data = parseExifData(webp.slice(offset + 8, offset + 8 + chunk_length)); for (var key in data) { var value = data[key]; let index = value.indexOf(':'); @@ -138,7 +134,7 @@ export function getWebpMetadata(file) { } } - offset += 8 + length; + offset += 8 + chunk_length; } r(txt_chunks);