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

Part Downloads #133

Closed
sirinath opened this issue Apr 18, 2014 · 5 comments
Closed

Part Downloads #133

sirinath opened this issue Apr 18, 2014 · 5 comments
Assignees

Comments

@sirinath
Copy link

Is it possible to ensure partly downloaded files are not downloaded from the start.

@back2dos
Copy link
Member

Can you please elaborate on the problem?

@sirinath
Copy link
Author

If you lose your internet connection in the middle then there are times the downloads are not resumed but restarted.

@back2dos
Copy link
Member

I understand. I will look into this.

@nadako
Copy link
Member

nadako commented Dec 22, 2015

I have looked into it, and it seems quite easy to implement: we pass the Range header with bytes=xxx- value, where xxx is the current file size. And we open the file in the append mode (so File.append, not File.write.

Here's a snipped to play with. I'm not a HTTP expert, but it looks okay to me. I did ctrl+c and restarted several times and still got full zip file downloaded. If noone sees any issues, I can add that to haxelib.

import haxe.io.Output;
import sys.io.File;

class Main {
    static function main() {
        var url = "http://lib.haxe.org/files/3.0/lime-2,0,0-alpha,8.zip";
        var f = File.append("lime.zip", true);
        f.seek(0, SeekEnd);
        var h = new haxe.Http(url);
        var cur = f.tell();
        if (cur > 0)
            h.addHeader("range", "bytes="+cur + "-");
        h.customRequest(false, new Progress(f, cur));
    }
}

class Progress extends Output {
    var o : Output;
    var cursize : Int;
    var cur : Int;
    var max : Int;

    public function new(o, cursize) {
        this.o = o;
        this.cursize = cursize;
        this.cur = cursize;
    }

    function report(n) {
        cur += n;
        if( max == null )
            Sys.print(cur+" bytes\r");
        else
            Sys.print(cur+"/"+max+" ("+Std.int((cur*100.0)/max)+"%)\r");
    }

    public override function writeByte(c) {
        o.writeByte(c);
        report(1);
    }

    public override function writeBytes(s,p,l) {
        var r = o.writeBytes(s,p,l);
        report(r);
        return r;
    }

    public override function close() {
        super.close();
        o.close();
        Sys.println("Download complete : "+cur+" bytes");
    }

    public override function prepare(m:Int) {
        max = m + cursize;
    }
}

@ibilon
Copy link
Member

ibilon commented Mar 10, 2016

Looks good to me,

just have to make sure that when the complete file is downloaded but is corrupted/incorrect it's deleted in order to not get stuck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants