2018-07-26 16:54:08 -04:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const config = require('config');
|
2017-10-09 22:01:02 -04:00
|
|
|
|
2018-08-01 11:54:35 -04:00
|
|
|
const ModelPath = '../../models/';
|
2018-07-26 19:01:16 -04:00
|
|
|
const Upload = require(ModelPath + 'Upload.js');
|
2018-07-28 12:19:38 -04:00
|
|
|
|
2018-08-01 11:54:35 -04:00
|
|
|
const uploadMultipart = require('../../util/upload/multipart');
|
|
|
|
const updateStats = require('../../util/upload/stats');
|
2017-10-09 22:01:02 -04:00
|
|
|
|
2018-08-01 11:54:35 -04:00
|
|
|
const wrap = require('../../util/wrap.js');
|
2018-07-26 13:17:37 -04:00
|
|
|
|
2018-07-29 20:08:52 -04:00
|
|
|
router.post('/', uploadMultipart, wrap(async (req, res) => {
|
2018-07-26 13:17:37 -04:00
|
|
|
const upload = {
|
2018-07-29 20:08:52 -04:00
|
|
|
id: req.file.name,
|
2018-07-26 19:40:42 -04:00
|
|
|
uploader: req.username,
|
|
|
|
uploaderKey: req.key,
|
2018-07-26 13:17:37 -04:00
|
|
|
date: Date.now(),
|
|
|
|
file: req.file
|
|
|
|
};
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
Upload.create(upload),
|
|
|
|
updateStats(req)
|
|
|
|
]);
|
|
|
|
|
|
|
|
res.status(200).json({
|
2018-07-27 14:26:55 -04:00
|
|
|
message: 'File uploaded.',
|
2018-07-29 20:08:52 -04:00
|
|
|
id: req.file.name,
|
2018-07-26 17:34:47 -04:00
|
|
|
url: config.get('Server.hostname') + '/v/' + upload.id
|
2017-10-09 22:01:02 -04:00
|
|
|
});
|
2018-07-26 13:17:37 -04:00
|
|
|
}));
|
2017-10-09 22:01:02 -04:00
|
|
|
|
|
|
|
module.exports = router;
|