summaryrefslogtreecommitdiff
path: root/src/file/server.ts
blob: 3bda5b14fe341b8c1ff23e659dec4f1f5a59b3c5 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
 * 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 * as stream from 'stream';

import DriveFile, { getGridFSBucket } from '../api/models/drive-file';

const gm = _gm.subClass({
	imageMagick: true
});

/**
 * 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('/assets', express.static(`${__dirname}/assets`, {
	maxAge: 1000 * 60 * 60 * 24 * 365 // 一年
}));

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

app.get('/default-avatar.jpg', (req, res) => {
	const file = fs.createReadStream(`${__dirname}/assets/avatar.jpg`);
	send(file, 'image/jpeg', req, res);
});

app.get('/app-default.jpg', (req, res) => {
	const file = fs.createReadStream(`${__dirname}/assets/dummy.png`);
	send(file, 'image/png', req, res);
});

interface ISend {
	contentType: string;
	stream: stream.Readable;
}

function thumbnail(data: stream.Readable, type: string, resize: number): ISend {
	const readable: stream.Readable = (() => {
		// 画像ではない場合
		if (!/^image\/.*$/.test(type)) {
			// 使わないことにしたストリームはしっかり取り壊しておく
			data.destroy();
			return fs.createReadStream(`${__dirname}/assets/not-an-image.png`);
		}

		const imageType = type.split('/')[1];

		// 画像でもPNGかJPEGでないならダメ
		if (imageType != 'png' && imageType != 'jpeg') {
			// 使わないことにしたストリームはしっかり取り壊しておく
			data.destroy();
			return fs.createReadStream(`${__dirname}/assets/thumbnail-not-available.png`);
		}

		return data;
	})();

	let g = gm(readable);

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

	const stream = g
		.compress('jpeg')
		.quality(80)
		.interlace('line')
		.noProfile() // Remove EXIF
		.stream();

	return {
		contentType: 'image/jpeg',
		stream
	};
}

const commonReadableHandlerGenerator = (req: express.Request, res: express.Response) => (e: Error): void => {
	console.dir(e);
	req.destroy();
	res.destroy(e);
};

function send(readable: stream.Readable, type: string, req: express.Request, res: express.Response): void {
	readable.on('error', commonReadableHandlerGenerator(req, res));

	const data = ((): ISend => {
		if (req.query.thumbnail !== undefined) {
			return thumbnail(readable, type, req.query.size);
		}
		return {
			contentType: type,
			stream: readable
		};
	})();

	if (readable !== data.stream) {
		data.stream.on('error', commonReadableHandlerGenerator(req, res));
	}

	if (req.query.download !== undefined) {
		res.header('Content-Disposition', 'attachment');
	}

	res.header('Content-Type', data.contentType);

	data.stream.pipe(res);

	data.stream.on('end', () => {
		res.end();
	});
}

async function sendFileById(req: express.Request, res: express.Response): Promise<void> {
	// Validate id
	if (!mongodb.ObjectID.isValid(req.params.id)) {
		res.status(400).send('incorrect id');
		return;
	}

	const fileId = new mongodb.ObjectID(req.params.id);

	// Fetch (drive) file
	const file = await DriveFile.findOne({ _id: fileId });

	// validate name
	if (req.params.name !== undefined && req.params.name !== file.filename) {
		res.status(404).send('there is no file has given name');
		return;
	}

	if (file == null) {
		res.status(404).sendFile(`${__dirname}/assets/dummy.png`);
		return;
	}

	const bucket = await getGridFSBucket();

	const readable = bucket.openDownloadStream(fileId);

	send(readable, file.contentType, req, res);
}

/**
 * Routing
 */

app.get('/:id', sendFileById);
app.get('/:id/:name', sendFileById);

module.exports = app;