summaryrefslogtreecommitdiff
path: root/src/server/file/index.ts
blob: 973528da33d8d528bd9e1536dc4bd2fe74338328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
 * File Server
 */

import * as fs from 'fs';
import * as Koa from 'koa';
import * as cors from '@koa/cors';
import * as Router from 'koa-router';
import sendDriveFile from './send-drive-file';

// Init app
const app = new Koa();
app.use(cors());

app.use(async (ctx, next) => {
	// Cache 365days
	ctx.set('Cache-Control', 'max-age=31536000, immutable');
	await next();
});

// Init router
const router = new Router();

router.get('/default-avatar.jpg', ctx => {
	const file = fs.createReadStream(`${__dirname}/assets/avatar.jpg`);
	ctx.set('Content-Type', 'image/jpeg');
	ctx.body = file;
});

router.get('/app-default.jpg', ctx => {
	const file = fs.createReadStream(`${__dirname}/assets/dummy.png`);
	ctx.set('Content-Type', 'image/jpeg');
	ctx.body = file;
});

router.get('/:id', sendDriveFile);
router.get('/:id/*', sendDriveFile);

// Register router
app.use(router.routes());

module.exports = app;