send/server/routes/filelist.js

51 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

const crypto = require('crypto');
2018-08-07 22:40:17 +00:00
const config = require('../config');
const storage = require('../storage');
const Limiter = require('../limiter');
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-07 22:40:17 +00:00
}
module.exports = {
async get(req, res) {
const kid = req.params.id;
2018-08-07 22:40:17 +00:00
try {
const fileId = id(req.user, kid);
2018-08-07 22:40:17 +00: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) {
const kid = req.params.id;
2018-08-07 22:40:17 +00:00
try {
const limiter = new Limiter(1024 * 1024 * 10);
const fileStream = req.pipe(limiter);
await storage.set(
id(req.user, kid),
2018-08-07 22:40:17 +00:00
fileStream,
2018-12-18 21:55:46 +00:00
null,
2018-08-07 22:40:17 +00:00
config.max_expire_seconds
);
res.sendStatus(200);
} catch (e) {
if (e.message === 'limit') {
return res.sendStatus(413);
}
res.sendStatus(500);
}
}
};