2019-02-26 22:53:11 +01:00
|
|
|
const crypto = require('crypto');
|
2018-08-08 00:40:17 +02:00
|
|
|
const config = require('../config');
|
|
|
|
const storage = require('../storage');
|
|
|
|
const Limiter = require('../limiter');
|
|
|
|
|
2019-02-26 22:53:11 +01:00
|
|
|
function id(user, kid) {
|
|
|
|
const sha = crypto.createHash('sha256');
|
|
|
|
sha.update(user);
|
|
|
|
sha.update(kid);
|
|
|
|
const hash = sha.digest('hex');
|
|
|
|
return `filelist-${hash}`;
|
2018-08-08 00:40:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
async get(req, res) {
|
2019-02-26 22:53:11 +01:00
|
|
|
const kid = req.params.id;
|
2018-08-08 00:40:17 +02:00
|
|
|
try {
|
2019-02-26 22:53:11 +01:00
|
|
|
const fileId = id(req.user, kid);
|
2020-07-27 20:18:52 +02:00
|
|
|
const { length, stream } = await storage.get(fileId);
|
2018-08-08 00:40:17 +02:00
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'application/octet-stream',
|
2020-07-27 20:18:52 +02:00
|
|
|
'Content-Length': length
|
2018-08-08 00:40:17 +02:00
|
|
|
});
|
2020-07-27 20:18:52 +02:00
|
|
|
stream.pipe(res);
|
2018-08-08 00:40:17 +02:00
|
|
|
} catch (e) {
|
|
|
|
res.sendStatus(404);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async post(req, res) {
|
2019-02-26 22:53:11 +01:00
|
|
|
const kid = req.params.id;
|
2018-08-08 00:40:17 +02:00
|
|
|
try {
|
|
|
|
const limiter = new Limiter(1024 * 1024 * 10);
|
|
|
|
const fileStream = req.pipe(limiter);
|
|
|
|
await storage.set(
|
2019-02-26 22:53:11 +01:00
|
|
|
id(req.user, kid),
|
2018-08-08 00:40:17 +02:00
|
|
|
fileStream,
|
2018-12-18 22:55:46 +01:00
|
|
|
null,
|
2018-08-08 00:40:17 +02:00
|
|
|
config.max_expire_seconds
|
|
|
|
);
|
|
|
|
res.sendStatus(200);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.message === 'limit') {
|
|
|
|
return res.sendStatus(413);
|
|
|
|
}
|
|
|
|
res.sendStatus(500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|