fox-send/server/routes/filelist.js

50 lines
1.1 KiB
JavaScript
Raw 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);
2020-07-27 18:18:52 +00:00
const { length, stream } = await storage.get(fileId);
2018-08-07 22:40:17 +00:00
res.writeHead(200, {
'Content-Type': 'application/octet-stream',
2020-07-27 18:18:52 +00:00
'Content-Length': length
2018-08-07 22:40:17 +00:00
});
2020-07-27 18:18:52 +00:00
stream.pipe(res);
2018-08-07 22:40:17 +00:00
} 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);
}
}
};