This commit is contained in:
Rizky 2024-04-01 16:41:59 +07:00
parent fe47485e28
commit ae6f89bfff
1 changed files with 6 additions and 11 deletions

View File

@ -49,17 +49,16 @@ export const _ = {
return new Response(original);
}
let file_name = dir(`${g.datadir}/files/upload/thumb/${w}/${rpath}`);
let path = `${w ? `w-${w}` : ""}${h ? `h-${h}` : ``}`;
let file_name = dir(
`${g.datadir}/files/upload/thumb/${path}/${rpath}.webp`
);
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 arg: any = { fit: "inside" };
@ -69,12 +68,8 @@ export const _ = {
if (h) {
arg.width = h;
}
let out = img.resize(arg);
if (format === "jpg" && !file_name.endsWith(".jpg")) {
file_name = file_name + ".jpg";
out = out.toFormat("jpg");
}
let out = img.resize(arg).webp({ quality: 75 });
out = out.webp();
await Bun.write(file_name, await out.toBuffer());
file = Bun.file(file_name);