37 lines
1.1 KiB
JavaScript
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);
|
|
})() |