f0ckv2/debug/clean.mjs

34 lines
905 B
JavaScript
Raw Normal View History

2022-01-02 14:15:54 +00:00
import sql from "../src/inc/sql.mjs";
import { promises as fs } from "fs";
const opts = {
b: "public/b",
t: "public/t",
tmp: "tmp"
};
const count = {
b: 0, t: 0, tmp: 0
};
const rows = await sql('items').select('id', 'dest');
2022-01-06 03:09:19 +00:00
const ids = rows.map(r => r.id.toString() + ".webp");
2022-01-02 14:15:54 +00:00
const dests = rows.map(r => r.dest);
const files = {
b: (await fs.readdir(opts.b)).filter(f => f !== '.empty'),
t: await fs.readdir(opts.t)
};
const unused = {
b: files.b.filter(f => !dests.includes(f)),
t: files.t.filter(f => !ids.includes(f))
}
count.b = (await Promise.all(unused.b.map(f => fs.rm(`${opts.b}/${f}`)))).length;
count.t = (await Promise.all(unused.t.map(f => fs.rm(`${opts.t}/${f}`)))).length;
// clear tmp
const tmp = (await fs.readdir(opts.tmp)).filter(f => f !== '.empty');
count.tmp = (await Promise.all(tmp.map(f => fs.rm(`${opts.tmp}/${f}`)))).length;
2022-01-06 03:09:19 +00:00
console.log(count, unused);