ibrod83 / nodejs-file-downloader

130 stars 23 forks source link

[ QUESTION ] Does nodejs-file-downloader supports downloading when there is multiple redirection ? #26

Closed TIRTAGT closed 2 years ago

TIRTAGT commented 2 years ago

I tried to download a test file, with 2 redirects that happen before the actual file is served by the server.

Example :

However, it does not work as I expected, it would not download the file after the first redirect (checking logs from server) I received an "aborted" error instead on my NodeJS console output, as I placed console.error(errors.message);.

I'm sure that I did not abort the download in anyway, so I was thinking maybe the package does not support redirects ? I tried downloading directly the file without any kind of header redirection (example was : https://example.com/storage/myfile.txt), and it works as expected !

My current code schema :

var SongDownloader = new Downloader({
    url: "https://example-url-here/myfile.txt",
    directory: `/a/location/to/store/my/file/txt/`,
    fileName: "myfile.txt"
});

SongDownloader.download().then(() => {
    // This never executes, as the download is aborted (which is the correct behavior if it's really aborted.
}).catch((errors: any) => {
    // This error block was triggered, the value of "errors.message" are "aborted".
    console.error(errors.message);
});

I'm not sure if I missed something, or redirection is not supported yet ? Any response is appreciated, thanks.

ibrod83 commented 2 years ago

Hey. Yes, the package supports redirects. I didn't manage to reproduce your problem. I've setup a test that sends a request to a local Express test server:

app.get('/redirect', (req, res) => {
    console.log('redirect')
    res.redirect(301, '/redirected')    
})

app.get('/redirected', (req, res) => {
    console.log('redirected')
    res.redirect(301, '/redirected2')    
})

app.get('/redirected2', (req, res) => {
    console.log('redirected2')
    const read = fs.createReadStream('./fixtures/Desert.jpg');
    read.pipe(res);    
})

I send a request to '/redirect', which results in the file being downloaded, and clearly 3 internal requests are made in the follow-redirects library. What status code and headers does your server emit, when it wants to redirect?

TIRTAGT commented 2 years ago

My server emits the "302" (Found) status code, and added the"Location" header.

Yeah, I'm still currently testing, and found out at few scenarios it works as expected, so I will close this issue for now until I can make a reliable reproducible example for you to test, since the error seems to be gone by itself and then came back (there is no reliable example for now)