From 37be3181560709b09fdf3be48d33bcaa3cc52c08 Mon Sep 17 00:00:00 2001 From: Ryan Hamilton Date: Wed, 23 Jul 2025 19:41:11 +0000 Subject: [PATCH] Refactor getImages function to enhance verbose logging and improve folder existence checks --- src/getImages.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/getImages.js b/src/getImages.js index 543e0c6..3a0f254 100644 --- a/src/getImages.js +++ b/src/getImages.js @@ -1,21 +1,30 @@ const fs = require("fs"); +const VERBOSE = process.env.VERBOSE === "1" || process.env.VERBOSE === "true"; + const CSV_FILE = process.env.CSV_FILE || "./uploads/sha1.csv"; const IMAGE_BASE_URL = process.env.IMAGE_BASE_URL || "https://placehold.co/"; -function getImages() { - console.log(`[pictshare-browse] Loading CSV file from: ${CSV_FILE}`); - if (!fs.existsSync(CSV_FILE)) return []; - return fs - .readFileSync(CSV_FILE, "utf-8") - .split("\n") +function getImages() { + if (VERBOSE) console.log(`[pictshare-browse] Loading CSV file from: ${CSV_FILE}`); + if (!fs.existsSync(CSV_FILE)) { + if (VERBOSE) console.warn(`[pictshare-browse] CSV file does not exist: ${CSV_FILE}`); + return []; + } + + 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 images = lines .filter(line => line.includes(";")) .map(line => { const [hash, filename] = line.split(";"); const folderPath = `./uploads/${filename.trim()}`; if (!fs.existsSync(folderPath) || !fs.lstatSync(folderPath).isDirectory()) { - return null; // Skip if folder does not exist + skipped++; + if (VERBOSE) console.warn(`[pictshare-browse] Skipping: ${filename.trim()} (missing folder: ${folderPath})`); + return null; } return { url: `${IMAGE_BASE_URL.replace(/\/$/, "")}/${filename.trim()}`, @@ -23,7 +32,9 @@ function getImages() { hash: hash, }; }) - .filter(Boolean); // Remove nulls from skipped items + .filter(Boolean); + if (VERBOSE) console.log(`[pictshare-browse] Images loaded: ${images.length}, Skipped: ${skipped}`); + return images; } module.exports = getImages;