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