2018-03-21 15:51:46 +00:00
|
|
|
const config = require('../../config');
|
|
|
|
const express = require('express');
|
2018-03-22 15:45:11 +00:00
|
|
|
const asyncHandler = require('express-async-handler');
|
2018-03-21 15:51:46 +00:00
|
|
|
const jwt = require('jsonwebtoken');
|
2018-04-27 15:13:18 +00:00
|
|
|
const resolvePath = require('resolve-path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const async = require('async');
|
2018-03-21 15:51:46 +00:00
|
|
|
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
async function checkAuthorization(req, res, onVerified)
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
if (!req.headers.authorization || req.headers.authorization.split(' ')[0] !== 'Bearer')
|
|
|
|
{
|
|
|
|
res.sendStatus(400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var token = req.headers.authorization.split(' ')[1];
|
2018-03-22 15:45:11 +00:00
|
|
|
jwt.verify(token, config.jwtSecret, async (err, decoded) =>
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
2018-03-22 15:45:11 +00:00
|
|
|
try
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
2018-03-22 15:45:11 +00:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
res.sendStatus(403);
|
|
|
|
return;
|
|
|
|
}
|
2018-03-21 15:51:46 +00:00
|
|
|
|
2018-04-27 15:13:18 +00:00
|
|
|
if (decoded.code)
|
2018-03-22 15:45:11 +00:00
|
|
|
await onVerified(decoded);
|
|
|
|
else
|
|
|
|
res.sendStatus(400);
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
console.log(e);
|
|
|
|
res.sendStatus(500);
|
|
|
|
}
|
2018-03-21 15:51:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-26 12:08:08 +00:00
|
|
|
module.exports = (repository, tusServer) =>
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
2018-03-22 15:45:11 +00:00
|
|
|
var router = express.Router();
|
|
|
|
|
2018-03-21 15:51:46 +00:00
|
|
|
// Upload API
|
2018-04-27 15:13:18 +00:00
|
|
|
router.get('/message/:code', asyncHandler(async (req, res) =>
|
|
|
|
{
|
2018-04-28 08:18:27 +00:00
|
|
|
var code = await repository.codes.get(req.params.code);
|
2018-04-27 15:13:18 +00:00
|
|
|
if (code === null)
|
|
|
|
{
|
|
|
|
res.sendStatus(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!code.messageHTML)
|
|
|
|
{
|
|
|
|
res.sendStatus(204);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-28 08:18:27 +00:00
|
|
|
var user = await repository.users.get(code.userId);
|
2018-04-27 15:13:18 +00:00
|
|
|
var name = user !== null ? user.name : null;
|
|
|
|
|
|
|
|
res.send({
|
|
|
|
name: name,
|
|
|
|
message: code.messageHTML
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
router.post('/complete', asyncHandler(async (req, res) =>
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
if (!req.body.files)
|
|
|
|
{
|
|
|
|
res.sendStatus(400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
await checkAuthorization(req, res, async (decoded) =>
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
var expiration = null; // TODO set expiration properties
|
2018-04-27 15:13:18 +00:00
|
|
|
|
|
|
|
async.each(req.body.files, (item, callback) =>
|
|
|
|
{
|
|
|
|
if (!item.id)
|
|
|
|
{
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var fullpath = resolvePath(config.fileUpload.path, item.id);
|
|
|
|
fs.stat(fullpath, (err, stats) =>
|
|
|
|
{
|
|
|
|
item.size = stats.size;
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
async (err) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
res.sendStatus(500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-28 08:18:27 +00:00
|
|
|
var uploadId = await repository.uploads.insert(decoded.codeUserId, decoded.code, req.body.files, expiration);
|
2018-04-28 13:00:30 +00:00
|
|
|
await repository.notifications.insert({
|
|
|
|
userId: decoded.codeUserId,
|
|
|
|
uploadId: uploadId
|
|
|
|
});
|
|
|
|
|
2018-04-27 15:13:18 +00:00
|
|
|
res.send({ id: uploadId });
|
|
|
|
});
|
2018-03-21 15:51:46 +00:00
|
|
|
});
|
2018-03-22 15:45:11 +00:00
|
|
|
}));
|
2018-03-21 15:51:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Tus upload
|
|
|
|
const uploadApp = express();
|
2018-03-22 15:45:11 +00:00
|
|
|
uploadApp.all('*', asyncHandler(async (req, res) =>
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
2018-03-22 15:45:11 +00:00
|
|
|
await checkAuthorization(req, res, async (decoded) =>
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
tusServer.handle(req, res);
|
|
|
|
});
|
2018-03-22 15:45:11 +00:00
|
|
|
}));
|
2018-03-21 15:51:46 +00:00
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
router.use('/upload', uploadApp);
|
|
|
|
return router;
|
2018-03-21 15:51:46 +00:00
|
|
|
}
|