mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
import { encode } from "blurhash";
|
|
import sharp from "sharp";
|
|
import type { MediaPreprocessor } from "./media-preprocessor.ts";
|
|
|
|
export class BlurhashPreprocessor implements MediaPreprocessor {
|
|
public async process(
|
|
file: File,
|
|
): Promise<{ file: File; blurhash: string | null }> {
|
|
try {
|
|
const arrayBuffer = await file.arrayBuffer();
|
|
const metadata = await sharp(arrayBuffer).metadata();
|
|
|
|
const blurhash = await new Promise<string | null>((resolve) => {
|
|
sharp(arrayBuffer)
|
|
.raw()
|
|
.ensureAlpha()
|
|
.toBuffer((err, buffer) => {
|
|
if (err) {
|
|
resolve(null);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
resolve(
|
|
encode(
|
|
new Uint8ClampedArray(buffer),
|
|
metadata?.width ?? 0,
|
|
metadata?.height ?? 0,
|
|
4,
|
|
4,
|
|
) as string,
|
|
);
|
|
} catch {
|
|
resolve(null);
|
|
}
|
|
});
|
|
});
|
|
|
|
return { file, blurhash };
|
|
} catch {
|
|
return { file, blurhash: null };
|
|
}
|
|
}
|
|
}
|