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 });
if (Object.keys(req.query_parameters).length > 0) {
await dirAsync(dir(`${g.datadir}/files`));
const base_dir = dir(`${g.datadir}/files/${rpath}`);
@ -51,9 +50,14 @@ export const _ = {
if (rpath) {
const base_dir = dir(`${g.datadir}/files/${rpath}`);
if (await existsAsync(base_dir)) {
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" },
});
}

View File

@ -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);
}

View File

@ -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("-");