2016-12-28 16:49:51 -06:00
|
|
|
/**
|
|
|
|
* 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';
|
2017-12-09 07:35:26 -06:00
|
|
|
import * as _gm from 'gm';
|
2017-11-09 19:54:08 -06:00
|
|
|
import * as stream from 'stream';
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-03-29 06:32:18 -05:00
|
|
|
import DriveFile, { getGridFSBucket } from '../../models/drive-file';
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2017-12-09 07:35:26 -06:00
|
|
|
const gm = _gm.subClass({
|
|
|
|
imageMagick: true
|
|
|
|
});
|
|
|
|
|
2016-12-28 16:49:51 -06:00
|
|
|
/**
|
|
|
|
* Init app
|
|
|
|
*/
|
|
|
|
const app = express();
|
|
|
|
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
app.locals.cache = true;
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
app.use(cors());
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Statics
|
|
|
|
*/
|
2017-04-14 06:45:37 -05:00
|
|
|
app.use('/assets', express.static(`${__dirname}/assets`, {
|
2016-12-28 16:49:51 -06:00
|
|
|
maxAge: 1000 * 60 * 60 * 24 * 365 // 一年
|
|
|
|
}));
|
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
res.send('yee haw');
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/default-avatar.jpg', (req, res) => {
|
2017-11-09 19:54:08 -06:00
|
|
|
const file = fs.createReadStream(`${__dirname}/assets/avatar.jpg`);
|
2016-12-28 16:49:51 -06:00
|
|
|
send(file, 'image/jpeg', req, res);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/app-default.jpg', (req, res) => {
|
2017-11-09 19:54:08 -06:00
|
|
|
const file = fs.createReadStream(`${__dirname}/assets/dummy.png`);
|
2016-12-28 16:49:51 -06:00
|
|
|
send(file, 'image/png', req, res);
|
|
|
|
});
|
|
|
|
|
2017-11-09 19:54:08 -06:00
|
|
|
interface ISend {
|
|
|
|
contentType: string;
|
|
|
|
stream: stream.Readable;
|
|
|
|
}
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2017-11-09 19:54:08 -06:00
|
|
|
function thumbnail(data: stream.Readable, type: string, resize: number): ISend {
|
2017-11-16 08:14:19 -06:00
|
|
|
const readable: stream.Readable = (() => {
|
2018-04-11 12:34:51 -05:00
|
|
|
// 動画であれば
|
|
|
|
if (/^video\/.*$/.test(type)) {
|
|
|
|
// 実装は先延ばし
|
|
|
|
// 使わないことになったストリームはしっかり取り壊す
|
|
|
|
data.destroy();
|
|
|
|
return fs.createReadStream(`${__dirname}/assets/thumbnail-not-available.png`);
|
|
|
|
// 画像であれば
|
|
|
|
} else if (/^image\/.*$/.test(type) || type == 'application/xml') {
|
2018-04-11 09:14:08 -05:00
|
|
|
// 0フレーム目を送る
|
|
|
|
try {
|
|
|
|
return gm(data).selectFrame(0).stream();
|
|
|
|
// だめだったら
|
|
|
|
} catch (e) {
|
|
|
|
// 使わないことになったストリームはしっかり取り壊す
|
|
|
|
data.destroy();
|
|
|
|
return fs.createReadStream(`${__dirname}/assets/thumbnail-not-available.png`);
|
|
|
|
}
|
|
|
|
// 動画か画像以外
|
|
|
|
} else {
|
2017-11-16 08:14:19 -06:00
|
|
|
data.destroy();
|
|
|
|
return fs.createReadStream(`${__dirname}/assets/not-an-image.png`);
|
2017-11-09 19:54:08 -06:00
|
|
|
}
|
2017-11-16 08:14:19 -06:00
|
|
|
})();
|
|
|
|
|
|
|
|
let g = gm(readable);
|
|
|
|
|
|
|
|
if (resize) {
|
|
|
|
g = g.resize(resize, resize);
|
|
|
|
}
|
|
|
|
|
|
|
|
const stream = g
|
|
|
|
.compress('jpeg')
|
|
|
|
.quality(80)
|
2017-12-10 11:59:05 -06:00
|
|
|
.interlace('line')
|
2017-11-16 08:14:19 -06:00
|
|
|
.stream();
|
|
|
|
|
|
|
|
return {
|
|
|
|
contentType: 'image/jpeg',
|
|
|
|
stream
|
|
|
|
};
|
2016-12-28 16:49:51 -06:00
|
|
|
}
|
|
|
|
|
2017-11-09 19:54:08 -06:00
|
|
|
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));
|
2016-12-28 16:49:51 -06:00
|
|
|
}
|
|
|
|
|
2017-11-09 19:54:08 -06:00
|
|
|
if (req.query.download !== undefined) {
|
|
|
|
res.header('Content-Disposition', 'attachment');
|
2016-12-28 16:49:51 -06:00
|
|
|
}
|
|
|
|
|
2017-11-09 19:54:08 -06:00
|
|
|
res.header('Content-Type', data.contentType);
|
2017-04-14 06:45:37 -05:00
|
|
|
|
2017-11-09 19:54:08 -06:00
|
|
|
data.stream.pipe(res);
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2017-11-09 19:54:08 -06:00
|
|
|
data.stream.on('end', () => {
|
|
|
|
res.end();
|
|
|
|
});
|
2016-12-28 16:49:51 -06:00
|
|
|
}
|
|
|
|
|
2017-11-06 18:18:40 -06:00
|
|
|
async function sendFileById(req: express.Request, res: express.Response): Promise<void> {
|
2017-02-06 07:04:00 -06:00
|
|
|
// Validate id
|
|
|
|
if (!mongodb.ObjectID.isValid(req.params.id)) {
|
|
|
|
res.status(400).send('incorrect id');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-06 01:32:01 -06:00
|
|
|
const fileId = new mongodb.ObjectID(req.params.id);
|
2017-12-09 08:03:48 -06:00
|
|
|
|
|
|
|
// Fetch (drive) file
|
2017-11-06 00:35:20 -06:00
|
|
|
const file = await DriveFile.findOne({ _id: fileId });
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2017-11-06 18:18:40 -06:00
|
|
|
// validate name
|
2017-11-06 18:30:51 -06:00
|
|
|
if (req.params.name !== undefined && req.params.name !== file.filename) {
|
2017-11-06 18:18:40 -06:00
|
|
|
res.status(404).send('there is no file has given name');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-26 08:11:42 -06:00
|
|
|
if (file == null) {
|
2017-11-06 00:37:04 -06:00
|
|
|
res.status(404).sendFile(`${__dirname}/assets/dummy.png`);
|
2016-12-28 16:49:51 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-06 01:32:01 -06:00
|
|
|
const bucket = await getGridFSBucket();
|
2017-11-06 00:35:20 -06:00
|
|
|
|
2017-11-09 19:54:08 -06:00
|
|
|
const readable = bucket.openDownloadStream(fileId);
|
|
|
|
|
|
|
|
send(readable, file.contentType, req, res);
|
2017-11-06 18:14:39 -06:00
|
|
|
}
|
2017-11-06 00:39:16 -06:00
|
|
|
|
2017-11-06 18:14:39 -06:00
|
|
|
/**
|
|
|
|
* Routing
|
|
|
|
*/
|
2017-11-06 00:39:16 -06:00
|
|
|
|
2017-11-06 18:14:39 -06:00
|
|
|
app.get('/:id', sendFileById);
|
|
|
|
app.get('/:id/:name', sendFileById);
|
2016-12-28 16:49:51 -06:00
|
|
|
|
|
|
|
module.exports = app;
|