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) {
|
|
|
|
if (!req.user) {
|
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
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);
|
2018-08-08 00:40:17 +02:00
|
|
|
const contentLength = await storage.length(fileId);
|
|
|
|
const fileStream = await storage.get(fileId);
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'application/octet-stream',
|
|
|
|
'Content-Length': contentLength
|
|
|
|
});
|
|
|
|
fileStream.pipe(res);
|
|
|
|
} catch (e) {
|
|
|
|
res.sendStatus(404);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async post(req, res) {
|
|
|
|
if (!req.user) {
|
|
|
|
return res.sendStatus(401);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|