This repository has been archived on 2023-05-12. You can view files and clone it, but cannot push or open issues or pull requests.
media-host/scripts/clear-broken-files.js
2022-08-10 16:15:47 -04:00

37 lines
1.1 KiB
JavaScript

/*
This is not a server file, but a command to clear broken files on the database.
Usage (in the root of the folder): node ./scripts/clear-broken-files.js
*/
const config = require("../config.json")
const {MongoClient} = require("mongodb");
const client = new MongoClient(config["db-url"], {useUnifiedTopology: true});
const fs = require("fs");
if (!config["media-folder"].startsWith("/")) {
config["media-folder"] = `${__dirname.split("/scripts")[0]}/${config["media-folder"]}`;
}
(async function() {
console.log("Connecting to DB...");
await client.connect();
console.log("- Connected to DB.");
let db = await client.db("mediahost");
let files = await db.collection("uploads");
let f = await files.find({}).toArray();
let a = [];
for (let c in f) {
if (
!fs.existsSync(`${config["media-folder"]}${f[c].id}/`) ||
f[c].name == "" ||
f[c].name == null ||
f[c].name == undefined
) {
await files.findOneAndDelete({id: f[c].id});
a.push(f[c].id);
console.log(`-- Removed ${f[c].id}.`)
}
}
console.log(`Removed ${a.length} broken files from database.`)
process.exit(1);
})()