SPB Git

Repository Public

import.ts 3.2 KB 57 lines typescript

1import { copyFile, mkdir, readdir, stat } from "node:fs/promises";2import path from "node:path";3import { randomUUID } from "node:crypto";4import "./_env";5import { sql } from "../src/lib/db";6import { env } from "../src/lib/env";7import { audit } from "../src/lib/audit";8import { safeFilename } from "../src/lib/text";9import { uploadTmpPath } from "../src/server/ingest";1011/**12 * Server-side import, for an administrator with shell access to the node:13 *   tsx scripts/import.ts <file or directory> [...]     (add --wait to follow the worker until everything is settled)14 * Files are COPIED into the import queue and go through exactly the same pipeline as a browser upload15 * (content sniffing, ffprobe, duplicate detection, cover extraction). The sources are left untouched.16 */17const AUDIO = /\.(flac|mp3|m4a|aac|wav|aiff?|ogg|opus|dsf|dff)$/i;1819async function collect(p: string, out: string[]) {20  const s = await stat(p);21  if (s.isDirectory()) { for (const f of (await readdir(p)).sort((a, b) => a.localeCompare(b, undefined, { numeric: true }))) if (!f.startsWith(".")) await collect(path.join(p, f), out); }22  else if (s.isFile() && AUDIO.test(p)) out.push(p);23}2425async function main() {26  const args = process.argv.slice(2);27  const wait = args.includes("--wait");28  const files: string[] = [];29  for (const a of args.filter((x) => !x.startsWith("--"))) await collect(path.resolve(a), files);30  if (files.length === 0) throw new Error("No audio file found. Usage: tsx scripts/import.ts <file or directory> [...] [--wait]");31  const [owner] = await sql<{ id: string }[]>`SELECT id FROM users WHERE is_owner`;32  if (!owner) throw new Error("No owner account: run scripts/bootstrap.ts first");33  await mkdir(path.dirname(uploadTmpPath(randomUUID())), { recursive: true, mode: 0o700 });34  const ids: string[] = [];35  for (const f of files) {36    const size = (await stat(f)).size;37    if (size > env.maxFileBytes) { console.log(`skipped (larger than UPLOAD_MAX_FILE_MB): ${f}`); continue; }38    const id = randomUUID();39    await copyFile(f, uploadTmpPath(id));40    await sql`INSERT INTO uploads (id, user_id, filename, size_bytes, received_bytes, status) VALUES (${id}, ${owner.id}, ${safeFilename(f)}, ${size}, ${size}, 'received')`;41    ids.push(id);42  }43  await audit({ userId: owner.id, actor: "admin", action: "import.cli", meta: { files: ids.length } });44  console.log(`${ids.length} file(s) queued for the worker`);45  if (wait) {46    for (;;) {47      const rows = await sql<{ status: string; n: number }[]>`SELECT status, count(*)::int AS n FROM uploads WHERE id = ANY(${ids}::uuid[]) GROUP BY status`;48      const pending = rows.filter((r) => ["received", "validating", "extracting"].includes(r.status)).reduce((s, r) => s + r.n, 0);49      if (pending === 0) { console.log(rows.map((r) => `${r.status}: ${r.n}`).join(", ")); break; }50      await new Promise((r) => setTimeout(r, 1000));51    }52    for (const r of await sql<{ filename: string; status: string; error_code: string | null }[]>`SELECT filename, status, error_code FROM uploads WHERE id = ANY(${ids}::uuid[]) AND status <> 'ready' ORDER BY filename`) console.log(`  ${r.status} ${r.error_code ?? ""} — ${r.filename}`);53  }54  await sql.end();55}56main().catch((e) => { console.error(e.message); process.exit(1); });57
Files main
Menu

Theme