express icon indicating copy to clipboard operation
express copied to clipboard

res.download() Range support

Open pyrostrex opened this issue 16 years ago • 17 comments

What is this? it shows up when i try to route a download link in expressjs. Here is my code.

server.get('/download/files/*.*', function(req, res){
    var downloadPath = './files/' + path.basename(unescape(req.url));

    path.exists(downloadPath, function(exists){
        if(exists)
            res.download(downloadPath);
        else
            res.send(404);
    }); 
});

Edit: Also include the filename it trying to parse.

Zzzzzzzz ZZ Zzzzzz Zzz ZZZZ Zzzzzzz Zzzzzzzzzzz_zzzzzzzzzzz.zzzz_zz_ZZZZZZZ_Z.zzzz1.rar

Edit2: FileSize

943 MB

pyrostrex avatar Sep 18 '10 17:09 pyrostrex

hmm not sure what your exact issue is, usually I only get segfaults with cyclic require()s in node. Anyways I added ./examples/download to show you how to use that functionality, so give that a look :)

tj avatar Sep 18 '10 17:09 tj

Hmm.. maybe because the file size. This doesn't shows up when downloading a small file.

Edit2: Why don't you use createReadStream instead of readFile? Imagine 943MB was inserted to a buffer then was sent one time?. If you use createReadStream, it will be easy enough to support Range right?

pyrostrex avatar Sep 19 '10 00:09 pyrostrex

yes that would be to big. Ideally we would transfer small files with readFile(), I will create a new issue for this

tj avatar Sep 19 '10 02:09 tj

maybe what we should do, at least eventually is abstract out connect's staticProvider serving code, so we can use that with res.download() for private files. But for now I will add a "stream threshold" setting, so larger files can be streamed via sys.pump()

tj avatar Sep 20 '10 15:09 tj

Hmm... can you update the example on how to use the stream threshold setting?

Edit : I've already see the documentation and I only need to set it to true right? One more, how about range? Did express support it already? This is important to my visitors because Sometimes there will got visitors who failed to connect in the middle of the downloading process. So it will be a burden for them to start the same download again. Imagine if it stuck at the same place again and again.

pyrostrex avatar Sep 22 '10 11:09 pyrostrex

Since for smaller files readFile() is quite a bit faster and allows recovery of missing files etc, we will stream only when >= "stream threshold" which defaults to 32k at the moment, you can change this by using app.set("stream threshold", 64 * 1024) etc.

I do agree though, we will work something out. Perhaps for small files it is irrelevant since they would transfer (or re-transfer) quickly, but for those which we stream lets add Range support, I will reopen this with a new title

tj avatar Sep 22 '10 15:09 tj

yikes, ok node has a small but nasty bug (patched here http://github.com/visionmedia/node/commit/f867493b8ef4efd14aa324d8d89347602c33bbe5) preventing this from working, however I will push the Range branch up so we can hack on that

tj avatar Sep 22 '10 17:09 tj

Owh. Then I should wait until you finally "hack" it though. Since my node application are really depending on the "Range" feature. One more, about this stream threshold. Is it in bytes or kilo bytes or megabytes?

pyrostrex avatar Sep 23 '10 05:09 pyrostrex

its in bytes, so you need to do 64 * 1024 or whatever you want. I have the "Range" branch up, and can merge when the next node is released with the bugfix. Currently I only support one range though, we need to work on the multipart

tj avatar Sep 23 '10 14:09 tj

Ooo.. So this means that it will fail on using download accelerator? Dunno what do you mean by multipart :P

pyrostrex avatar Sep 24 '10 04:09 pyrostrex

the range specification mentions that you can request several ranges, and then the response must be a multipart response much like a multipart file upload would be

tj avatar Sep 24 '10 14:09 tj

I've been watching for a long time. Is there any update for this request?

pyrostrex avatar Oct 04 '10 10:10 pyrostrex

0.2.3 was just released, so I can merge this in now. Have you looked at the range branch? will merge in a bit

tj avatar Oct 04 '10 15:10 tj

merged it in. feel free to take a look and tweak as needed but should be fine

tj avatar Oct 04 '10 18:10 tj

Yep, I've looked at the range branch everyday :P. Wow, thanks man! I will try it out. I will see if anything wrong happens.

pyrostrex avatar Oct 05 '10 06:10 pyrostrex

I cant remember (will have to check) but I think the spec mentioned that if you are to support Range at all, you have to support multiple range support, which kinda sucks :s it will become quite a bit larger if that is the case

tj avatar Oct 05 '10 12:10 tj

closing for now, let me know if you come across any issues with this feature

tj avatar Oct 07 '10 13:10 tj