add streaming
This commit is contained in:
parent
34cb970f11
commit
1bd7e4d486
16 changed files with 438 additions and 187 deletions
|
@ -3,6 +3,14 @@ const locales = require('../common/locales');
|
|||
const routes = require('./routes');
|
||||
const pages = require('./routes/pages');
|
||||
const tests = require('../test/frontend/routes');
|
||||
const express = require('express');
|
||||
const expressWs = require('express-ws');
|
||||
const config = require('./config');
|
||||
|
||||
const wsapp = express();
|
||||
expressWs(wsapp, null, { perMessageDeflate: false });
|
||||
wsapp.ws('/api/ws', require('./routes/ws'));
|
||||
wsapp.listen(8081, config.listen_address);
|
||||
|
||||
module.exports = function(app, devServer) {
|
||||
assets.setMiddleware(devServer.middleware);
|
||||
|
|
|
@ -4,13 +4,15 @@ const Raven = require('raven');
|
|||
const config = require('./config');
|
||||
const routes = require('./routes');
|
||||
const pages = require('./routes/pages');
|
||||
const expressWs = require('express-ws');
|
||||
|
||||
if (config.sentry_dsn) {
|
||||
Raven.config(config.sentry_dsn).install();
|
||||
}
|
||||
|
||||
const app = express();
|
||||
|
||||
expressWs(app, null, { perMessageDeflate: false });
|
||||
app.ws('/api/ws', require('./routes/ws')); //want to move this into routes/index.js but it's not working...
|
||||
routes(app);
|
||||
|
||||
app.use(
|
||||
|
|
|
@ -62,6 +62,10 @@ module.exports = function(app) {
|
|||
app.post(`/api/params/:id${ID_REGEX}`, owner, require('./params'));
|
||||
app.post(`/api/info/:id${ID_REGEX}`, owner, require('./info'));
|
||||
|
||||
if (!IS_DEV) {
|
||||
app.ws('/api/ws', require('./ws'));
|
||||
}
|
||||
|
||||
app.get('/__version__', function(req, res) {
|
||||
res.sendFile(require.resolve('../../dist/version.json'));
|
||||
});
|
||||
|
|
68
server/routes/ws.js
Normal file
68
server/routes/ws.js
Normal file
|
@ -0,0 +1,68 @@
|
|||
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;
|
||||
|
||||
try {
|
||||
ws.on('close', e => {
|
||||
if (e !== 1000) {
|
||||
if (fileStream !== undefined) {
|
||||
fileStream.destroy();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let first = true;
|
||||
ws.on('message', function(message) {
|
||||
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) {
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
log.error('upload', e);
|
||||
//res.sendStatus(500);
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue