From 49d18ad3592438200fe6adc1a6a10702f5a8cb59 Mon Sep 17 00:00:00 2001 From: Rizky Date: Mon, 5 Aug 2024 14:25:57 +0700 Subject: [PATCH] fix empty bundle --- pkgs/api/_zip.ts | 74 ++++++++++++++++++++++++++++++--------- pkgs/empty_bundle.sqlite | Bin 0 -> 12288 bytes 2 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 pkgs/empty_bundle.sqlite diff --git a/pkgs/api/_zip.ts b/pkgs/api/_zip.ts index 0917658..b886776 100644 --- a/pkgs/api/_zip.ts +++ b/pkgs/api/_zip.ts @@ -1,5 +1,7 @@ import { $ } from "bun"; -import { removeAsync } from "fs-jetpack"; +import Database from "bun:sqlite"; +import { copyAsync, removeAsync } from "fs-jetpack"; +import mime from "mime"; import { dir } from "utils/dir"; import { g } from "utils/global"; @@ -7,38 +9,76 @@ export const _ = { url: "/_zip", raw: true, async api() { - await removeAsync(dir(`${g.datadir}/bundle`)); + await removeAsync(dir(`${g.datadir}/bundle.sqlite`)); + await copyAsync( + dir(`pkgs/empty_bundle.sqlite`), + dir(`${g.datadir}/bundle.sqlite`) + ); + const db = new Database(dir(`${g.datadir}/bundle.sqlite`)); + + const add = ({ + path, + type, + content, + }: { + path: string; + type: string; + content: string | Buffer; + }) => { + const query = db.query( + "INSERT INTO files (path, type, content) VALUES ($path, $type, $content)" + ); + + const res = query.run({ + $path: path.substring(`${g.datadir}/bundle`.length), + $type: type, + $content: content, + }); + console.log(res); + }; for (const [directory, files] of Object.entries(g.deploy.content || {})) { if (directory !== "code" && directory !== "site") { for (const comp of Object.values(files) as any) { let filepath = `${g.datadir}/bundle/${directory}/${comp.id}.json`; - await Bun.write(filepath, JSON.stringify(comp), { - createPath: true, + + add({ + path: filepath, + type: mime.getType(filepath) || "text/plain", + content: JSON.stringify(comp), }); } } else if (directory === "site") { - await Bun.write( - `${g.datadir}/bundle/${directory}.json`, - JSON.stringify(files), - { - createPath: true, - } - ); + const filepath = `${g.datadir}/bundle/${directory}.json`; + add({ + path: filepath, + type: mime.getType(filepath) || "text/plain", + content: JSON.stringify(files), + }); } else { for (const [filename, content] of Object.entries(files)) { let filepath = `${g.datadir}/bundle/${directory}/${filename}`; if (content instanceof Buffer || typeof content === "string") { - await Bun.write(filepath, content, { createPath: true }); + add({ + path: filepath, + type: mime.getType(filepath) || "text/plain", + content, + }); } else { for (const [k, v] of Object.entries(content || {})) { filepath = `${g.datadir}/bundle/${directory}/${filename}/${k}`; if (v instanceof Buffer || typeof v === "string") { - await Bun.write(filepath, v, { createPath: true }); + add({ + path: filepath, + type: mime.getType(filepath) || "text/plain", + content: v, + }); } else { - await Bun.write(filepath, JSON.stringify(v), { - createPath: true, + add({ + path: filepath, + type: mime.getType(filepath) || "text/plain", + content: JSON.stringify(v), }); } } @@ -47,7 +87,7 @@ export const _ = { } } - await $`zip -r bundle.zip .`.quiet().cwd(`${g.datadir}/bundle`); - return new Response(Bun.file(`${g.datadir}/bundle/bundle.zip`)); + await $`zip bundle.zip bundle.sqlite`.quiet().cwd(`${g.datadir}`); + return new Response(Bun.file(`${g.datadir}/bundle.zip`)); }, }; diff --git a/pkgs/empty_bundle.sqlite b/pkgs/empty_bundle.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..a98823be6bc6fd8f98b6142202ea674f07ab710e GIT binary patch literal 12288 zcmeI#Jxjwt7zgl6Diws1ZULtUrv^l@(orxCK?{A+Gzw11*24)JKC@12XJ-q zJNOYCy+$hzcI(jLAGq9e&t2~2m+AUf?N|zmCfS%vI$>3&Y3z)MF{Z0hQf2il+dHd< zs%pHm7?rUoCD~ie