List files copied into the uploads folder by hand in the media library
The library only knew files uploaded through the admin. Listing it now registers any image in the uploads folder that has no media row, under its own name and unprocessed, at most every 30 seconds. Non-images, dotfiles and temporary names are skipped. The delete check also counts records that name a file by its bare filename, and a hand-copied name is escaped in its URL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
dad3803ccc
commit
8ec34ea185
3 changed files with 144 additions and 11 deletions
|
|
@ -29,10 +29,19 @@
|
|||
|
||||
Names are random, so two uploads of "logo.png" can't collide
|
||||
and a filename says nothing about what's in it.
|
||||
|
||||
── Files copied in by hand ──
|
||||
A file put straight into UPLOADS_DIR has no media row, so the
|
||||
library wouldn't know it. registerLooseFiles() gives each image
|
||||
it finds one, under the file's own name, unprocessed, uploaded by
|
||||
nobody. The Media routes call it when the library is listed. Only
|
||||
images are registered (by their bytes, as for an upload); anything
|
||||
else, dotfiles and the temporary names store() writes through
|
||||
are left alone.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { mkdir, rename, unlink, writeFile } from "node:fs/promises";
|
||||
import { mkdir, readFile, readdir, rename, stat, unlink, writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
import sharp from "sharp";
|
||||
|
|
@ -182,3 +191,82 @@ export async function remove(filename) {
|
|||
if (err.code !== "ENOENT") throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/* ── Files copied in by hand ─────────────────────────────────── */
|
||||
|
||||
/* What a file already on disk is: the fields a media row needs, or
|
||||
null when it isn't an image this library takes. */
|
||||
async function describe(filename) {
|
||||
const full = path.join(UPLOADS_DIR, filename);
|
||||
const info = await stat(full);
|
||||
if (!info.isFile() || info.size === 0 || info.size > MAX_UPLOAD_BYTES) return null;
|
||||
|
||||
const buf = await readFile(full);
|
||||
const kind = sniff(buf);
|
||||
if (!kind) return null;
|
||||
|
||||
let width = null;
|
||||
let height = null;
|
||||
if (kind !== "svg") {
|
||||
try {
|
||||
const meta = await sharp(buf).metadata();
|
||||
width = meta.width ?? null;
|
||||
height = meta.pageHeight ?? meta.height ?? null;
|
||||
} catch {
|
||||
return null; // looks like an image, won't open as one
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
filename,
|
||||
content_type: TYPES[kind].mime,
|
||||
bytes: info.size,
|
||||
width,
|
||||
height,
|
||||
processed: 0,
|
||||
};
|
||||
}
|
||||
|
||||
/* Gives every image in UPLOADS_DIR without a media row one. Returns
|
||||
how many it added. Safe to run often: it only reads the folder
|
||||
and the table, and inserts nothing twice (filename is UNIQUE). */
|
||||
export async function registerLooseFiles(db) {
|
||||
let names;
|
||||
try {
|
||||
names = await readdir(UPLOADS_DIR);
|
||||
} catch (err) {
|
||||
if (err.code === "ENOENT") return 0;
|
||||
throw err;
|
||||
}
|
||||
|
||||
const known = new Set(db.prepare(`SELECT filename FROM media`).all().map((row) => row.filename));
|
||||
const insert = db.prepare(
|
||||
`INSERT OR IGNORE INTO media
|
||||
(filename, original_name, content_type, bytes, width, height, processed, uploaded_by)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, NULL)`,
|
||||
);
|
||||
|
||||
let added = 0;
|
||||
for (const name of names) {
|
||||
if (known.has(name) || name.startsWith(".") || path.basename(name) !== name) continue;
|
||||
let row;
|
||||
try {
|
||||
row = await describe(name);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (!row) continue;
|
||||
const result = insert.run(
|
||||
row.filename,
|
||||
row.filename,
|
||||
row.content_type,
|
||||
row.bytes,
|
||||
row.width,
|
||||
row.height,
|
||||
row.processed,
|
||||
);
|
||||
added += result.changes;
|
||||
}
|
||||
if (added) console.log(`media: registered ${added} file(s) found in ${UPLOADS_DIR}`);
|
||||
return added;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue