From 2fc8bde6eb10dbcbd607d77ba30761a2028d1410 Mon Sep 17 00:00:00 2001 From: Ryan Hamilton Date: Wed, 23 Jul 2025 19:51:00 +0000 Subject: [PATCH] Refactor getImages function to improve filename validation and enhance skipped items logging --- src/getImages.js | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/src/getImages.js b/src/getImages.js index def0ee8..4e0b7eb 100644 --- a/src/getImages.js +++ b/src/getImages.js @@ -15,27 +15,50 @@ function getImages() { const lines = fs.readFileSync(CSV_FILE, "utf-8").split("\n"); if (VERBOSE) console.log(`[pictshare-browse] CSV file line count: ${lines.length}`); - let skipped = 0; const path = require("path"); const csvDir = path.dirname(CSV_FILE); + let skippedItems = []; const images = lines - .filter(line => line.includes(";")) + .map(line => line.trim()) + .filter(line => line && line.includes(";")) .map(line => { - const [hash, filename] = line.split(";"); - const dirPath = path.join(csvDir, filename.trim()); - if (!fs.existsSync(dirPath) || !fs.lstatSync(dirPath).isDirectory()) { - skipped++; - if (VERBOSE) console.warn(`[pictshare-browse] Skipping: ${filename.trim()} (missing directory: ${dirPath})`); + const [hash, filenameRaw] = line.split(";"); + const filename = filenameRaw && filenameRaw.trim(); + if (!filename) { + skippedItems.push({ reason: "empty filename", line }); + return null; + } + const dirPath = path.join(csvDir, filename); + try { + const stat = fs.statSync(dirPath); + if (!stat.isDirectory()) { + skippedItems.push({ reason: "not a directory", filename, dirPath }); + return null; + } + } catch (e) { + skippedItems.push({ reason: "missing directory", filename, dirPath }); return null; } return { - url: `${IMAGE_BASE_URL.replace(/\/$/, "")}/${filename.trim()}`, - name: filename.trim(), + url: `${IMAGE_BASE_URL.replace(/\/$/, "")}/${filename}`, + name: filename, hash: hash, }; }) .filter(Boolean); - if (VERBOSE) console.log(`[pictshare-browse] Images loaded: ${images.length}, Skipped: ${skipped}`); + if (VERBOSE) { + console.log(`[pictshare-browse] Images loaded: ${images.length}, Skipped: ${skippedItems.length}`); + if (skippedItems.length > 0) { + console.warn(`[pictshare-browse] Skipped items:`); + skippedItems.forEach(item => { + if (item.line) { + console.warn(` [line] ${item.reason}: ${item.line}`); + } else { + console.warn(` [${item.filename}] ${item.reason}: ${item.dirPath}`); + } + }); + } + } return images; }