165 lines
3.5 KiB
JavaScript
165 lines
3.5 KiB
JavaScript
'use strict'
|
|
|
|
const config = require('./config');
|
|
const JsonUserDatabase = require('./lib/JsonUserDatabase');
|
|
|
|
const _ = require('lodash');
|
|
const express = require('express');
|
|
const bodyParser = require('body-parser');
|
|
const tus = require('tus-node-server');
|
|
const jwt = require('jsonwebtoken');
|
|
const path = require('path');
|
|
|
|
const webpack = require('webpack');
|
|
const webpackDevMiddleware = require('webpack-dev-middleware');
|
|
const webpackHotMiddleware = require('webpack-hot-middleware');
|
|
const webpackConfig = require('./webpack.config.js');
|
|
|
|
/*
|
|
function metadataToObject(stringValue)
|
|
{
|
|
const keyValuePairList = stringValue.split(',');
|
|
|
|
return _.reduce(keyValuePairList , (metadata, keyValuePair) => {
|
|
let [key, base64Value] = keyValuePair.split(' ');
|
|
metadata[key] = new Buffer(base64Value, "base64").toString("ascii");
|
|
|
|
return metadata;
|
|
}, {});
|
|
}
|
|
*/
|
|
|
|
|
|
function checkAuthorization(req, res, onVerified)
|
|
{
|
|
if (!req.headers.authorization || req.headers.authorization.split(' ')[0] !== 'Bearer')
|
|
{
|
|
res.sendStatus(400);
|
|
return;
|
|
}
|
|
|
|
var token = req.headers.authorization.split(' ')[1];
|
|
jwt.verify(token, config.jwtSecret, (err, decoded) =>
|
|
{
|
|
if (err)
|
|
{
|
|
res.sendStatus(403);
|
|
return;
|
|
}
|
|
|
|
onVerified(decoded);
|
|
});
|
|
}
|
|
|
|
|
|
(async function()
|
|
{
|
|
const isDevelopment = process.env.NODE_ENV !== 'production';
|
|
|
|
const userDatabase = new JsonUserDatabase(config.userDatabasePath);
|
|
await userDatabase.load();
|
|
|
|
const tusServer = new tus.Server();
|
|
tusServer.datastore = new tus.FileStore({
|
|
path: config.fileUploadPublicPath,
|
|
directory: config.fileUploadPath
|
|
});
|
|
|
|
/*
|
|
tusServer.on(tus.EVENTS.EVENT_UPLOAD_COMPLETE, (event) =>
|
|
{
|
|
console.log(event);
|
|
|
|
const metadata = metadataToObject(event.file.upload_metadata);
|
|
jwt.verify(metadata.token, config.jwtSecret, (err, decoded) =>
|
|
{
|
|
if (err)
|
|
return;
|
|
|
|
const filePath = path.join(config.fileUploadPath, event.file.id);
|
|
console.log(filePath);
|
|
|
|
// TODO save metadata for file and notify people
|
|
});
|
|
});
|
|
*/
|
|
|
|
|
|
const app = express();
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
app.use(bodyParser.json());
|
|
|
|
|
|
// Token API
|
|
app.post('/token/upload', async (req, res) =>
|
|
{
|
|
if (!req.body.code)
|
|
{
|
|
res.sendStatus(400);
|
|
return;
|
|
}
|
|
|
|
if (await userDatabase.isValidCode(req.body.code))
|
|
{
|
|
jwt.sign({
|
|
code: req.body.code
|
|
}, config.jwtSecret, (err, token) =>
|
|
{
|
|
if (err)
|
|
res.sendStatus(500);
|
|
else
|
|
res.send(token);
|
|
});
|
|
}
|
|
else
|
|
res.sendStatus(403);
|
|
});
|
|
|
|
|
|
// Upload API
|
|
app.post('/complete', (req, res) =>
|
|
{
|
|
if (!req.body.files)
|
|
{
|
|
res.sendStatus(400);
|
|
return;
|
|
}
|
|
|
|
checkAuthorization(req, res, (decoded) =>
|
|
{
|
|
console.log(req.body.files);
|
|
// TODO save set
|
|
});
|
|
});
|
|
|
|
|
|
// Tus upload
|
|
const uploadApp = express();
|
|
uploadApp.all('*', (req, res) =>
|
|
{
|
|
checkAuthorization(req, res, (decoded) =>
|
|
{
|
|
tusServer.handle(req, res);
|
|
});
|
|
});
|
|
|
|
app.use('/upload', uploadApp);
|
|
|
|
|
|
// Frontend
|
|
if (isDevelopment)
|
|
{
|
|
const compiler = webpack(webpackConfig);
|
|
|
|
app.use(webpackDevMiddleware(compiler, {
|
|
publicPath: webpackConfig.output.publicPath
|
|
}));
|
|
|
|
app.use(webpackHotMiddleware(compiler));
|
|
}
|
|
|
|
app.use(express.static(path.join(__dirname, 'public', 'dist')));
|
|
|
|
var server = app.listen(config.port, () => console.log('Recv running on port ' + server.address().port));
|
|
})(); |