2023-01-12 21:40:33 -07:00
|
|
|
import * as fs from "node:fs";
|
|
|
|
import type Koa from "koa";
|
|
|
|
import sharp from "sharp";
|
|
|
|
import type { IImage } from "@/services/drive/image-processor.js";
|
|
|
|
import { convertToWebp } from "@/services/drive/image-processor.js";
|
|
|
|
import { createTemp } from "@/misc/create-temp.js";
|
|
|
|
import { downloadUrl } from "@/misc/download-url.js";
|
|
|
|
import { detectType } from "@/misc/get-file-info.js";
|
|
|
|
import { StatusError } from "@/misc/fetch.js";
|
|
|
|
import { FILE_TYPE_BROWSERSAFE } from "@/const.js";
|
|
|
|
import { serverLogger } from "../index.js";
|
|
|
|
import { isMimeImage } from "@/misc/is-mime-image.js";
|
2019-02-04 11:01:36 -07:00
|
|
|
|
2019-11-24 01:09:32 -07:00
|
|
|
export async function proxyMedia(ctx: Koa.Context) {
|
2023-01-12 21:40:33 -07:00
|
|
|
const url = "url" in ctx.query ? ctx.query.url : `https://${ctx.params.url}`;
|
2019-02-04 11:01:36 -07:00
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
if (typeof url !== "string") {
|
2022-02-03 05:38:57 -07:00
|
|
|
ctx.status = 400;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-04 11:01:36 -07:00
|
|
|
// Create temp file
|
2019-03-20 13:50:44 -06:00
|
|
|
const [path, cleanup] = await createTemp();
|
2019-02-04 11:01:36 -07:00
|
|
|
|
|
|
|
try {
|
2019-03-20 13:50:44 -06:00
|
|
|
await downloadUrl(url, path);
|
2019-02-04 11:01:36 -07:00
|
|
|
|
2020-01-12 00:40:58 -07:00
|
|
|
const { mime, ext } = await detectType(path);
|
2023-01-12 21:40:33 -07:00
|
|
|
const isConvertibleImage = isMimeImage(mime, "sharp-convertible-image");
|
2019-02-04 11:01:36 -07:00
|
|
|
|
|
|
|
let image: IImage;
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
if ("static" in ctx.query && isConvertibleImage) {
|
2022-04-27 20:14:03 -06:00
|
|
|
image = await convertToWebp(path, 498, 280);
|
2023-01-12 21:40:33 -07:00
|
|
|
} else if ("preview" in ctx.query && isConvertibleImage) {
|
2022-04-27 20:14:03 -06:00
|
|
|
image = await convertToWebp(path, 200, 200);
|
2023-01-12 21:40:33 -07:00
|
|
|
} else if ("badge" in ctx.query) {
|
2022-06-19 09:33:46 -06:00
|
|
|
if (!isConvertibleImage) {
|
|
|
|
// 画像でないなら404でお茶を濁す
|
2023-01-12 21:40:33 -07:00
|
|
|
throw new StatusError("Unexpected mime", 404);
|
2022-06-19 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const mask = sharp(path)
|
|
|
|
.resize(96, 96, {
|
2023-01-12 21:40:33 -07:00
|
|
|
fit: "inside",
|
2022-06-19 09:33:46 -06:00
|
|
|
withoutEnlargement: false,
|
|
|
|
})
|
|
|
|
.greyscale()
|
|
|
|
.normalise()
|
|
|
|
.linear(1.75, -(128 * 1.75) + 128) // 1.75x contrast
|
2023-01-12 21:40:33 -07:00
|
|
|
.flatten({ background: "#000" })
|
|
|
|
.toColorspace("b-w");
|
2022-06-19 09:33:46 -06:00
|
|
|
|
|
|
|
const stats = await mask.clone().stats();
|
|
|
|
|
|
|
|
if (stats.entropy < 0.1) {
|
|
|
|
// エントロピーがあまりない場合は404にする
|
2023-01-12 21:40:33 -07:00
|
|
|
throw new StatusError("Skip to provide badge", 404);
|
2022-06-19 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const data = sharp({
|
2023-01-12 21:40:33 -07:00
|
|
|
create: {
|
|
|
|
width: 96,
|
|
|
|
height: 96,
|
|
|
|
channels: 4,
|
|
|
|
background: { r: 0, g: 0, b: 0, alpha: 0 },
|
|
|
|
},
|
2022-06-19 09:33:46 -06:00
|
|
|
})
|
2023-01-12 21:40:33 -07:00
|
|
|
.pipelineColorspace("b-w")
|
|
|
|
.boolean(await mask.png().toBuffer(), "eor");
|
2022-06-19 09:33:46 -06:00
|
|
|
|
|
|
|
image = {
|
|
|
|
data: await data.png().toBuffer(),
|
2023-01-12 21:40:33 -07:00
|
|
|
ext: "png",
|
|
|
|
type: "image/png",
|
2022-06-19 09:33:46 -06:00
|
|
|
};
|
2023-01-12 21:40:33 -07:00
|
|
|
} else if (mime === "image/svg+xml") {
|
2022-04-27 20:14:03 -06:00
|
|
|
image = await convertToWebp(path, 2048, 2048, 1);
|
2023-01-12 21:40:33 -07:00
|
|
|
} else if (
|
|
|
|
!(mime.startsWith("image/") &&FILE_TYPE_BROWSERSAFE.includes(mime))
|
|
|
|
) {
|
|
|
|
throw new StatusError("Rejected type", 403, "Rejected type");
|
2019-02-04 11:01:36 -07:00
|
|
|
} else {
|
|
|
|
image = {
|
|
|
|
data: fs.readFileSync(path),
|
|
|
|
ext,
|
2020-01-12 00:40:58 -07:00
|
|
|
type: mime,
|
2019-02-04 11:01:36 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
ctx.set("Content-Type", image.type);
|
|
|
|
ctx.set("Cache-Control", "max-age=31536000, immutable");
|
2019-02-04 11:01:36 -07:00
|
|
|
ctx.body = image.data;
|
|
|
|
} catch (e) {
|
2021-10-16 02:16:24 -06:00
|
|
|
serverLogger.error(`${e}`);
|
2019-02-05 08:20:00 -07:00
|
|
|
|
2022-06-19 09:33:46 -06:00
|
|
|
if (e instanceof StatusError && (e.statusCode === 302 || e.isClientError)) {
|
2021-09-03 06:00:44 -06:00
|
|
|
ctx.status = e.statusCode;
|
2019-02-05 08:20:00 -07:00
|
|
|
} else {
|
|
|
|
ctx.status = 500;
|
|
|
|
}
|
2019-02-04 11:01:36 -07:00
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
}
|