send/server/routes/ws.js

75 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-06-21 00:05:33 +00:00
const crypto = require('crypto');
const storage = require('../storage');
const config = require('../config');
const mozlog = require('../log');
const Limiter = require('../limiter');
const wsStream = require('websocket-stream/stream');
const log = mozlog('send.upload');
module.exports = async function(ws, req) {
let fileStream;
2018-06-21 20:57:53 +00:00
ws.on('close', e => {
if (e !== 1000) {
if (fileStream !== undefined) {
fileStream.destroy();
2018-06-21 00:05:33 +00:00
}
2018-06-21 20:57:53 +00:00
}
});
2018-06-21 00:05:33 +00:00
2018-06-21 20:57:53 +00:00
let first = true;
ws.on('message', function(message) {
try {
2018-06-21 00:05:33 +00:00
if (first) {
const newId = crypto.randomBytes(5).toString('hex');
const owner = crypto.randomBytes(10).toString('hex');
const fileInfo = JSON.parse(message);
const metadata = fileInfo.fileMetadata;
const auth = fileInfo.authorization;
if (!metadata || !auth) {
2018-06-21 20:57:53 +00:00
ws.send(
JSON.stringify({
error: 400
})
);
2018-06-21 00:05:33 +00:00
}
const meta = {
owner,
metadata,
auth: auth.split(' ')[1],
nonce: crypto.randomBytes(16).toString('base64')
};
const limiter = new Limiter(config.max_file_size);
fileStream = wsStream(ws, { binary: true }).pipe(limiter);
storage.set(newId, fileStream, meta);
const protocol = config.env === 'production' ? 'https' : req.protocol;
const url = `${protocol}://${req.get('host')}/download/${newId}/`;
ws.send(
JSON.stringify({
url,
owner: meta.owner,
id: newId,
authentication: `send-v1 ${meta.nonce}`
})
);
first = false;
}
2018-06-21 20:57:53 +00:00
} catch (e) {
log.error('upload', e);
ws.send(
JSON.stringify({
error: 500
})
);
}
});
2018-06-21 00:05:33 +00:00
};