Files
aigen/server/utils/studio2/store.ts
T

15 lines
1.1 KiB
TypeScript

import { mkdirSync, readFileSync, writeFileSync, renameSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
export function studio2Root() { const dir = join(String(useRuntimeConfig().libraryDir), 'studio2'); mkdirSync(dir, { recursive: true }); return dir; }
export function saveRecord(record: any) { const path = join(studio2Root(), `${record.id}.json`); writeFileSync(path + '.tmp', JSON.stringify(record)); renameSync(path + '.tmp', path); }
export function readRecord(owner: string, id: string) { if (!/^[\w-]+$/.test(id))
throw createError({ statusCode: 400, statusMessage: 'Invalid job ID' }); const r = JSON.parse(readFileSync(join(studio2Root(), `${id}.json`), 'utf8')); if (r.owner !== owner)
throw createError({ statusCode: 404, statusMessage: 'Job not found' }); return r; }
export function records(owner?: string) { return readdirSync(studio2Root()).filter(n => n.endsWith('.json')).flatMap(n => { try {
const r = JSON.parse(readFileSync(join(studio2Root(), n), 'utf8'));
return !owner || r.owner === owner ? [r] : [];
}
catch {
return [];
} }).sort((a, b) => b.queuedAt - a.queuedAt); }