|
|
|
@ -5,20 +5,19 @@ import Busboy from 'busboy'; |
|
|
|
|
import fs from 'fs-extra'; |
|
|
|
|
import multer from 'multer'; |
|
|
|
|
import httpStatus from 'http-status'; |
|
|
|
|
import moment from 'moment-timezone'; |
|
|
|
|
// import moment from 'moment-timezone';
|
|
|
|
|
import { handler as ErrorHandel } from '../../middlewares/errors'; |
|
|
|
|
import ApiException from '../../../common/utils/APIException'; |
|
|
|
|
import eventBus from '../../../common/services/event-bus'; |
|
|
|
|
import Image from '../../../common/models/image.model'; |
|
|
|
|
// import eventBus from '../../../common/services/event-bus';
|
|
|
|
|
// import Image from '../../../common/models/image.model';
|
|
|
|
|
import { |
|
|
|
|
cdn as cdnConfig, |
|
|
|
|
storage as storageConfig |
|
|
|
|
} from '../../../config/vars'; |
|
|
|
|
/** storage will create folder when new date */ |
|
|
|
|
const date = new Date(); |
|
|
|
|
const year = moment(date).format('YYYY'); |
|
|
|
|
const month = moment(date).format('MM'); |
|
|
|
|
const filePath = `${storageConfig.uri}/${year}/${month}/upload`; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const filePath = `${storageConfig.uri}`; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const replaceBaseUrl = (location) => |
|
|
|
@ -58,7 +57,7 @@ exports.uploadMultiple = (req, res, next) => { |
|
|
|
|
for (let index = 0; index < req.files.length; index += 1) { |
|
|
|
|
urls.push(replaceBaseUrl(req.files[index].path)); |
|
|
|
|
/** resize image uploaded */ |
|
|
|
|
eventBus.emit(Image.Events.IMAGE_CREATED, req.files[index]); |
|
|
|
|
// eventBus.emit(Image.Events.IMAGE_CREATED, req.files[index]);
|
|
|
|
|
} |
|
|
|
|
return res.json({ urls: urls }); |
|
|
|
|
} catch (ex) { |
|
|
|
@ -68,24 +67,30 @@ exports.uploadMultiple = (req, res, next) => { |
|
|
|
|
|
|
|
|
|
exports.uploadFile = (req, res, next) => { |
|
|
|
|
try { |
|
|
|
|
console.log(req.query); |
|
|
|
|
let filename = null; |
|
|
|
|
const cfg = { highWaterMark: 2097152 }; |
|
|
|
|
const cfg = { highWaterMark: 1048576 * 2 }; // 20 mb
|
|
|
|
|
cfg.headers = req.headers; |
|
|
|
|
req.busboy = Busboy(cfg); |
|
|
|
|
multer({ dest: `${filePath}` }); |
|
|
|
|
const pathName = `${filePath}/${req.query.path}`; |
|
|
|
|
multer({ |
|
|
|
|
dest: `${filePath}`, |
|
|
|
|
limits: { |
|
|
|
|
fileSize: 1024 * 1024 * 2048 // 2048MB
|
|
|
|
|
}, }); |
|
|
|
|
req.pipe(req.busboy); // Pipe it trough busboy
|
|
|
|
|
return req.busboy.on('file', (name, file, info) => { |
|
|
|
|
filename = info.filename; |
|
|
|
|
console.log('start', filename); |
|
|
|
|
|
|
|
|
|
// Create a write stream of the new file
|
|
|
|
|
const fstream = fs.createWriteStream(path.join(filePath, filename)); |
|
|
|
|
const fstream = fs.createWriteStream(path.join(pathName, filename)); |
|
|
|
|
// Pipe it trough
|
|
|
|
|
file.pipe(fstream); |
|
|
|
|
|
|
|
|
|
// On finish of the upload
|
|
|
|
|
fstream.on('close', () => { |
|
|
|
|
console.log(`Upload of '${filename}' finished`); |
|
|
|
|
return res.json({ url: replaceBaseUrl(`${filePath}/${filename}`) }); |
|
|
|
|
return res.json({ url: replaceBaseUrl(`${pathName}/${filename}`) }); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
} catch (ex) { |
|
|
|
|