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

Res to rename file #620

Closed
Enishowk opened this issue Jul 11, 2018 · 4 comments
Closed

Res to rename file #620

Enishowk opened this issue Jul 11, 2018 · 4 comments

Comments

@Enishowk
Copy link

Hello,

I have a question about Multer. Is it possible to retrieve the Response object from Express in the file rename function?

I use this route here: app.post("/companies/upload", ensureAuthenticated, upload, CompanyController.upload);

I have a first Middleware (ensureAuthenticated) that decrypts the token, and returns the user ID in the object res.locals.token.
So, i want to rename the file with the id of the user or his company and so, to make a request before renaming it.

Like this for example:

var storage = multer.diskStorage({
  filename: function (req, file, cb) {
    // Get id but RES is not available.
   const {id} = res.locals.token
    // Do the request, for example : 
    const user = await userRepository.findOne({where: { id: sub }, relations: ["company"]});
    cb(null, user.company.id) 
  }
})

Can i do that (pass the res object) ? Or is it possible to rename the file AFTER in the next middleware ?
Or the only way is to pass company.id from front in file object ?

Thanks. 😄

@LinusU
Copy link
Member

LinusU commented Jul 11, 2018

I think you can access the Response on req.res

@Enishowk
Copy link
Author

Enishowk commented Jul 11, 2018

Yes, it's good ! Thank you very much !

So, I have another question. If upload is success but after i have a problem with insert name in database, its possible to delete the file upload with multer ? Of i must do that with FS in node ? :)

Sorry, it's a little bit off topic but i can't find this option in documentation and i think it's necessary.

@LinusU
Copy link
Member

LinusU commented Jul 11, 2018

You need to do that manually with FS :)

@Enishowk
Copy link
Author

Thanks :) I close it.

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

No branches or pull requests

2 participants