summaryrefslogtreecommitdiff
path: root/src/file/server.ts
blob: 0f269c44242c3ac9986be9d723afbad360ceaef3 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * File Server
 */

import * as fs from 'fs';
import * as express from 'express';
import * as bodyParser from 'body-parser';
import * as cors from 'cors';
import * as mongodb from 'mongodb';
import * as gm from 'gm';

import File from '../api/models/drive-file';

/**
 * Init app
 */
const app = express();

app.disable('x-powered-by');
app.locals.cache = true;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());

/**
 * Statics
 */
app.use('/resources', express.static(__dirname + '/resources', {
	maxAge: 1000 * 60 * 60 * 24 * 365 // 一年
}));

app.get('/', (req, res) => {
	res.send('yee haw');
});

app.get('/default-avatar.jpg', (req, res) => {
	const file = fs.readFileSync(__dirname + '/resources/avatar.jpg');
	send(file, 'image/jpeg', req, res);
});

app.get('/app-default.jpg', (req, res) => {
	const file = fs.readFileSync(__dirname + '/resources/dummy.png');
	send(file, 'image/png', req, res);
});

async function raw(data: Buffer, type: string, download: boolean, res: express.Response): Promise<any> {
	res.header('Content-Type', type);

	if (download) {
		res.header('Content-Disposition', 'attachment');
	}

	res.send(data);
}

async function thumbnail(data: Buffer, type: string, resize: number, res: express.Response): Promise<any> {
	if (!/^image\/.*$/.test(type)) {
		data = fs.readFileSync(__dirname + '/resources/dummy.png');
	}

	let g = gm(data);

	if (resize) {
		g = g.resize(resize, resize);
	}

	g
	.compress('jpeg')
	.quality(80)
	.toBuffer('jpeg', (err, img) => {
		if (err !== undefined && err !== null) {
			console.error(err);
			res.sendStatus(500);
			return;
		}

		res.header('Content-Type', 'image/jpeg');
		res.send(img);
	});
}

function send(data: Buffer, type: string, req: express.Request, res: express.Response): void {
	if (req.query.thumbnail !== undefined) {
		thumbnail(data, type, req.query.size, res);
	} else {
		raw(data, type, req.query.download !== undefined, res);
	}
}

/**
 * Routing
 */

app.get('/:id', async (req, res): Promise<void> => {
	const file = await File.findOne({_id: new mongodb.ObjectID(req.params.id)});

	if (file === null) {
		res.status(404).sendFile(__dirname + '/resources/dummy.png');
		return;
	}

	send(file.data.buffer, file.type, req, res);
});

app.get('/:id/:name', async (req, res): Promise<void> => {
	const file = await File.findOne({_id: new mongodb.ObjectID(req.params.id)});

	if (file === null) {
		res.status(404).sendFile(__dirname + '/resources/dummy.png');
		return;
	}

	send(file.data.buffer, file.type, req, res);
});

module.exports = app;