diff --git a/pkgs/api/_file.ts b/pkgs/api/_file.ts index c746e2f..0059731 100644 --- a/pkgs/api/_file.ts +++ b/pkgs/api/_file.ts @@ -24,7 +24,6 @@ export const _ = { let res = new Response("NOT FOUND", { status: 404 }); - if (Object.keys(req.query_parameters).length > 0) { await dirAsync(dir(`${g.datadir}/files`)); const base_dir = dir(`${g.datadir}/files/${rpath}`); @@ -51,7 +50,12 @@ export const _ = { if (rpath) { const base_dir = dir(`${g.datadir}/files/${rpath}`); if (await existsAsync(base_dir)) { - if ((await readdir(base_dir)).length === 0) { + const s = await stat(base_dir); + if (s.isDirectory()) { + if ((await readdir(base_dir)).length === 0) { + await removeAsync(base_dir); + } + } else { await removeAsync(base_dir); } } @@ -74,10 +78,11 @@ export const _ = { if (await existsAsync(dir(`${g.datadir}/files/${rpath}`))) { await renameAsync(dir(`${g.datadir}/files/${rpath}`), rename); } else { - const target = dir( - `${g.datadir}/files/${dirname(rpath)}/${rename}` - ); - await dirAsync(target); + console.log(dir(`${g.datadir}/files/${rpath}`)) + // const target = dir( + // `${g.datadir}/files/${dirname(rpath)}/${rename}` + // ); + // await dirAsync(target); } newname = `/${dirname(rpath)}/${rename}`; } @@ -108,7 +113,7 @@ export const _ = { headers: { "content-type": "application/json" }, }); } catch (e) { - return new Response(JSON.stringify([]), { + return new Response(JSON.stringify(null), { headers: { "content-type": "application/json" }, }); } diff --git a/pkgs/api/_img.ts b/pkgs/api/_img.ts index 8dbe13e..4b3e5ea 100644 --- a/pkgs/api/_img.ts +++ b/pkgs/api/_img.ts @@ -12,6 +12,7 @@ export const _ = { let res = new Response("NOT FOUND", { status: 404 }); const w = parseInt(req.query_parameters.w); + const format = req.query_parameters.f; let force = typeof req.query_parameters.force === "string"; let rpath = decodeURIComponent(req.params._); @@ -32,17 +33,28 @@ export const _ = { return new Response(original); } - const file_name = dir(`${g.datadir}/files/thumb/${w}/${rpath}`); + let file_name = dir(`${g.datadir}/files/upload/thumb/${w}/${rpath}`); let file = Bun.file(file_name); if (!(await file.exists())) { await dirAsync(dirname(file_name)); force = true; } + + if (format === "jpg" && !file_name.endsWith(".jpg")) { + force = true; + } + if (force) { const img = sharp(await original.arrayBuffer()); - const out = await img.resize({ width: w, fit: "inside" }).toBuffer(); - await Bun.write(file_name, out); + let out = img.resize({ width: w, fit: "inside" }); + + if (format === "jpg" && !file_name.endsWith(".jpg")) { + file_name = file_name + ".jpg"; + out = out.toFormat("jpg"); + } + + await Bun.write(file_name, await out.toBuffer()); file = Bun.file(file_name); } diff --git a/pkgs/api/_upload.ts b/pkgs/api/_upload.ts index df3a394..3bcea7b 100644 --- a/pkgs/api/_upload.ts +++ b/pkgs/api/_upload.ts @@ -1,5 +1,6 @@ import mp from "@surfy/multipart-parser"; -import { format, parse } from "path"; +import { dirAsync, existsAsync } from "fs-jetpack"; +import { format, parse, dirname } from "path"; import { apiContext } from "service-srv"; import { dir } from "utils/dir"; import { g } from "utils/global"; @@ -52,6 +53,10 @@ const saveFile = async ( pto.name = pto.name.replace(/[\W_]+/gi, "-"); to = format(pto); + if (await existsAsync(dirname(to))) { + dirAsync(dirname(to)); + } + while (await Bun.file(dir(`${g.datadir}/files/${to}`)).exists()) { const p = parse(to); const arr = p.name.split("-");