affanshahid / multer-storage-cloudinary

A Cloudinary multer storage engine
MIT License
104 stars 18 forks source link

Version 4.0.0 Not returning response from upload.single(?) middleware, not allowing user to access file in req.file #22

Open adamoceallaigh opened 3 years ago

adamoceallaigh commented 3 years ago

Bug report for Multer-Storage-Cloudinary

Before proceeding, please update to the latest version and test if the issue persists

Describe the bug

Hi To all it may concern,

First of all, this library is a godsend, nice work everyone :)).

I just said I'd write this to make ye aware of the fact I found a bug with the latest version 4.0.0. It quite simply doesn't work as it seems to upload the file to my Cloudinary account in the default folder as a unique id, but basically it never comes out of the middleware upload.single('?', where ? is the name of your key to file being uploaded) into my route and I can never access it with req.file. Tried awaiting it and promises.all() but nothing worked. I have looked at the code and followed it through a series of files with the debugger and have identified where the problem is for ye I think. So the problem is with this function and this promise. Basically, with the latest version I was running it kept spooling and then eventually timing out as the request wasn't getting any response, I think with my limited knowledge of this, is that the promise just simply was never returned as when I did the exact same request with lower version, followed this function especially to see what the story was when it executed this and first of all it did its thing sending off a promise, but within two secs it had resolved the promise and was back in the callback function if(err != null) line and then actually, for the first time (after multiple checks of my code to think what the hell I was doing wrong) I eventually bumped down to lower version and managed to get the code to continue back to my route and also saw that the code indeed did make a new folder with the image I sent as It's meant too.

My code itself was the basic default code ye give in documentation as I really thought it was something I was doing so wanted to eliminate that problem and whittling it down to the documentation code was what I thought would be the best way to test until I debugged it. I can attach a link to the server repo down below and I will leave it on a specific branch if that would be the handiest ?.

Index.js

      CloudinaryStorage.prototype.upload = function (opts, file) {

        var _this = this;

        return new Promise(function (resolve, reject) {

            var stream = _this.cloudinary.v2.uploader.upload_stream(opts, function (err, response) {

                if (err != null)

                    return reject(err);

                return resolve(response);

            });

            file.stream.pipe(stream);

        });

    };

    return CloudinaryStorage;

Issue Type (Can be multiple)

[ ] Build - Can’t install or import the SDK [ ] Babel - Babel errors or cross-browser issues [ ] Performance - Performance issues [X] Behaviour - Functions aren’t working as expected (Such as generate URL) [ ] Documentation - Inconsistency between the docs and behaviour [ ] Incorrect Types - For typescript users who are having problems with our d.ts files [ ] Other (Specify)

Steps to reproduce

… Install the latest version of this package and set up a folder same structure as my own I guess, then set up all config options and connection methods and whatever other set up is necessary. Make a route and try to use postman to post a file to that route and ensure the key that you use in the upload.single("?") corresponds to the key you are using for the form-data in Postman request. Try to send the post request and place a debugger on your set up file and follow it through until you come to this function put a breakpoint on this function and you will observe it comes into this function, basically says leave it with me and sends back a promise. The node Js default async await code kicks into play on another thread I think waiting for the response and it starts a count and it never gets back a file by the time it gets to a certain number returns a timeout. Install version 3.0.0 with npm i multer-storage-cloudinary@3.0.0 and follow the same steps outlined above or just try to make the post request with Postman again and the same debugging breakpoints will occur but the promise will be resolved within 2 minutes and you will be taken back out of the middleware and your file will be available in req.file but also check cloudinary and the folder will have been created and the new image will be there. When doing these exact same

Versions, Libraries and Environment

Multer-Storage-Cloudinary SDK version - 4.0.0 Node - 14.5.5 NPM - 7.5.4 OS - Mac OS Big Sur - Version 11.1 MacBook Pro (13-inch, M1, 2020) Apple M1 Chip

Repository

If possible, please provide a link to a reproducible repository that showcases the problem https://github.com/Adamoc14/Gymbud-Server.git

Steps To Reproduce Issue with this code

Run 1) npm i; npm test 2) Go onto Postman or try to make a post request to http://localhost:7000/image/upload 3) Make sure to change the body of the request in Postman to form-data and leave the key equal to "uploadingImage" and pick any image that you have on your computer. 4) Observe the fact that the request always spools and never returns which results in a timeout error.

Postman Request

Screenshot 2021-03-01 at 20 33 47

I have attached the repo I'm using and placed this code on a separate branch 'multer-code-not-working' for ye 👍.

Hope this makes it easy for ye to narrow down and find the bug, any questions just reply here or give me a shout on my email here( oceallaighadam96@gmail.com ) and I'll be sure to reply

Cheers,

Adam