syncova-folio/server/seed.js
Jerrit Fritzsche 55610ad454
All checks were successful
Docker-Image veröffentlichen / publish (push) Successful in 52s
Amtsbroschüre Leezen als einziges Beispiel bündeln
2026-11-26 04:04:24 +01:00

83 lines
3.5 KiB
JavaScript

import fs from 'node:fs/promises';
import { draftSchema, validatePublication } from './model.js';
import { releasePath, renderPublication } from './publisher.js';
import { audit, transaction, uploadPath } from './storage.js';
const slug = 'amt-leezen';
const bundle = new URL('./demo/amt-leezen/', import.meta.url);
function remapBlockMedia(block, mediaIds) {
if (block.type === 'gallery') {
return { ...block, mediaIds: block.mediaIds.map((id) => mediaIds.get(id)) };
}
if (block.type === 'image' || block.type === 'video') {
return {
...block,
mediaId: mediaIds.get(block.mediaId),
...(block.posterMediaId && { posterMediaId: mediaIds.get(block.posterMediaId) }),
};
}
return block;
}
export async function seed() {
if (process.env.SAMPLE_DATA !== 'true') return;
const paths = [];
try {
await transaction(async (db) => {
const flag = await db.query(
"INSERT INTO settings (key) VALUES ('sample-amt-leezen-v1') ON CONFLICT DO NOTHING RETURNING key",
);
if (!flag.rowCount) return;
const existing = await db.query('SELECT id FROM magazines WHERE slug=$1', [slug]);
if (existing.rowCount) return;
const edition = JSON.parse(await fs.readFile(new URL('edition.json', bundle), 'utf8'));
const mediaIds = new Map(edition.media.map((file) => [file.id, crypto.randomUUID()]));
const draft = draftSchema.parse({
...edition.draft,
cover: edition.draft.cover ? mediaIds.get(edition.draft.cover) : '',
pages: edition.draft.pages.map((page) => ({
...page, blocks: page.blocks.map((block) => remapBlockMedia(block, mediaIds)),
})),
});
const id = crypto.randomUUID();
const inserted = await db.query(
'INSERT INTO magazines (id,slug,draft) VALUES ($1,$2,$3) ON CONFLICT (slug) DO NOTHING RETURNING id',
[id, slug, draft],
);
if (!inserted.rowCount) return;
for (const source of edition.media) {
const file = { ...source, id: mediaIds.get(source.id) };
const path = uploadPath(file);
paths.push(path);
await fs.copyFile(new URL(`media/${source.id}.${source.extension}`, bundle), path);
await db.query('INSERT INTO media (id,name,mime,extension,bytes) VALUES ($1,$2,$3,$4,$5)',
[file.id, file.name, file.mime, file.extension, file.bytes]);
}
const files = await validatePublication(db, draft);
const release = crypto.randomUUID();
const { pages, html } = await renderPublication(draft, slug, release);
const htmlPath = releasePath(release, 'html');
const pdfPath = releasePath(release, 'pdf');
paths.push(htmlPath, pdfPath);
await fs.writeFile(htmlPath, html);
await fs.copyFile(new URL('amt-leezen.pdf', bundle), pdfPath);
const pdf = await fs.stat(pdfPath);
await db.query("INSERT INTO versions (magazine_id,revision,draft,reason) VALUES ($1,1,$2,'Beispielbroschüre angelegt')", [id, draft]);
await db.query(`INSERT INTO releases (id,magazine_id,revision,draft,publish_at,state,pages,media_ids,bytes,published_at)
VALUES ($1,$2,1,$3,now(),'ready',$4,$5,$6,now())`,
[release, id, draft, JSON.stringify(pages), files.map((file) => file.id), Buffer.byteLength(html) + pdf.size]);
await db.query('UPDATE magazines SET current_release=$1 WHERE id=$2', [release, id]);
await audit(db, 'created', '„Amt Leezen“ als bearbeitbare Beispielbroschüre angelegt');
});
} catch (error) {
await Promise.allSettled(paths.map((path) => fs.rm(path, { force: true })));
throw error;
}
}