Refactor getImages function to improve filename validation and enhance skipped items logging
All checks were successful
CI / build (push) Successful in 31s
All checks were successful
CI / build (push) Successful in 31s
This commit is contained in:
@@ -15,27 +15,50 @@ function getImages() {
|
|||||||
|
|
||||||
const lines = fs.readFileSync(CSV_FILE, "utf-8").split("\n");
|
const lines = fs.readFileSync(CSV_FILE, "utf-8").split("\n");
|
||||||
if (VERBOSE) console.log(`[pictshare-browse] CSV file line count: ${lines.length}`);
|
if (VERBOSE) console.log(`[pictshare-browse] CSV file line count: ${lines.length}`);
|
||||||
let skipped = 0;
|
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const csvDir = path.dirname(CSV_FILE);
|
const csvDir = path.dirname(CSV_FILE);
|
||||||
|
let skippedItems = [];
|
||||||
const images = lines
|
const images = lines
|
||||||
.filter(line => line.includes(";"))
|
.map(line => line.trim())
|
||||||
|
.filter(line => line && line.includes(";"))
|
||||||
.map(line => {
|
.map(line => {
|
||||||
const [hash, filename] = line.split(";");
|
const [hash, filenameRaw] = line.split(";");
|
||||||
const dirPath = path.join(csvDir, filename.trim());
|
const filename = filenameRaw && filenameRaw.trim();
|
||||||
if (!fs.existsSync(dirPath) || !fs.lstatSync(dirPath).isDirectory()) {
|
if (!filename) {
|
||||||
skipped++;
|
skippedItems.push({ reason: "empty filename", line });
|
||||||
if (VERBOSE) console.warn(`[pictshare-browse] Skipping: ${filename.trim()} (missing directory: ${dirPath})`);
|
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 null;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
url: `${IMAGE_BASE_URL.replace(/\/$/, "")}/${filename.trim()}`,
|
url: `${IMAGE_BASE_URL.replace(/\/$/, "")}/${filename}`,
|
||||||
name: filename.trim(),
|
name: filename,
|
||||||
hash: hash,
|
hash: hash,
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.filter(Boolean);
|
.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;
|
return images;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user