Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating New Generic File Upload Controller #424

Merged

Conversation

Aman-Gupta-404
Copy link
Contributor

Working on the feature to upload files (issue #412 )

  • In Progress

@polymahh polymahh linked an issue Sep 5, 2023 that may be closed by this pull request
@salahlalami
Copy link
Member

@Aman-Gupta-404 any update , please commit your progress , and make pr as draft

@Aman-Gupta-404
Copy link
Contributor Author

Hi @salahlalami, I have created the files and the middleware for uploading the files. When I am testing the API using postman, I am getting a production issue, not sure why it is happening. Can use your help there.

Updates that are done

  1. created files according to the steps mentioned in the issue feat : Create New Generic File Upload Controller #412
  2. created an upload model in the model folder
  3. Created 3 middlewares, one to create public file models, second one to create private file models, and third to upload the file using multer.
  4. added API end points in the routes file, to upload public and private files separately.

Please do let me know if there are any issues or anything that I can update, I will do that

@salahlalami salahlalami changed the base branch from master to dev September 6, 2023 01:06
models/erpModels/Upload.js Outdated Show resolved Hide resolved
middlewares/uploadMiddleware.js Outdated Show resolved Hide resolved
models/erpModels/Upload.js Outdated Show resolved Hide resolved
if (req.upload) {
// fetching the file extention of the uploaded file
let fileExtension = file.originalname.split('.')[1];
let _fileName = `${req.upload._id}.${fileExtension}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filename = orginalFileName+'-'+uniqueFileID+.${fileExtension}`;

orginalFileName = orginalFileName -> to lower case -> then use "npmjs.com/package/transliteration " to convert english-char
uniqueFileID = unique ID of 5 chars including [a-z,0-9]

models/erpModels/Upload.js Outdated Show resolved Hide resolved
type: String,
},
removed: {
type: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type boolean
default false

@salahlalami
Copy link
Member

@Aman-Gupta-404 please review requested change ,

@Aman-Gupta-404
Copy link
Contributor Author

Hi @salahlalami, I have reviewed and made the changes to according to your request

Apart from the changes, a few things I would like to mention.

  • I have changed the order of the middlewares in the /routes/eprRoutes/eprApi.js, the multer middle ware is being used first.

Do let me know if there are any issues with this code, can work on it

middlewares/uploadMiddleware.js Outdated Show resolved Hide resolved
@@ -213,8 +213,8 @@ router.route('/offer/summary').get(catchErrors(offerController.summary));
// //____________________________________________ API for Upload controller _________________

router.route('/public/upload/:model/:fieldId').post(
createPublicUpload,
uploadMiddleware.single('upload'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aman-Gupta-404

  1. could you add option to add multiply files
  2. Add option to add allowed file , ex only image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @salahlalami, so regarding this

Can you tell me how do I make the option accessible from the backend, like should the options come from request params or from request body?
Secondly, can you tell me if a user is uploading 5 files (one file is of type image, and others of type PDF), and the user has passed the option to accept/allow only image type files, will the server reject all the other types of files and not upload it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aman-Gupta-404
1- option is pre-defin config accessible only from the backend , default is single , typeFile is image by deafult, user can't decide . this is generic upload , we will use in different route

2- same this is should have some default , and other is option config is accessible only from the backend , user can't change it

there is no request params or from request body

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, i'll start working on it

@Aman-Gupta-404
Copy link
Contributor Author

Hi @salahlalami

  1. I added the option to add multiple files [set the limit to 100 though, for safety purposes]

updated the all the functions in uploadMiddleware.js file to save multiple files to the database at once (used insertMany function of mongoose)

  1. Added the feature to check the type of file type before accepting it in the backend, and this can be changed

Here I added a filter function for multer called fileFilter to filter/check and accept only specific files only.
Here we can update _fileType array within fileFilter function present int the uploadMiddleware.js in the future to upload certain types of files only.

Do check it, and let me know if there are any issues

@salahlalami
Copy link
Member

Thank you @Aman-Gupta-404 ,
1- revert app.js
2- revert erpApi.js
3- remove yarn lock from your commit

could you record video to see how it work

@Aman-Gupta-404
Copy link
Contributor Author

Hey @salahlalami, I have resolved the conflicts and tested the code, it works fine after reverting the changes

I have also recorded the video to show how it works:

See the video by clicking here

do let me know if there are any other issues

@salahlalami
Copy link
Member

Thank you @Aman-Gupta-404 , we will merge it soon

@salahlalami salahlalami merged commit 03410db into idurar:dev Sep 11, 2023
1 check passed
@salahlalami salahlalami added good first issue Good for newcomers and removed 🚧 in progress labels Sep 18, 2023
awesomedev82 pushed a commit to awesomedev82/idurar-erp-crm that referenced this pull request May 20, 2024
…r-controller

Creating New Generic File Upload Controller
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat : Create New Generic File Upload Controller
4 participants