2023-01-13 05:40:33 +01:00
|
|
|
import * as fs from "node:fs";
|
|
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import { dirname } from "node:path";
|
|
|
|
import type Koa from "koa";
|
|
|
|
import send from "koa-send";
|
|
|
|
import rename from "rename";
|
|
|
|
import { serverLogger } from "../index.js";
|
|
|
|
import { contentDisposition } from "@/misc/content-disposition.js";
|
|
|
|
import { DriveFiles } from "@/models/index.js";
|
|
|
|
import { InternalStorage } from "@/services/drive/internal-storage.js";
|
|
|
|
import { createTemp } from "@/misc/create-temp.js";
|
|
|
|
import { downloadUrl } from "@/misc/download-url.js";
|
|
|
|
import { detectType } from "@/misc/get-file-info.js";
|
2023-01-29 02:46:03 +01:00
|
|
|
import { convertToWebp } from "@/services/drive/image-processor.js";
|
2023-01-13 05:40:33 +01:00
|
|
|
import { GenerateVideoThumbnail } from "@/services/drive/generate-video-thumbnail.js";
|
|
|
|
import { StatusError } from "@/misc/fetch.js";
|
|
|
|
import { FILE_TYPE_BROWSERSAFE } from "@/const.js";
|
2021-08-19 11:33:41 +02:00
|
|
|
|
2022-02-27 03:07:39 +01:00
|
|
|
const _filename = fileURLToPath(import.meta.url);
|
2021-08-19 11:33:41 +02:00
|
|
|
const _dirname = dirname(_filename);
|
2018-05-03 13:03:14 +02:00
|
|
|
|
2021-08-19 11:33:41 +02:00
|
|
|
const assets = `${_dirname}/../../server/file/assets/`;
|
2018-05-04 10:59:51 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const commonReadableHandlerGenerator =
|
|
|
|
(ctx: Koa.Context) => (e: Error): void => {
|
|
|
|
serverLogger.error(e);
|
|
|
|
ctx.status = 500;
|
|
|
|
ctx.set("Cache-Control", "max-age=300");
|
|
|
|
};
|
2018-04-12 23:06:18 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export default async function (ctx: Koa.Context) {
|
2019-04-07 14:50:36 +02:00
|
|
|
const key = ctx.params.key;
|
2018-04-12 23:06:18 +02:00
|
|
|
|
|
|
|
// Fetch drive file
|
2023-01-13 05:40:33 +01:00
|
|
|
const file = await DriveFiles.createQueryBuilder("file")
|
|
|
|
.where("file.accessKey = :accessKey", { accessKey: key })
|
|
|
|
.orWhere("file.thumbnailAccessKey = :thumbnailAccessKey", {
|
|
|
|
thumbnailAccessKey: key,
|
|
|
|
})
|
|
|
|
.orWhere("file.webpublicAccessKey = :webpublicAccessKey", {
|
|
|
|
webpublicAccessKey: key,
|
|
|
|
})
|
2019-04-07 14:50:36 +02:00
|
|
|
.getOne();
|
2018-04-12 23:06:18 +02:00
|
|
|
|
|
|
|
if (file == null) {
|
|
|
|
ctx.status = 404;
|
2023-01-13 05:40:33 +01:00
|
|
|
ctx.set("Cache-Control", "max-age=86400");
|
|
|
|
await send(ctx as any, "/dummy.png", { root: assets });
|
2018-04-12 23:06:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-31 09:23:47 +01:00
|
|
|
const isThumbnail = file.thumbnailAccessKey === key;
|
|
|
|
const isWebpublic = file.webpublicAccessKey === key;
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (!file.storedInternal) {
|
2023-01-13 05:40:33 +01:00
|
|
|
if (file.isLink && file.uri) {
|
|
|
|
// 期限切れリモートファイル
|
2022-05-25 09:50:22 +02:00
|
|
|
const [path, cleanup] = await createTemp();
|
2019-12-31 09:23:47 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
await downloadUrl(file.uri, path);
|
|
|
|
|
2020-01-12 08:40:58 +01:00
|
|
|
const { mime, ext } = await detectType(path);
|
2019-12-31 09:23:47 +01:00
|
|
|
|
|
|
|
const convertFile = async () => {
|
|
|
|
if (isThumbnail) {
|
2023-01-13 05:40:33 +01:00
|
|
|
if (
|
|
|
|
[
|
|
|
|
"image/jpeg",
|
|
|
|
"image/webp",
|
|
|
|
"image/png",
|
|
|
|
"image/svg+xml",
|
|
|
|
"image/avif",
|
|
|
|
].includes(mime)
|
|
|
|
) {
|
2023-01-29 02:46:03 +01:00
|
|
|
return await convertToWebp(path, 996, 560);
|
2023-01-13 05:40:33 +01:00
|
|
|
} else if (mime.startsWith("video/")) {
|
2019-12-31 09:23:47 +01:00
|
|
|
return await GenerateVideoThumbnail(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 19:05:52 +01:00
|
|
|
if (isWebpublic) {
|
2023-01-13 05:40:33 +01:00
|
|
|
if (["image/svg+xml"].includes(mime)) {
|
2023-01-29 02:46:03 +01:00
|
|
|
return await convertToWebp(path, 2048, 2048, 100);
|
2022-01-19 19:05:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 09:23:47 +01:00
|
|
|
return {
|
|
|
|
data: fs.readFileSync(path),
|
|
|
|
ext,
|
2020-01-12 08:40:58 +01:00
|
|
|
type: mime,
|
2019-12-31 09:23:47 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const image = await convertFile();
|
|
|
|
ctx.body = image.data;
|
2023-01-13 05:40:33 +01:00
|
|
|
ctx.set(
|
|
|
|
"Content-Type",
|
|
|
|
FILE_TYPE_BROWSERSAFE.includes(image.type)
|
|
|
|
? image.type
|
|
|
|
: "application/octet-stream",
|
|
|
|
);
|
|
|
|
ctx.set("Cache-Control", "max-age=31536000, immutable");
|
2019-12-31 09:23:47 +01:00
|
|
|
} catch (e) {
|
2021-10-16 10:16:24 +02:00
|
|
|
serverLogger.error(`${e}`);
|
2019-12-31 09:23:47 +01:00
|
|
|
|
2021-10-16 10:16:24 +02:00
|
|
|
if (e instanceof StatusError && e.isClientError) {
|
2021-09-03 14:00:44 +02:00
|
|
|
ctx.status = e.statusCode;
|
2023-01-13 05:40:33 +01:00
|
|
|
ctx.set("Cache-Control", "max-age=86400");
|
2019-12-31 09:23:47 +01:00
|
|
|
} else {
|
|
|
|
ctx.status = 500;
|
2023-01-13 05:40:33 +01:00
|
|
|
ctx.set("Cache-Control", "max-age=300");
|
2019-12-31 09:23:47 +01:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:19:14 +02:00
|
|
|
ctx.status = 204;
|
2023-01-13 05:40:33 +01:00
|
|
|
ctx.set("Cache-Control", "max-age=86400");
|
2018-04-17 13:04:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-03 23:20:41 +01:00
|
|
|
if (isThumbnail || isWebpublic) {
|
2020-01-18 19:38:22 +01:00
|
|
|
const { mime, ext } = await detectType(InternalStorage.resolvePath(key));
|
2020-01-03 23:20:41 +01:00
|
|
|
const filename = rename(file.name, {
|
2023-01-13 05:40:33 +01:00
|
|
|
suffix: isThumbnail ? "-thumb" : "-web",
|
2021-12-09 15:58:30 +01:00
|
|
|
extname: ext ? `.${ext}` : undefined,
|
2020-01-03 23:20:41 +01:00
|
|
|
}).toString();
|
|
|
|
|
2019-12-19 17:39:59 +01:00
|
|
|
ctx.body = InternalStorage.read(key);
|
2023-01-13 05:40:33 +01:00
|
|
|
ctx.set(
|
|
|
|
"Content-Type",
|
|
|
|
FILE_TYPE_BROWSERSAFE.includes(mime) ? mime : "application/octet-stream",
|
|
|
|
);
|
|
|
|
ctx.set("Cache-Control", "max-age=31536000, immutable");
|
|
|
|
ctx.set("Content-Disposition", contentDisposition("inline", filename));
|
2018-05-03 13:03:14 +02:00
|
|
|
} else {
|
2019-04-12 18:43:22 +02:00
|
|
|
const readable = InternalStorage.read(file.accessKey!);
|
2023-01-13 05:40:33 +01:00
|
|
|
readable.on("error", commonReadableHandlerGenerator(ctx));
|
2019-04-07 14:50:36 +02:00
|
|
|
ctx.body = readable;
|
2023-01-13 05:40:33 +01:00
|
|
|
ctx.set(
|
|
|
|
"Content-Type",
|
|
|
|
FILE_TYPE_BROWSERSAFE.includes(file.type)
|
|
|
|
? file.type
|
|
|
|
: "application/octet-stream",
|
|
|
|
);
|
|
|
|
ctx.set("Cache-Control", "max-age=31536000, immutable");
|
|
|
|
ctx.set("Content-Disposition", contentDisposition("inline", file.name));
|
2018-05-03 13:03:14 +02:00
|
|
|
}
|
2018-04-12 23:06:18 +02:00
|
|
|
}
|