Refactor getImages function to improve filename validation and enhance skipped items logging
All checks were successful
CI / build (push) Successful in 31s

This commit is contained in:
2025-07-23 19:51:00 +00:00
parent b2d69dc969
commit 2fc8bde6eb

View File

@@ -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;
}