| 1 | module http |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | // SilentStreamingDownloader just saves the downloaded file chunks to the given path. |
| 6 | // It does *no reporting at all*. |
| 7 | // Note: the folder part of the path should already exist, and has to be writable. |
| 8 | pub struct SilentStreamingDownloader { |
| 9 | pub mut: |
| 10 | path string |
| 11 | f os.File |
| 12 | } |
| 13 | |
| 14 | // on_start is called once at the start of the download. |
| 15 | pub fn (mut d SilentStreamingDownloader) on_start(mut request Request, path string) ! { |
| 16 | d.path = path |
| 17 | d.f = os.create(path)! |
| 18 | } |
| 19 | |
| 20 | // on_chunk is called multiple times, once per chunk of received content. |
| 21 | pub fn (mut d SilentStreamingDownloader) on_chunk(request &Request, chunk []u8, already_received u64, expected u64) ! { |
| 22 | d.f.write(chunk)! |
| 23 | } |
| 24 | |
| 25 | // on_finish is called once at the end of the download. |
| 26 | pub fn (mut d SilentStreamingDownloader) on_finish(request &Request, response &Response) ! { |
| 27 | d.f.close() |
| 28 | if response.status_code == 0 && d.path.len > 0 { |
| 29 | os.rm(d.path) or {} |
| 30 | } |
| 31 | } |
| 32 | |