Files
pictshare-browse/src/getImages.js
2025-07-23 19:41:11 +00:00

41 lines
1.4 KiB
JavaScript

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() {
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()) {
skipped++;
if (VERBOSE) console.warn(`[pictshare-browse] Skipping: ${filename.trim()} (missing folder: ${folderPath})`);
return null;
}
return {
url: `${IMAGE_BASE_URL.replace(/\/$/, "")}/${filename.trim()}`,
name: filename.trim(),
hash: hash,
};
})
.filter(Boolean);
if (VERBOSE) console.log(`[pictshare-browse] Images loaded: ${images.length}, Skipped: ${skipped}`);
return images;
}
module.exports = getImages;