2016-12-28 22:49:51 +00: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';
|
|
|
|
import * as gm from 'gm';
|
|
|
|
|
2017-11-06 06:35:20 +00:00
|
|
|
import DriveFile, { getGridFSBucket } from '../api/models/drive-file';
|
2016-12-28 22:49:51 +00: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 11:45:37 +00:00
|
|
|
app.use('/assets', express.static(`${__dirname}/assets`, {
|
2016-12-28 22:49:51 +00:00
|
|
|
maxAge: 1000 * 60 * 60 * 24 * 365 // 一年
|
|
|
|
}));
|
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
res.send('yee haw');
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/default-avatar.jpg', (req, res) => {
|
2017-04-14 11:45:37 +00:00
|
|
|
const file = fs.readFileSync(`${__dirname}/assets/avatar.jpg`);
|
2016-12-28 22:49:51 +00:00
|
|
|
send(file, 'image/jpeg', req, res);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/app-default.jpg', (req, res) => {
|
2017-04-14 11:45:37 +00:00
|
|
|
const file = fs.readFileSync(`${__dirname}/assets/dummy.png`);
|
2016-12-28 22:49:51 +00:00
|
|
|
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)) {
|
2017-04-14 11:45:37 +00:00
|
|
|
data = fs.readFileSync(`${__dirname}/assets/dummy.png`);
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let g = gm(data);
|
|
|
|
|
|
|
|
if (resize) {
|
|
|
|
g = g.resize(resize, resize);
|
|
|
|
}
|
|
|
|
|
|
|
|
g
|
2017-04-14 11:45:37 +00:00
|
|
|
.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);
|
|
|
|
});
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 00:18:40 +00:00
|
|
|
async function sendFileById(req: express.Request, res: express.Response): Promise<void> {
|
2017-02-06 13:04:00 +00:00
|
|
|
// Validate id
|
|
|
|
if (!mongodb.ObjectID.isValid(req.params.id)) {
|
|
|
|
res.status(400).send('incorrect id');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-06 07:32:01 +00:00
|
|
|
const fileId = new mongodb.ObjectID(req.params.id);
|
2017-11-06 06:35:20 +00:00
|
|
|
const file = await DriveFile.findOne({ _id: fileId });
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2017-11-07 00:18:40 +00:00
|
|
|
// validate name
|
2017-11-07 00:30:51 +00:00
|
|
|
if (req.params.name !== undefined && req.params.name !== file.filename) {
|
2017-11-07 00:18:40 +00:00
|
|
|
res.status(404).send('there is no file has given name');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-26 14:11:42 +00:00
|
|
|
if (file == null) {
|
2017-11-06 06:37:04 +00:00
|
|
|
res.status(404).sendFile(`${__dirname}/assets/dummy.png`);
|
2016-12-28 22:49:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-06 07:32:01 +00:00
|
|
|
const bucket = await getGridFSBucket();
|
2017-11-06 06:35:20 +00:00
|
|
|
|
|
|
|
const buffer = await ((id): Promise<Buffer> => new Promise((resolve, reject) => {
|
2017-11-06 07:32:01 +00:00
|
|
|
const chunks = [];
|
|
|
|
const readableStream = bucket.openDownloadStream(id);
|
2017-11-07 00:04:16 +00:00
|
|
|
readableStream.on('data', chunk => {
|
2017-11-06 06:35:20 +00:00
|
|
|
chunks.push(chunk);
|
2017-11-06 07:32:01 +00:00
|
|
|
});
|
2017-11-06 06:35:20 +00:00
|
|
|
readableStream.on('end', () => {
|
2017-11-06 07:32:01 +00:00
|
|
|
resolve(Buffer.concat(chunks));
|
|
|
|
});
|
|
|
|
}))(fileId);
|
2017-11-06 06:35:20 +00:00
|
|
|
|
|
|
|
send(buffer, file.metadata.type, req, res);
|
2017-11-07 00:14:39 +00:00
|
|
|
}
|
2017-11-06 06:39:16 +00:00
|
|
|
|
2017-11-07 00:14:39 +00:00
|
|
|
/**
|
|
|
|
* Routing
|
|
|
|
*/
|
2017-11-06 06:39:16 +00:00
|
|
|
|
2017-11-07 00:14:39 +00:00
|
|
|
app.get('/:id', sendFileById);
|
|
|
|
app.get('/:id/:name', sendFileById);
|
2016-12-28 22:49:51 +00:00
|
|
|
|
|
|
|
module.exports = app;
|