2022-03-05 22:57:23 +04:00
|
|
|
import { Request, Response, Router } from 'express';
|
|
|
|
import multer, { StorageEngine } from 'multer';
|
|
|
|
import mime from 'mime';
|
|
|
|
import mkdirp from 'mkdirp';
|
2022-08-24 17:05:40 +03:00
|
|
|
import Transport from '../../controllers/transport.js';
|
|
|
|
import { random16 } from '../../utils/crypto.js';
|
2022-10-03 16:23:59 +04:00
|
|
|
import appConfig from '../../utils/appConfig.js';
|
2022-03-05 22:57:23 +04:00
|
|
|
|
|
|
|
const router = Router();
|
2019-03-11 18:44:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Multer storage for uploaded files and images
|
2022-03-05 22:57:23 +04:00
|
|
|
*
|
|
|
|
* @type {StorageEngine}
|
2019-03-11 18:44:00 +03:00
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
const storage: StorageEngine = multer.diskStorage({
|
2019-03-11 18:44:00 +03:00
|
|
|
destination: (req, file, cb) => {
|
2022-09-29 06:41:24 +08:00
|
|
|
const dir: string = appConfig.uploads || 'public/uploads';
|
2019-03-11 18:44:00 +03:00
|
|
|
|
2022-03-05 22:57:23 +04:00
|
|
|
mkdirp(dir);
|
|
|
|
cb(null, dir);
|
2019-03-11 18:44:00 +03:00
|
|
|
},
|
|
|
|
filename: async (req, file, cb) => {
|
|
|
|
const filename = await random16();
|
|
|
|
|
|
|
|
cb(null, `${filename}.${mime.getExtension(file.mimetype)}`);
|
2022-03-05 22:57:23 +04:00
|
|
|
},
|
2019-03-11 18:44:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multer middleware for image uploading
|
|
|
|
*/
|
|
|
|
const imageUploader = multer({
|
2022-03-05 22:57:23 +04:00
|
|
|
storage: storage,
|
2019-03-11 18:44:00 +03:00
|
|
|
fileFilter: (req, file, cb) => {
|
|
|
|
if (!/image/.test(file.mimetype) && !/video\/mp4/.test(file.mimetype)) {
|
|
|
|
cb(null, false);
|
2022-03-05 22:57:23 +04:00
|
|
|
|
2019-03-11 18:44:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, true);
|
2022-03-05 22:57:23 +04:00
|
|
|
},
|
|
|
|
}).fields([ {
|
|
|
|
name: 'image',
|
|
|
|
maxCount: 1,
|
|
|
|
} ]);
|
2019-03-11 18:44:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Multer middleware for file uploading
|
|
|
|
*/
|
|
|
|
const fileUploader = multer({
|
2022-03-05 22:57:23 +04:00
|
|
|
storage: storage,
|
|
|
|
}).fields([ {
|
|
|
|
name: 'file',
|
|
|
|
maxCount: 1,
|
|
|
|
} ]);
|
2019-03-11 18:44:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Accepts images to upload
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
router.post('/transport/image', imageUploader, async (req: Request, res: Response) => {
|
|
|
|
const response = {
|
|
|
|
success: 0,
|
|
|
|
message: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
if (req.files === undefined) {
|
|
|
|
response.message = 'No files found';
|
|
|
|
res.status(400).json(response);
|
2019-03-11 18:44:00 +03:00
|
|
|
|
2022-03-05 22:57:23 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!('image' in req.files)) {
|
2019-03-11 18:44:00 +03:00
|
|
|
res.status(400).json(response);
|
2022-03-05 22:57:23 +04:00
|
|
|
|
2019-03-11 18:44:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:28:40 +03:00
|
|
|
const fileData = {
|
|
|
|
...req.files.image[0],
|
|
|
|
url: '/uploads/' + req.files.image[0].filename,
|
|
|
|
};
|
|
|
|
|
|
|
|
console.log(fileData);
|
|
|
|
|
2019-03-11 18:44:00 +03:00
|
|
|
try {
|
|
|
|
Object.assign(
|
|
|
|
response,
|
2022-04-22 23:28:40 +03:00
|
|
|
await Transport.save(fileData, req.body.map ? JSON.parse(req.body.map) : undefined)
|
2019-03-11 18:44:00 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
response.success = 1;
|
|
|
|
res.status(200).json(response);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).json(response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accepts files to upload
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
router.post('/transport/file', fileUploader, async (req: Request, res: Response) => {
|
|
|
|
const response = { success: 0 };
|
|
|
|
|
|
|
|
if (req.files === undefined) {
|
|
|
|
res.status(400).json(response);
|
2019-03-11 18:44:00 +03:00
|
|
|
|
2022-03-05 22:57:23 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!('file' in req.files)) {
|
2019-03-11 18:44:00 +03:00
|
|
|
res.status(400).json(response);
|
2022-03-05 22:57:23 +04:00
|
|
|
|
2019-03-11 18:44:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Object.assign(
|
|
|
|
response,
|
|
|
|
await Transport.save(req.files.file[0], req.body.map ? JSON.parse(req.body.map) : undefined)
|
|
|
|
);
|
|
|
|
|
|
|
|
response.success = 1;
|
|
|
|
res.status(200).json(response);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).json(response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accept file url to fetch
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
router.post('/transport/fetch', multer().none(), async (req: Request, res: Response) => {
|
|
|
|
const response = { success: 0 };
|
2019-03-11 18:44:00 +03:00
|
|
|
|
|
|
|
if (!req.body.url) {
|
|
|
|
res.status(400).json(response);
|
2022-03-05 22:57:23 +04:00
|
|
|
|
2019-03-11 18:44:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Object.assign(response, await Transport.fetch(req.body.url, req.body.map ? JSON.parse(req.body.map) : undefined));
|
|
|
|
|
|
|
|
response.success = 1;
|
|
|
|
res.status(200).json(response);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
res.status(500).json(response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-05 22:57:23 +04:00
|
|
|
export default router;
|