2017-08-24 23:54:02 +02:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const storage = require('../storage');
|
|
|
|
const config = require('../config');
|
|
|
|
const mozlog = require('../log');
|
2018-05-31 23:06:25 +02:00
|
|
|
const Limiter = require('../limiter');
|
2019-03-06 19:31:50 +01:00
|
|
|
const { encryptedSize } = require('../../app/utils');
|
2017-08-24 23:54:02 +02:00
|
|
|
|
|
|
|
const log = mozlog('send.upload');
|
|
|
|
|
2018-05-31 23:06:25 +02:00
|
|
|
module.exports = async function(req, res) {
|
2019-03-26 17:32:44 +01:00
|
|
|
const newId = crypto.randomBytes(8).toString('hex');
|
2017-08-31 18:43:36 +02:00
|
|
|
const metadata = req.header('X-File-Metadata');
|
|
|
|
const auth = req.header('Authorization');
|
|
|
|
if (!metadata || !auth) {
|
|
|
|
return res.sendStatus(400);
|
2017-08-24 23:54:02 +02:00
|
|
|
}
|
2017-11-30 22:41:09 +01:00
|
|
|
const owner = crypto.randomBytes(10).toString('hex');
|
2017-08-31 18:43:36 +02:00
|
|
|
const meta = {
|
2017-11-30 22:41:09 +01:00
|
|
|
owner,
|
2017-08-31 18:43:36 +02:00
|
|
|
metadata,
|
|
|
|
auth: auth.split(' ')[1],
|
|
|
|
nonce: crypto.randomBytes(16).toString('base64')
|
|
|
|
};
|
2017-08-24 23:54:02 +02:00
|
|
|
|
2018-05-31 23:06:25 +02:00
|
|
|
try {
|
2019-03-06 19:31:50 +01:00
|
|
|
const limiter = new Limiter(encryptedSize(config.max_file_size));
|
2018-05-31 23:06:25 +02:00
|
|
|
const fileStream = req.pipe(limiter);
|
2018-08-08 20:07:09 +02:00
|
|
|
//this hasn't been updated to expiration time setting yet
|
|
|
|
//if you want to fallback to this code add this
|
|
|
|
await storage.set(newId, fileStream, meta, config.default_expire_seconds);
|
2021-05-07 13:00:45 +02:00
|
|
|
const url = `${config.deriveBaseUrl(req)}/download/${newId}/`;
|
2018-05-31 23:06:25 +02:00
|
|
|
res.set('WWW-Authenticate', `send-v1 ${meta.nonce}`);
|
|
|
|
res.json({
|
|
|
|
url,
|
|
|
|
owner: meta.owner,
|
|
|
|
id: newId
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
if (e.message === 'limit') {
|
|
|
|
return res.sendStatus(413);
|
2017-08-24 23:54:02 +02:00
|
|
|
}
|
2018-05-31 23:06:25 +02:00
|
|
|
log.error('upload', e);
|
|
|
|
res.sendStatus(500);
|
|
|
|
}
|
2017-08-24 23:54:02 +02:00
|
|
|
};
|