Refactor getImages function to enhance verbose logging and improve folder existence checks
All checks were successful
CI / build (push) Successful in 32s
All checks were successful
CI / build (push) Successful in 32s
This commit is contained in:
@@ -1,21 +1,30 @@
|
|||||||
const fs = require("fs");
|
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 CSV_FILE = process.env.CSV_FILE || "./uploads/sha1.csv";
|
||||||
const IMAGE_BASE_URL = process.env.IMAGE_BASE_URL || "https://placehold.co/";
|
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
|
function getImages() {
|
||||||
.readFileSync(CSV_FILE, "utf-8")
|
if (VERBOSE) console.log(`[pictshare-browse] Loading CSV file from: ${CSV_FILE}`);
|
||||||
.split("\n")
|
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(";"))
|
.filter(line => line.includes(";"))
|
||||||
.map(line => {
|
.map(line => {
|
||||||
const [hash, filename] = line.split(";");
|
const [hash, filename] = line.split(";");
|
||||||
const folderPath = `./uploads/${filename.trim()}`;
|
const folderPath = `./uploads/${filename.trim()}`;
|
||||||
if (!fs.existsSync(folderPath) || !fs.lstatSync(folderPath).isDirectory()) {
|
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 {
|
return {
|
||||||
url: `${IMAGE_BASE_URL.replace(/\/$/, "")}/${filename.trim()}`,
|
url: `${IMAGE_BASE_URL.replace(/\/$/, "")}/${filename.trim()}`,
|
||||||
@@ -23,7 +32,9 @@ function getImages() {
|
|||||||
hash: hash,
|
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;
|
module.exports = getImages;
|
||||||
|
|||||||
Reference in New Issue
Block a user