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