This commit is contained in:
Rizky 2024-02-24 16:43:00 +07:00
parent fea458b827
commit f9137f22ce
3 changed files with 33 additions and 11 deletions

View File

@ -24,7 +24,6 @@ export const _ = {
let res = new Response("NOT FOUND", { status: 404 }); let res = new Response("NOT FOUND", { status: 404 });
if (Object.keys(req.query_parameters).length > 0) { if (Object.keys(req.query_parameters).length > 0) {
await dirAsync(dir(`${g.datadir}/files`)); await dirAsync(dir(`${g.datadir}/files`));
const base_dir = dir(`${g.datadir}/files/${rpath}`); const base_dir = dir(`${g.datadir}/files/${rpath}`);
@ -51,7 +50,12 @@ export const _ = {
if (rpath) { if (rpath) {
const base_dir = dir(`${g.datadir}/files/${rpath}`); const base_dir = dir(`${g.datadir}/files/${rpath}`);
if (await existsAsync(base_dir)) { 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); await removeAsync(base_dir);
} }
} }
@ -74,10 +78,11 @@ export const _ = {
if (await existsAsync(dir(`${g.datadir}/files/${rpath}`))) { if (await existsAsync(dir(`${g.datadir}/files/${rpath}`))) {
await renameAsync(dir(`${g.datadir}/files/${rpath}`), rename); await renameAsync(dir(`${g.datadir}/files/${rpath}`), rename);
} else { } else {
const target = dir( console.log(dir(`${g.datadir}/files/${rpath}`))
`${g.datadir}/files/${dirname(rpath)}/${rename}` // const target = dir(
); // `${g.datadir}/files/${dirname(rpath)}/${rename}`
await dirAsync(target); // );
// await dirAsync(target);
} }
newname = `/${dirname(rpath)}/${rename}`; newname = `/${dirname(rpath)}/${rename}`;
} }
@ -108,7 +113,7 @@ export const _ = {
headers: { "content-type": "application/json" }, headers: { "content-type": "application/json" },
}); });
} catch (e) { } catch (e) {
return new Response(JSON.stringify([]), { return new Response(JSON.stringify(null), {
headers: { "content-type": "application/json" }, headers: { "content-type": "application/json" },
}); });
} }

View File

@ -12,6 +12,7 @@ export const _ = {
let res = new Response("NOT FOUND", { status: 404 }); let res = new Response("NOT FOUND", { status: 404 });
const w = parseInt(req.query_parameters.w); const w = parseInt(req.query_parameters.w);
const format = req.query_parameters.f;
let force = typeof req.query_parameters.force === "string"; let force = typeof req.query_parameters.force === "string";
let rpath = decodeURIComponent(req.params._); let rpath = decodeURIComponent(req.params._);
@ -32,17 +33,28 @@ export const _ = {
return new Response(original); 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); let file = Bun.file(file_name);
if (!(await file.exists())) { if (!(await file.exists())) {
await dirAsync(dirname(file_name)); await dirAsync(dirname(file_name));
force = true; force = true;
} }
if (format === "jpg" && !file_name.endsWith(".jpg")) {
force = true;
}
if (force) { if (force) {
const img = sharp(await original.arrayBuffer()); const img = sharp(await original.arrayBuffer());
const out = await img.resize({ width: w, fit: "inside" }).toBuffer(); let out = img.resize({ width: w, fit: "inside" });
await Bun.write(file_name, out);
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); file = Bun.file(file_name);
} }

View File

@ -1,5 +1,6 @@
import mp from "@surfy/multipart-parser"; 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 { apiContext } from "service-srv";
import { dir } from "utils/dir"; import { dir } from "utils/dir";
import { g } from "utils/global"; import { g } from "utils/global";
@ -52,6 +53,10 @@ const saveFile = async (
pto.name = pto.name.replace(/[\W_]+/gi, "-"); pto.name = pto.name.replace(/[\W_]+/gi, "-");
to = format(pto); to = format(pto);
if (await existsAsync(dirname(to))) {
dirAsync(dirname(to));
}
while (await Bun.file(dir(`${g.datadir}/files/${to}`)).exists()) { while (await Bun.file(dir(`${g.datadir}/files/${to}`)).exists()) {
const p = parse(to); const p = parse(to);
const arr = p.name.split("-"); const arr = p.name.split("-");