f0ckv2/debug/clean.mjs
2022-01-06 04:09:19 +01:00

34 lines
905 B
JavaScript

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');
const ids = rows.map(r => r.id.toString() + ".webp");
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;
console.log(count, unused);