v / vlib / net / http / download_silent_downloader.v
31 lines · 26 sloc · 947 bytes · 2c62c6d0fe270138d8905050d13502c3b5246d8c
Raw
1module http
2
3import 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.
8pub struct SilentStreamingDownloader {
9pub mut:
10 path string
11 f os.File
12}
13
14// on_start is called once at the start of the download.
15pub 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.
21pub 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.
26pub 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