diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2016-12-29 07:49:51 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2016-12-29 07:49:51 +0900 |
| commit | b3f42e62af698a67c2250533c437569559f1fdf9 (patch) | |
| tree | cdf6937576e99cccf85e6fa3aa8860a1173c7cfb /src/file | |
| download | sharkey-b3f42e62af698a67c2250533c437569559f1fdf9.tar.gz sharkey-b3f42e62af698a67c2250533c437569559f1fdf9.tar.bz2 sharkey-b3f42e62af698a67c2250533c437569559f1fdf9.zip | |
Initial commit :four_leaf_clover:
Diffstat (limited to 'src/file')
| -rw-r--r-- | src/file/resources/avatar.jpg | bin | 0 -> 1322 bytes | |||
| -rw-r--r-- | src/file/resources/bad-egg.png | bin | 0 -> 4783 bytes | |||
| -rw-r--r-- | src/file/resources/dummy.png | bin | 0 -> 6285 bytes | |||
| -rw-r--r-- | src/file/server.ts | 115 |
4 files changed, 115 insertions, 0 deletions
diff --git a/src/file/resources/avatar.jpg b/src/file/resources/avatar.jpg Binary files differnew file mode 100644 index 0000000000..3c803f568e --- /dev/null +++ b/src/file/resources/avatar.jpg diff --git a/src/file/resources/bad-egg.png b/src/file/resources/bad-egg.png Binary files differnew file mode 100644 index 0000000000..a7c5930bd4 --- /dev/null +++ b/src/file/resources/bad-egg.png diff --git a/src/file/resources/dummy.png b/src/file/resources/dummy.png Binary files differnew file mode 100644 index 0000000000..39332b0c1b --- /dev/null +++ b/src/file/resources/dummy.png diff --git a/src/file/server.ts b/src/file/server.ts new file mode 100644 index 0000000000..0f269c4424 --- /dev/null +++ b/src/file/server.ts @@ -0,0 +1,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; |