fixed minor streaming nits

This commit is contained in:
Danny Coates 2018-06-25 10:57:52 -07:00
parent c157e4d31c
commit a4cf46c0eb
No known key found for this signature in database
GPG key ID: 4C442633C62E00CB
5 changed files with 36 additions and 65 deletions

View file

@ -16,51 +16,46 @@ module.exports = async function(ws, req) {
}
});
let first = true;
ws.on('message', async function(message) {
ws.once('message', async function(message) {
try {
if (first) {
const newId = crypto.randomBytes(5).toString('hex');
const owner = crypto.randomBytes(10).toString('hex');
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) {
ws.send(
JSON.stringify({
error: 400
})
);
ws.close();
}
const meta = {
owner,
metadata,
auth: auth.split(' ')[1],
nonce: crypto.randomBytes(16).toString('base64')
};
const protocol = config.env === 'production' ? 'https' : req.protocol;
const url = `${protocol}://${req.get('host')}/download/${newId}/`;
const limiter = new Limiter(config.max_file_size);
fileStream = wsStream(ws, { binary: true }).pipe(limiter);
storage.set(newId, fileStream, meta);
const fileInfo = JSON.parse(message);
const metadata = fileInfo.fileMetadata;
const auth = fileInfo.authorization;
if (!metadata || !auth) {
ws.send(
JSON.stringify({
url,
owner: meta.owner,
id: newId,
authentication: `send-v1 ${meta.nonce}`
error: 400
})
);
first = false;
return ws.close();
}
const meta = {
owner,
metadata,
auth: auth.split(' ')[1],
nonce: crypto.randomBytes(16).toString('base64')
};
const protocol = config.env === 'production' ? 'https' : req.protocol;
const url = `${protocol}://${req.get('host')}/download/${newId}/`;
const limiter = new Limiter(config.max_file_size);
fileStream = wsStream(ws, { binary: true }).pipe(limiter);
storage.set(newId, fileStream, meta);
ws.send(
JSON.stringify({
url,
owner: meta.owner,
id: newId,
authentication: `send-v1 ${meta.nonce}`
})
);
} catch (e) {
log.error('upload', e);
ws.send(