Files
SkyArtShop/backend/cleanup-orphaned-files.js
Local Server 017c6376fc updateweb
2025-12-24 00:13:23 -06:00

35 lines
967 B
JavaScript

const { pool } = require("./config/database");
const fs = require("fs");
const path = require("path");
async function cleanOrphanedFiles() {
try {
const result = await pool.query("SELECT id, filename FROM uploads");
console.log("Files in database:", result.rows.length);
const uploadDir = path.join(__dirname, "..", "website", "uploads");
for (const file of result.rows) {
const filePath = path.join(uploadDir, file.filename);
const exists = fs.existsSync(filePath);
console.log(
`ID ${file.id}: ${file.filename} - ${exists ? "EXISTS" : "MISSING"}`
);
if (!exists) {
console.log(` Deleting orphaned record ID ${file.id}`);
await pool.query("DELETE FROM uploads WHERE id = $1", [file.id]);
}
}
console.log("\nCleanup complete!");
await pool.end();
} catch (err) {
console.error("Error:", err);
await pool.end();
process.exit(1);
}
}
cleanOrphanedFiles();