mirror of
https://github.com/versia-pub/server.git
synced 2026-04-28 05:09:16 +02:00
refactor: ♻️ Rewrite media management code
This commit is contained in:
parent
d09f74e58a
commit
faf829437d
34 changed files with 1195 additions and 904 deletions
45
classes/media/preprocessors/blurhash.ts
Normal file
45
classes/media/preprocessors/blurhash.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import { encode } from "blurhash";
|
||||
import sharp from "sharp";
|
||||
import type { MediaPreprocessor } from "./media-preprocessor";
|
||||
|
||||
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) => {
|
||||
(async () =>
|
||||
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 };
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue