Migrate from sharp to jimp

This avoids native/binary problems like #131 and #115 at the expense of a bit of performance.

Fixes #131
Fixes #115
This commit is contained in:
Bruno Bernardino 2025-12-12 16:03:16 +00:00
parent a68bdba4b5
commit dcac5d8c44
No known key found for this signature in database
GPG key ID: D1B0A69ADD114ECE
4 changed files with 499 additions and 393 deletions

View file

@ -50,12 +50,12 @@
"@std/path": "jsr:@std/path@1.1.2",
"chart.js": "npm:chart.js@4.5.0",
"jimp": "npm:jimp@1.6.0",
"mrmime": "npm:mrmime@2.0.1",
"nodemailer": "npm:nodemailer@7.0.6",
"openid-client": "npm:openid-client@6.8.0",
"otpauth": "npm:otpauth@9.4.1",
"preact": "npm:preact@10.27.2",
"sharp": "npm:sharp@0.34.4",
"tailwindcss": "npm:tailwindcss@3.4.17",
"tailwindcss/": "npm:/tailwindcss@3.4.17/",
"tailwindcss/plugin": "npm:/tailwindcss@3.4.17/plugin.js",

870
deno.lock

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
services:
website:
image: ghcr.io/bewcloud/bewcloud:v3.0.2
image: ghcr.io/bewcloud/bewcloud:v3.1.0
restart: always
ports:
- 127.0.0.1:8000:8000

View file

@ -1,5 +1,5 @@
import { Handlers } from 'fresh/server.ts';
import sharp from 'sharp';
import { Jimp } from 'jimp';
import { FreshContextState } from '/lib/types.ts';
import { FileModel } from '/lib/models/files.ts';
@ -59,14 +59,16 @@ export const handler: Handlers<Data, FreshContextState> = {
}
try {
const image = sharp(fileResult.contents! as unknown as ArrayBuffer).resize({
width,
height,
fit: 'cover',
background: { r: 0, g: 0, b: 0, alpha: 0 },
}).png();
const image = await Jimp.read(
fileResult.contents!.buffer.slice(
fileResult.contents!.byteOffset,
fileResult.contents!.byteLength + fileResult.contents!.byteOffset,
) as ArrayBuffer,
);
const resizedImageContents = await image.toBuffer();
image.scaleToFit({ w: width, h: height });
const resizedImageContents = await image.getBuffer(fileResult.contentType! as 'image/jpeg' | 'image/png');
return new Response(Uint8Array.from(resizedImageContents), {
status: 200,