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); 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); 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 arg: any = { fit: "inside" }; const arg: any = { fit: "inside" };
@ -69,12 +68,8 @@ export const _ = {
if (h) { if (h) {
arg.width = h; arg.width = h;
} }
let out = img.resize(arg); let out = img.resize(arg).webp({ quality: 75 });
out = out.webp();
if (format === "jpg" && !file_name.endsWith(".jpg")) {
file_name = file_name + ".jpg";
out = out.toFormat("jpg");
}
await Bun.write(file_name, await out.toBuffer()); await Bun.write(file_name, await out.toBuffer());
file = Bun.file(file_name); file = Bun.file(file_name);