30 lines
1.1 KiB
JavaScript
30 lines
1.1 KiB
JavaScript
// Generated, do not edit. sha256:c5d56ec66e3cd8cb38447fbd3bde3d13f334c82cd570f61ee957f449d17935a3
|
|||
|
|
import { mkdtemp, rm, readFile } from 'node:fs/promises';
|
||
|
|
import { tmpdir } from 'node:os';
|
||
|
|
import { join } from 'node:path';
|
||
|
|
import { run } from './db/conn.mjs';
|
||
|
|
|
||
|
|
function bestandsnaam() {
|
||
|
|
const d = new Date();
|
||
|
|
const p = n => String(n).padStart(2, '0');
|
||
|
|
return `assetapp-${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}-${p(d.getHours())}${p(d.getMinutes())}.sqlite`;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function exportDatabase(res) {
|
||
|
|
const dir = await mkdtemp(join(tmpdir(), 'assetapp-export-'));
|
||
|
|
const uit = join(dir, 'export.sqlite');
|
||
|
|
try {
|
||
|
|
run(`VACUUM INTO '${uit.replace(/'/g, "''")}'`, []);
|
||
|
|
const bytes = await readFile(uit);
|
||
|
|
res.writeHead(200, {
|
||
|
|
'Content-Type': 'application/vnd.sqlite3',
|
||
|
|
'Content-Disposition': `attachment; filename="${bestandsnaam()}"`,
|
||
|
|
'Content-Length': bytes.length,
|
||
|
|
'Cache-Control': 'no-store',
|
||
|
|
'X-Content-Type-Options': 'nosniff',
|
||
|
|
});
|
||
|
|
res.end(bytes);
|
||
|
|
} finally {
|
||
|
|
await rm(dir, { recursive: true, force: true });
|
||
|
|
}
|
||
|
|
}
|