add upload mutiple

master
manhtien465 2 years ago
parent c609113d8a
commit e99c8ded67
  1. BIN
      backup/9/Screen Recording 2023-03-23 at 11.04.39 PM.mov
  2. BIN
      backup/9/Screen Recording 2023-03-24 at 1.43.01 PM.mov
  3. BIN
      backup/9/Screen Recording 2023-03-26 at 8.29.13 PM.mov
  4. BIN
      public/9/1/1_1/test.docx
  5. BIN
      public/9/1/Screen Recording 2023-03-26 at 8.53.20 PM.mov
  6. BIN
      public/9/1/Screen Recording 2023-03-26 at 9.24.20 PM.mov
  7. BIN
      public/9/1/Screen Recording 2023-03-26 at 9.27.55 PM.mov
  8. BIN
      public/9/1/Screen Recording 2023-03-29 at 9.59.01 AM.mov
  9. BIN
      public/9/1/Screen Shot 2022-12-20 at 5.34.40 PM.png
  10. BIN
      public/9/1/Screen Shot 2022-12-20 at 5.35.27 PM.png
  11. BIN
      public/9/1/Screen Shot 2022-12-20 at 5.36.02 PM.png
  12. BIN
      public/9/c1a875_e21d978198ad4b9c85cf63a8d1b146cc_mv2 (1).jpeg
  13. BIN
      public/9/c1a875_e75b8c21992e4f749fed9f2bfec29872_mv2 (1).png
  14. BIN
      public/9/c1a875_e75b8c21992e4f749fed9f2bfec29872_mv2 (2).png
  15. BIN
      public/9/coinbanner.jpeg
  16. BIN
      public/9/test.docx
  17. BIN
      public/9/test/test1/test.docx
  18. BIN
      public/9/test1/test.docx
  19. BIN
      public/Stumble_Guys_0.47.3_3686_ff0525.apk
  20. 31
      src/api/controllers/v1/image.controller.js
  21. 21
      src/api/controllers/v1/path.controller.js
  22. 12
      src/api/routes/v1/image.route.js
  23. 6
      src/common/services/adapters/upload-adapter.js
  24. 5
      src/config/locales/vi.json
  25. 3
      src/config/vars.js

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 313 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 272 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 284 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 284 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -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) {

@ -1,6 +1,6 @@
// import httpStatus from 'http-status'; // import httpStatus from 'http-status';
import fs from 'fs'; import fs from 'fs';
import multer from 'multer';
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 { import {
@ -80,7 +80,7 @@ exports.update = (req, res, next) => {
console.log(cdnConfig.uri, storageConfig.uri); console.log(cdnConfig.uri, storageConfig.uri);
const oldPath = req.body.oldPath.replace(cdnConfig.uri, storageConfig.uri); const oldPath = req.body.oldPath.replace(cdnConfig.uri, storageConfig.uri);
const newPath = req.body.newPath.replace(cdnConfig.uri, storageConfig.uri); const newPath = req.body.newPath.replace(cdnConfig.uri, storageConfig.uri);
console.log('aaa', oldPath, newPath);
fs.rename(oldPath, newPath, (err) => { fs.rename(oldPath, newPath, (err) => {
if (err) { if (err) {
console.log(err); console.log(err);
@ -97,11 +97,20 @@ exports.update = (req, res, next) => {
exports.delete = (req, res, next) => { exports.delete = (req, res, next) => {
try { try {
const user = req.user;
const dir = `${storageConfig.uri_backup}/${user.id}`;
multer({ dest: `${dir}` });
const path = req.body.path.replace(cdnConfig.uri, storageConfig.uri); const path = req.body.path.replace(cdnConfig.uri, storageConfig.uri);
fs.rm(path, { recursive: true }, err => { const newpath = req.body.path.replace(cdnConfig.uri, storageConfig.uri_backup);
if (err) { console.log(path, newpath);
return res.status(400).json({ code: 400, message: 'lỗi', detail: err }); // fs.rm(path, { recursive: true }, err => {
} // if (err) {
// return res.status(400).json({ code: 400, message: 'lỗi', detail: err });
// }
// return res.json({ code: 0, message: 'success' });
// });
fs.rename(path, newpath, (err) => {
if (err) throw err;
return res.json({ code: 0, message: 'success' }); return res.json({ code: 0, message: 'success' });
}); });
return null; return null;

@ -1,13 +1,13 @@
import express from 'express'; import express from 'express';
import validate from 'express-validation'; // import validate from 'express-validation';
// import { authorize } from '../../middlewares/auth.middleware'; // import { authorize } from '../../middlewares/auth.middleware';
// import Permissions from '../../../common/utils/Permissions'; // import Permissions from '../../../common/utils/Permissions';
import { uploader } from '../../../common/services/adapters/upload-adapter'; import { uploader } from '../../../common/services/adapters/upload-adapter';
import controller from '../../controllers/v1/image.controller'; import controller from '../../controllers/v1/image.controller';
import { // import {
uploadValidation // uploadValidation
} from '../../validations/v1/image.validation'; // } from '../../validations/v1/image.validation';
const router = express.Router(); const router = express.Router();
@ -24,8 +24,8 @@ router
.route('/upload-multiple') .route('/upload-multiple')
.post( .post(
// authorize([Permissions.IMAGE_UPLOAD]), // authorize([Permissions.IMAGE_UPLOAD]),
validate(uploadValidation), // validate(uploadValidation),
uploader.array('file', 10), uploader.array('file', 100),
controller.uploadMultiple controller.uploadMultiple
); );
router router

@ -63,12 +63,12 @@ const storage = multer.diskStorage({
* setup folder follow date * setup folder follow date
*/ */
createDefaultFolder({}); createDefaultFolder({});
console.log('run in herer', req.query.path, file);
/** /**
* save image follow type * save image follow type
*/ */
const path = req.query.path; const path = req.query.path;
console.log('path', path);
// const fileName = file.originalname.includes('.') // const fileName = file.originalname.includes('.')
// ? file.originalname.slice(0, file.originalname.lastIndexOf('.')) // ? file.originalname.slice(0, file.originalname.lastIndexOf('.'))
// : file.originalname; // : file.originalname;
@ -96,7 +96,7 @@ const fileFilter = (req, file, cb) => {
const uploader = multer({ const uploader = multer({
storage, storage,
limits: { limits: {
fileSize: 1024 * 1024 * 2 // 5MB fileSize: 1024 * 1024 * 2048 // 5MB
}, },
fileFilter fileFilter
}); });

@ -1,4 +1,7 @@
{ {
"NOT_FOUND!": "NOT_FOUND!", "NOT_FOUND!": "NOT_FOUND!",
"Không tìm thấy tài khoản này!": "Không tìm thấy tài khoản này!" "Không tìm thấy tài khoản này!": "Không tìm thấy tài khoản này!",
"Unexpected field": "Unexpected field",
"File too large": "File too large",
"Invalid file!": "Invalid file!"
} }

@ -18,7 +18,8 @@ module.exports = {
uri: process.env.NODE_ENV === 'production' ? process.env.POSTGRES_URI : process.env.POSTGRES_URI_TEST uri: process.env.NODE_ENV === 'production' ? process.env.POSTGRES_URI : process.env.POSTGRES_URI_TEST
}, },
storage: { storage: {
uri: process.env.NODE_ENV === 'production' ? process.env.STORAGE_URI : process.env.DEV_STORAGE_URI uri: process.env.NODE_ENV === 'production' ? process.env.STORAGE_URI : process.env.DEV_STORAGE_URI,
uri_backup: process.env.NODE_ENV === 'production' ? process.env.STORAGE_BACKUP_URI : process.env.DEV_STORAGE_BACKUP_URI
}, },
mongo: { mongo: {
uri: process.env.NODE_ENV === 'production' ? process.env.MONGO_URI : process.env.MONGO_URI_TEST uri: process.env.NODE_ENV === 'production' ? process.env.MONGO_URI : process.env.MONGO_URI_TEST

Loading…
Cancel
Save