2023-01-13 05:40:33 +01:00
|
|
|
|
import * as fs from "node:fs";
|
|
|
|
|
import * as crypto from "node:crypto";
|
|
|
|
|
import { join } from "node:path";
|
|
|
|
|
import * as stream from "node:stream";
|
|
|
|
|
import * as util from "node:util";
|
|
|
|
|
import { FSWatcher } from "chokidar";
|
|
|
|
|
import { fileTypeFromFile } from "file-type";
|
2023-05-20 04:26:13 +02:00
|
|
|
|
import probeImageSize from "probe-image-size";
|
2023-01-13 05:40:33 +01:00
|
|
|
|
import FFmpeg from "fluent-ffmpeg";
|
|
|
|
|
import isSvg from "is-svg";
|
|
|
|
|
import { type predictionType } from "nsfwjs";
|
|
|
|
|
import sharp from "sharp";
|
2023-07-14 04:00:26 +02:00
|
|
|
|
import * as blurhash from "blurhash-as";
|
2023-01-13 05:40:33 +01:00
|
|
|
|
import { detectSensitive } from "@/services/detect-sensitive.js";
|
|
|
|
|
import { createTempDir } from "./create-temp.js";
|
2020-01-12 08:40:58 +01:00
|
|
|
|
|
2020-04-11 11:28:40 +02:00
|
|
|
|
const pipeline = util.promisify(stream.pipeline);
|
2023-07-14 04:00:26 +02:00
|
|
|
|
blurhash.init();
|
2020-04-11 11:28:40 +02:00
|
|
|
|
|
2020-01-12 08:40:58 +01:00
|
|
|
|
export type FileInfo = {
|
|
|
|
|
size: number;
|
|
|
|
|
md5: string;
|
|
|
|
|
type: {
|
|
|
|
|
mime: string;
|
|
|
|
|
ext: string | null;
|
|
|
|
|
};
|
|
|
|
|
width?: number;
|
|
|
|
|
height?: number;
|
2021-12-03 03:19:28 +01:00
|
|
|
|
orientation?: number;
|
2020-07-18 17:24:07 +02:00
|
|
|
|
blurhash?: string;
|
2022-07-07 14:06:37 +02:00
|
|
|
|
sensitive: boolean;
|
|
|
|
|
porn: boolean;
|
2020-01-12 08:40:58 +01:00
|
|
|
|
warnings: string[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const TYPE_OCTET_STREAM = {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
mime: "application/octet-stream",
|
2021-12-09 15:58:30 +01:00
|
|
|
|
ext: null,
|
2020-01-12 08:40:58 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const TYPE_SVG = {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
mime: "image/svg+xml",
|
|
|
|
|
ext: "svg",
|
2020-01-12 08:40:58 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get file information
|
|
|
|
|
*/
|
2023-01-13 05:40:33 +01:00
|
|
|
|
export async function getFileInfo(
|
|
|
|
|
path: string,
|
|
|
|
|
opts: {
|
|
|
|
|
skipSensitiveDetection: boolean;
|
|
|
|
|
sensitiveThreshold?: number;
|
|
|
|
|
sensitiveThresholdForPorn?: number;
|
|
|
|
|
enableSensitiveMediaDetectionForVideos?: boolean;
|
|
|
|
|
},
|
|
|
|
|
): Promise<FileInfo> {
|
2020-01-12 08:40:58 +01:00
|
|
|
|
const warnings = [] as string[];
|
|
|
|
|
|
|
|
|
|
const size = await getFileSize(path);
|
|
|
|
|
const md5 = await calcHash(path);
|
|
|
|
|
|
|
|
|
|
let type = await detectType(path);
|
|
|
|
|
|
|
|
|
|
// image dimensions
|
|
|
|
|
let width: number | undefined;
|
|
|
|
|
let height: number | undefined;
|
2021-12-03 03:19:28 +01:00
|
|
|
|
let orientation: number | undefined;
|
2020-01-12 08:40:58 +01:00
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
|
if (
|
|
|
|
|
[
|
|
|
|
|
"image/jpeg",
|
|
|
|
|
"image/gif",
|
|
|
|
|
"image/png",
|
|
|
|
|
"image/apng",
|
|
|
|
|
"image/webp",
|
|
|
|
|
"image/bmp",
|
|
|
|
|
"image/tiff",
|
|
|
|
|
"image/svg+xml",
|
|
|
|
|
"image/vnd.adobe.photoshop",
|
|
|
|
|
"image/avif",
|
|
|
|
|
].includes(type.mime)
|
|
|
|
|
) {
|
|
|
|
|
const imageSize = await detectImageSize(path).catch((e) => {
|
2020-01-12 08:40:58 +01:00
|
|
|
|
warnings.push(`detectImageSize failed: ${e}`);
|
|
|
|
|
return undefined;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// うまく判定できない画像は octet-stream にする
|
|
|
|
|
if (!imageSize) {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
warnings.push("cannot detect image dimensions");
|
2020-01-12 08:40:58 +01:00
|
|
|
|
type = TYPE_OCTET_STREAM;
|
2023-01-13 05:40:33 +01:00
|
|
|
|
} else if (imageSize.wUnits === "px") {
|
2020-01-12 08:40:58 +01:00
|
|
|
|
width = imageSize.width;
|
|
|
|
|
height = imageSize.height;
|
2021-12-03 03:19:28 +01:00
|
|
|
|
orientation = imageSize.orientation;
|
2020-01-12 08:40:58 +01:00
|
|
|
|
|
|
|
|
|
// 制限を超えている画像は octet-stream にする
|
|
|
|
|
if (imageSize.width > 16383 || imageSize.height > 16383) {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
warnings.push("image dimensions exceeds limits");
|
2020-01-12 08:40:58 +01:00
|
|
|
|
type = TYPE_OCTET_STREAM;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
warnings.push(`unsupported unit type: ${imageSize.wUnits}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-18 17:24:07 +02:00
|
|
|
|
let blurhash: string | undefined;
|
2020-01-12 08:40:58 +01:00
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
|
if (
|
|
|
|
|
[
|
|
|
|
|
"image/jpeg",
|
|
|
|
|
"image/gif",
|
|
|
|
|
"image/png",
|
|
|
|
|
"image/apng",
|
|
|
|
|
"image/webp",
|
|
|
|
|
"image/svg+xml",
|
|
|
|
|
"image/avif",
|
|
|
|
|
].includes(type.mime)
|
|
|
|
|
) {
|
|
|
|
|
blurhash = await getBlurhash(path).catch((e) => {
|
2020-07-18 17:24:07 +02:00
|
|
|
|
warnings.push(`getBlurhash failed: ${e}`);
|
2020-01-12 08:40:58 +01:00
|
|
|
|
return undefined;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-07 14:06:37 +02:00
|
|
|
|
let sensitive = false;
|
|
|
|
|
let porn = false;
|
|
|
|
|
|
|
|
|
|
if (!opts.skipSensitiveDetection) {
|
2022-07-19 10:09:21 +02:00
|
|
|
|
await detectSensitivity(
|
2022-07-07 14:06:37 +02:00
|
|
|
|
path,
|
|
|
|
|
type.mime,
|
|
|
|
|
opts.sensitiveThreshold ?? 0.5,
|
|
|
|
|
opts.sensitiveThresholdForPorn ?? 0.75,
|
|
|
|
|
opts.enableSensitiveMediaDetectionForVideos ?? false,
|
2023-01-13 05:40:33 +01:00
|
|
|
|
).then(
|
|
|
|
|
(value) => {
|
|
|
|
|
[sensitive, porn] = value;
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
warnings.push(`detectSensitivity failed: ${error}`);
|
|
|
|
|
},
|
|
|
|
|
);
|
2022-07-07 14:06:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-12 08:40:58 +01:00
|
|
|
|
return {
|
|
|
|
|
size,
|
|
|
|
|
md5,
|
|
|
|
|
type,
|
|
|
|
|
width,
|
|
|
|
|
height,
|
2021-12-03 03:19:28 +01:00
|
|
|
|
orientation,
|
2020-07-18 17:24:07 +02:00
|
|
|
|
blurhash,
|
2022-07-07 14:06:37 +02:00
|
|
|
|
sensitive,
|
|
|
|
|
porn,
|
2020-01-12 08:40:58 +01:00
|
|
|
|
warnings,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
|
async function detectSensitivity(
|
|
|
|
|
source: string,
|
|
|
|
|
mime: string,
|
|
|
|
|
sensitiveThreshold: number,
|
|
|
|
|
sensitiveThresholdForPorn: number,
|
|
|
|
|
analyzeVideo: boolean,
|
|
|
|
|
): Promise<[sensitive: boolean, porn: boolean]> {
|
2022-07-07 14:06:37 +02:00
|
|
|
|
let sensitive = false;
|
|
|
|
|
let porn = false;
|
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
|
function judgePrediction(
|
|
|
|
|
result: readonly predictionType[],
|
|
|
|
|
): [sensitive: boolean, porn: boolean] {
|
2022-07-07 14:06:37 +02:00
|
|
|
|
let sensitive = false;
|
|
|
|
|
let porn = false;
|
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
|
if (
|
|
|
|
|
(result.find((x) => x.className === "Sexy")?.probability ?? 0) >
|
|
|
|
|
sensitiveThreshold
|
|
|
|
|
)
|
|
|
|
|
sensitive = true;
|
|
|
|
|
if (
|
|
|
|
|
(result.find((x) => x.className === "Hentai")?.probability ?? 0) >
|
|
|
|
|
sensitiveThreshold
|
|
|
|
|
)
|
|
|
|
|
sensitive = true;
|
|
|
|
|
if (
|
|
|
|
|
(result.find((x) => x.className === "Porn")?.probability ?? 0) >
|
|
|
|
|
sensitiveThreshold
|
|
|
|
|
)
|
|
|
|
|
sensitive = true;
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
(result.find((x) => x.className === "Porn")?.probability ?? 0) >
|
|
|
|
|
sensitiveThresholdForPorn
|
|
|
|
|
)
|
|
|
|
|
porn = true;
|
2022-07-07 14:06:37 +02:00
|
|
|
|
|
|
|
|
|
return [sensitive, porn];
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
|
if (["image/jpeg", "image/png", "image/webp"].includes(mime)) {
|
2022-07-07 14:06:37 +02:00
|
|
|
|
const result = await detectSensitive(source);
|
|
|
|
|
if (result) {
|
|
|
|
|
[sensitive, porn] = judgePrediction(result);
|
|
|
|
|
}
|
2023-01-13 05:40:33 +01:00
|
|
|
|
} else if (
|
|
|
|
|
analyzeVideo &&
|
|
|
|
|
(mime === "image/apng" || mime.startsWith("video/"))
|
|
|
|
|
) {
|
2022-07-07 14:06:37 +02:00
|
|
|
|
const [outDir, disposeOutDir] = await createTempDir();
|
|
|
|
|
try {
|
|
|
|
|
const command = FFmpeg()
|
|
|
|
|
.input(source)
|
|
|
|
|
.inputOptions([
|
2023-01-13 05:40:33 +01:00
|
|
|
|
"-skip_frame",
|
|
|
|
|
"nokey", // 可能ならキーフレームのみを取得してほしいとする(そうなるとは限らない)
|
|
|
|
|
"-lowres",
|
|
|
|
|
"3", // 元の画質でデコードする必要はないので 1/8 画質でデコードしてもよいとする(そうなるとは限らない)
|
2022-07-07 14:06:37 +02:00
|
|
|
|
])
|
|
|
|
|
.noAudio()
|
|
|
|
|
.videoFilters([
|
|
|
|
|
{
|
2023-01-13 05:40:33 +01:00
|
|
|
|
filter: "select", // フレームのフィルタリング
|
2022-07-07 14:06:37 +02:00
|
|
|
|
options: {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
e: "eq(pict_type,PICT_TYPE_I)", // I-Frame のみをフィルタする(VP9 とかはデコードしてみないとわからないっぽい)
|
2022-07-07 14:06:37 +02:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-01-13 05:40:33 +01:00
|
|
|
|
filter: "blackframe", // 暗いフレームの検出
|
2022-07-07 14:06:37 +02:00
|
|
|
|
options: {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
amount: "0", // 暗さに関わらず全てのフレームで測定値を取る
|
2022-07-07 14:06:37 +02:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-01-13 05:40:33 +01:00
|
|
|
|
filter: "metadata",
|
2022-07-07 14:06:37 +02:00
|
|
|
|
options: {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
mode: "select", // フレーム選択モード
|
|
|
|
|
key: "lavfi.blackframe.pblack", // フレームにおける暗部の百分率(前のフィルタからのメタデータを参照する)
|
|
|
|
|
value: "50",
|
|
|
|
|
function: "less", // 50% 未満のフレームを選択する(50% 以上暗部があるフレームだと誤検知を招くかもしれないので)
|
2022-07-07 14:06:37 +02:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-01-13 05:40:33 +01:00
|
|
|
|
filter: "scale",
|
2022-07-07 14:06:37 +02:00
|
|
|
|
options: {
|
|
|
|
|
w: 299,
|
|
|
|
|
h: 299,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
])
|
2023-01-13 05:40:33 +01:00
|
|
|
|
.format("image2")
|
|
|
|
|
.output(join(outDir, "%d.png"))
|
|
|
|
|
.outputOptions(["-vsync", "0"]); // 可変フレームレートにすることで穴埋めをさせない
|
2022-07-07 14:06:37 +02:00
|
|
|
|
const results: ReturnType<typeof judgePrediction>[] = [];
|
|
|
|
|
let frameIndex = 0;
|
|
|
|
|
let targetIndex = 0;
|
|
|
|
|
let nextIndex = 1;
|
|
|
|
|
for await (const path of asyncIterateFrames(outDir, command)) {
|
|
|
|
|
try {
|
|
|
|
|
const index = frameIndex++;
|
|
|
|
|
if (index !== targetIndex) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
targetIndex = nextIndex;
|
|
|
|
|
nextIndex += index; // fibonacci sequence によってフレーム数制限を掛ける
|
|
|
|
|
const result = await detectSensitive(path);
|
|
|
|
|
if (result) {
|
|
|
|
|
results.push(judgePrediction(result));
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
fs.promises.unlink(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-13 05:40:33 +01:00
|
|
|
|
sensitive =
|
|
|
|
|
results.filter((x) => x[0]).length >=
|
|
|
|
|
Math.ceil(results.length * sensitiveThreshold);
|
|
|
|
|
porn =
|
|
|
|
|
results.filter((x) => x[1]).length >=
|
|
|
|
|
Math.ceil(results.length * sensitiveThresholdForPorn);
|
2022-07-07 14:06:37 +02:00
|
|
|
|
} finally {
|
|
|
|
|
disposeOutDir();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [sensitive, porn];
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
|
async function* asyncIterateFrames(
|
|
|
|
|
cwd: string,
|
|
|
|
|
command: FFmpeg.FfmpegCommand,
|
|
|
|
|
): AsyncGenerator<string, void> {
|
2022-07-07 14:06:37 +02:00
|
|
|
|
const watcher = new FSWatcher({
|
|
|
|
|
cwd,
|
|
|
|
|
disableGlobbing: true,
|
|
|
|
|
});
|
|
|
|
|
let finished = false;
|
2023-01-13 05:40:33 +01:00
|
|
|
|
command.once("end", () => {
|
2022-07-07 14:06:37 +02:00
|
|
|
|
finished = true;
|
|
|
|
|
watcher.close();
|
|
|
|
|
});
|
|
|
|
|
command.run();
|
2023-01-13 05:40:33 +01:00
|
|
|
|
for (let i = 1; true; i++) {
|
2022-07-07 14:06:37 +02:00
|
|
|
|
const current = `${i}.png`;
|
|
|
|
|
const next = `${i + 1}.png`;
|
|
|
|
|
const framePath = join(cwd, current);
|
|
|
|
|
if (await exists(join(cwd, next))) {
|
|
|
|
|
yield framePath;
|
2023-01-13 05:40:33 +01:00
|
|
|
|
} else if (!finished) {
|
2022-07-07 14:06:37 +02:00
|
|
|
|
watcher.add(next);
|
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
watcher.on("add", function onAdd(path) {
|
|
|
|
|
if (path === next) {
|
|
|
|
|
// 次フレームの書き出しが始まっているなら、現在フレームの書き出しは終わっている
|
2022-07-07 14:06:37 +02:00
|
|
|
|
watcher.unwatch(current);
|
2023-01-13 05:40:33 +01:00
|
|
|
|
watcher.off("add", onAdd);
|
2022-07-07 14:06:37 +02:00
|
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-01-13 05:40:33 +01:00
|
|
|
|
command.once("end", resolve); // 全てのフレームを処理し終わったなら、最終フレームである現在フレームの書き出しは終わっている
|
|
|
|
|
command.once("error", reject);
|
2022-07-07 14:06:37 +02:00
|
|
|
|
});
|
|
|
|
|
yield framePath;
|
|
|
|
|
} else if (await exists(framePath)) {
|
|
|
|
|
yield framePath;
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function exists(path: string): Promise<boolean> {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
return fs.promises.access(path).then(
|
|
|
|
|
() => true,
|
|
|
|
|
() => false,
|
|
|
|
|
);
|
2022-07-07 14:06:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-12 08:40:58 +01:00
|
|
|
|
/**
|
|
|
|
|
* Detect MIME Type and extension
|
|
|
|
|
*/
|
2021-12-25 17:42:06 +01:00
|
|
|
|
export async function detectType(path: string): Promise<{
|
|
|
|
|
mime: string;
|
|
|
|
|
ext: string | null;
|
|
|
|
|
}> {
|
2020-01-12 08:40:58 +01:00
|
|
|
|
// Check 0 byte
|
|
|
|
|
const fileSize = await getFileSize(path);
|
|
|
|
|
if (fileSize === 0) {
|
|
|
|
|
return TYPE_OCTET_STREAM;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:51:34 +01:00
|
|
|
|
const type = await fileTypeFromFile(path);
|
2020-01-12 08:40:58 +01:00
|
|
|
|
|
|
|
|
|
if (type) {
|
|
|
|
|
// XMLはSVGかもしれない
|
2023-01-13 05:40:33 +01:00
|
|
|
|
if (type.mime === "application/xml" && (await checkSvg(path))) {
|
2020-01-12 08:40:58 +01:00
|
|
|
|
return TYPE_SVG;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
mime: type.mime,
|
2021-12-09 15:58:30 +01:00
|
|
|
|
ext: type.ext,
|
2020-01-12 08:40:58 +01:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 種類が不明でもSVGかもしれない
|
|
|
|
|
if (await checkSvg(path)) {
|
|
|
|
|
return TYPE_SVG;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// それでも種類が不明なら application/octet-stream にする
|
|
|
|
|
return TYPE_OCTET_STREAM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check the file is SVG or not
|
|
|
|
|
*/
|
|
|
|
|
export async function checkSvg(path: string) {
|
|
|
|
|
try {
|
|
|
|
|
const size = await getFileSize(path);
|
|
|
|
|
if (size > 1 * 1024 * 1024) return false;
|
|
|
|
|
return isSvg(fs.readFileSync(path));
|
|
|
|
|
} catch {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get file size
|
|
|
|
|
*/
|
|
|
|
|
export async function getFileSize(path: string): Promise<number> {
|
2020-04-11 11:28:40 +02:00
|
|
|
|
const getStat = util.promisify(fs.stat);
|
|
|
|
|
return (await getStat(path)).size;
|
2020-01-12 08:40:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate MD5 hash
|
|
|
|
|
*/
|
|
|
|
|
async function calcHash(path: string): Promise<string> {
|
2023-01-13 05:40:33 +01:00
|
|
|
|
const hash = crypto.createHash("md5").setEncoding("hex");
|
2020-04-11 11:28:40 +02:00
|
|
|
|
await pipeline(fs.createReadStream(path), hash);
|
|
|
|
|
return hash.read();
|
2020-01-12 08:40:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Detect dimensions of image
|
|
|
|
|
*/
|
|
|
|
|
async function detectImageSize(path: string): Promise<{
|
|
|
|
|
width: number;
|
|
|
|
|
height: number;
|
|
|
|
|
wUnits: string;
|
|
|
|
|
hUnits: string;
|
2021-12-03 03:19:28 +01:00
|
|
|
|
orientation?: number;
|
2020-01-12 08:40:58 +01:00
|
|
|
|
}> {
|
|
|
|
|
const readable = fs.createReadStream(path);
|
|
|
|
|
const imageSize = await probeImageSize(readable);
|
|
|
|
|
readable.destroy();
|
|
|
|
|
return imageSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate average color of image
|
|
|
|
|
*/
|
2020-07-18 17:24:07 +02:00
|
|
|
|
function getBlurhash(path: string): Promise<string> {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
sharp(path)
|
|
|
|
|
.raw()
|
|
|
|
|
.ensureAlpha()
|
2023-01-13 05:40:33 +01:00
|
|
|
|
.resize(64, 64, { fit: "inside" })
|
2020-07-18 17:24:07 +02:00
|
|
|
|
.toBuffer((err, buffer, { width, height }) => {
|
|
|
|
|
if (err) return reject(err);
|
2020-10-17 13:12:00 +02:00
|
|
|
|
|
|
|
|
|
let hash;
|
|
|
|
|
|
|
|
|
|
try {
|
2023-07-14 04:00:26 +02:00
|
|
|
|
hash = blurhash.encode(
|
|
|
|
|
new Uint8ClampedArray(buffer),
|
|
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
7,
|
|
|
|
|
7,
|
|
|
|
|
);
|
2020-10-17 13:12:00 +02:00
|
|
|
|
} catch (e) {
|
|
|
|
|
return reject(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resolve(hash);
|
2020-07-18 17:24:07 +02:00
|
|
|
|
});
|
|
|
|
|
});
|
2020-01-12 08:40:58 +01:00
|
|
|
|
}
|