cavaliergopher / grab

A download manager package for Go
BSD 3-Clause "New" or "Revised" License
1.39k stars 150 forks source link

need callback for reading data from response #57

Open chzhuo opened 5 years ago

chzhuo commented 5 years ago

Server will return Etag in headers. The Etag is this hash value of the response's body. So When dumping body to file, i need a callback to streamy get body data to count hash value. You may want me to count hash after body has dumped.But this will reread file from local disk. And when range-get, i need the range-start, range-length.

cavaliercoder commented 5 years ago

Hey this is an interesting idea. You are right that the checksum validation currently re-reads from disk - though this is deliberate. This is for two reasons. Firstly, it validates that the transfer stream matches the expected hash; the server sent the correct data. Secondly, it validates that grab wrote the correct data to disk, without any accidental corruption when say, resuming a previous download, etc.

So if you're happy to read from disk, you can extract the ETag from Response.HTTPResponse.Header.Get("ETag") and then validate the file yourself.

Otherwise, maybe we could add something like Request.CopyTo(w io.Writer) and copy all bytes to w (your hash.Hash) as the file is downloaded.

What do you think of this?