2016-12-29 07:49:51 +09:00
|
|
|
/**
|
|
|
|
* File Server
|
|
|
|
*/
|
|
|
|
|
2023-01-13 13:40:33 +09:00
|
|
|
import * as fs from "node:fs";
|
|
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import { dirname } from "node:path";
|
|
|
|
import Koa from "koa";
|
|
|
|
import cors from "@koa/cors";
|
|
|
|
import Router from "@koa/router";
|
|
|
|
import sendDriveFile from "./send-drive-file.js";
|
2022-02-27 11:07:39 +09:00
|
|
|
|
|
|
|
const _filename = fileURLToPath(import.meta.url);
|
2021-08-19 18:33:41 +09:00
|
|
|
const _dirname = dirname(_filename);
|
2018-04-13 06:06:18 +09:00
|
|
|
|
|
|
|
// Init app
|
|
|
|
const app = new Koa();
|
2016-12-29 07:49:51 +09:00
|
|
|
app.use(cors());
|
2021-08-24 13:08:20 +09:00
|
|
|
app.use(async (ctx, next) => {
|
2023-01-13 13:40:33 +09:00
|
|
|
ctx.set(
|
|
|
|
"Content-Security-Policy",
|
|
|
|
`default-src 'none'; img-src 'self'; media-src 'self'; style-src 'unsafe-inline'`,
|
|
|
|
);
|
2021-08-24 13:08:20 +09:00
|
|
|
await next();
|
|
|
|
});
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-04-13 06:06:18 +09:00
|
|
|
// Init router
|
|
|
|
const router = new Router();
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2023-01-13 13:40:33 +09:00
|
|
|
router.get("/app-default.jpg", (ctx) => {
|
2021-08-19 18:33:41 +09:00
|
|
|
const file = fs.createReadStream(`${_dirname}/assets/dummy.png`);
|
2018-05-03 20:03:14 +09:00
|
|
|
ctx.body = file;
|
2023-01-13 13:40:33 +09:00
|
|
|
ctx.set("Content-Type", "image/jpeg");
|
|
|
|
ctx.set("Cache-Control", "max-age=31536000, immutable");
|
2016-12-29 07:49:51 +09:00
|
|
|
});
|
|
|
|
|
2023-01-13 13:40:33 +09:00
|
|
|
router.get("/:key", sendDriveFile);
|
|
|
|
router.get("/:key/(.*)", sendDriveFile);
|
2017-11-06 15:39:16 +09:00
|
|
|
|
2018-04-13 06:06:18 +09:00
|
|
|
// Register router
|
|
|
|
app.use(router.routes());
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2022-02-27 11:07:39 +09:00
|
|
|
export default app;
|