2018-03-21 15:51:46 +00:00
|
|
|
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-05-13 07:33:10 +00:00
|
|
|
const NotificationType = require('../repository/notification').NotificationType;
|
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-05-13 07:33:10 +00:00
|
|
|
if (decoded.codeId)
|
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-05-02 21:01:29 +00:00
|
|
|
router.get('/info/:code', asyncHandler(async (req, res) =>
|
2018-04-27 15:13:18 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-02 21:01:29 +00:00
|
|
|
let info = {
|
|
|
|
message: null,
|
2018-08-16 13:18:02 +00:00
|
|
|
expirationDate: code.expirationDate,
|
|
|
|
expiration: code.expiration
|
2018-05-02 21:01:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (code.messageHTML)
|
2018-04-27 15:13:18 +00:00
|
|
|
{
|
2018-05-02 21:01:29 +00:00
|
|
|
var user = await repository.users.get(code.userId);
|
|
|
|
var name = user !== null ? user.name : null;
|
2018-04-27 15:13:18 +00:00
|
|
|
|
2018-05-02 21:01:29 +00:00
|
|
|
info.message = {
|
|
|
|
name: name,
|
|
|
|
message: code.messageHTML
|
|
|
|
};
|
|
|
|
}
|
2018-04-27 15:13:18 +00:00
|
|
|
|
2018-05-02 21:01:29 +00:00
|
|
|
res.send(info);
|
2018-04-27 15:13:18 +00:00
|
|
|
}));
|
|
|
|
|
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
|
|
|
{
|
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-08-16 13:18:02 +00:00
|
|
|
let expirationDate = await repository.codes.getUploadExpirationDate(decoded.codeId);
|
|
|
|
|
2018-05-13 07:33:10 +00:00
|
|
|
var uploadId = await repository.uploads.insert({
|
|
|
|
userId: decoded.codeUserId,
|
|
|
|
codeId: decoded.codeId,
|
|
|
|
files: req.body.files,
|
2018-08-16 13:18:02 +00:00
|
|
|
expirationDate: expirationDate
|
2018-05-13 07:33:10 +00:00
|
|
|
});
|
2018-05-02 21:01:29 +00:00
|
|
|
|
2018-04-28 13:00:30 +00:00
|
|
|
await repository.notifications.insert({
|
|
|
|
userId: decoded.codeUserId,
|
2018-05-13 07:33:10 +00:00
|
|
|
uploadId: uploadId,
|
|
|
|
type: NotificationType.UploadComplete
|
2018-04-28 13:00:30 +00:00
|
|
|
});
|
|
|
|
|
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
|
|
|
}
|