SPB Git
2.2 KB · 29 lines typescript
1/** import-file queue: { jobId, workspaceId, path | attachmentId, kind, parentId?, filename? } → runImport with progress in settings `import.<jobId>`. */2import fs from "node:fs/promises";3import { eq } from "drizzle-orm";4import { db, schema } from "../../db/index";5import { absPath } from "../../lib/files/storage";6import { runImport, detectKind, setImportProgress, type ImportKind } from "../../lib/import/index";78export interface ImportJob { jobId?: string; workspaceId: string; path?: string; attachmentId?: string; kind?: ImportKind; parentId?: string | null; filename?: string }9export async function handleImport(d: ImportJob) {10  const jobId = d.jobId ?? `adhoc-${Date.now()}`;11  let buffer: Buffer; let filename = d.filename ?? "import";12  try {13    if (d.attachmentId) { const [a] = await db.select().from(schema.attachments).where(eq(schema.attachments.id, d.attachmentId)); if (!a) throw new Error("attachment not found"); buffer = await fs.readFile(absPath(a.storagePath)); filename = d.filename ?? a.filename; }14    else if (d.path) buffer = await fs.readFile(d.path);15    else throw new Error("path or attachmentId required");16    const kind = d.kind ?? detectKind(filename);17    await setImportProgress(jobId, { status: "running", progress: 0.02, kind, filename, workspaceId: d.workspaceId, message: `Importing ${filename}` });18    let lastWrite = 0;19    const result = await runImport({ workspaceId: d.workspaceId, kind, buffer, filename, parentId: d.parentId ?? null, progress: async (p) => { const now = Date.now(); if (now - lastWrite < 400 && (p.progress ?? 0) < 0.95) return; lastWrite = now; await setImportProgress(jobId, { status: "running", ...(p.progress !== undefined ? { progress: p.progress } : {}), ...(p.message ? { message: p.message } : {}) }); } });20    await setImportProgress(jobId, { status: "done", progress: 1, pageId: result.pageId, stats: result.stats, message: `Imported ${filename}` });21    if (d.path) await fs.unlink(d.path).catch(() => {});22    return { jobId, pageId: result.pageId, ...result.stats };23  } catch (e) {24    await setImportProgress(jobId, { status: "error", error: (e as Error).message ?? String(e) }).catch(() => {});25    if (d.path) await fs.unlink(d.path).catch(() => {});26    throw e;27  }28}29
Files · main
Menu

Theme