|
15 | 15 | (defn- download!
|
16 | 16 | "Downloads from the InputStream to the OutputStream. To print progress, it
|
17 | 17 | requires the content length."
|
18 |
| - [^InputStream is ^OutputStream os content-len] |
| 18 | + [^InputStream is ^OutputStream os content-len resume] |
19 | 19 | (let [data (byte-array *download-buffer-size*)
|
20 |
| - with-print (and *verbose* (pos? content-len))] |
| 20 | + with-print (and *verbose* (pos? content-len)) |
| 21 | + resume (or resume 0)] |
21 | 22 | (loop [len (.read is data)
|
22 |
| - sum len |
| 23 | + sum (+ resume len) |
23 | 24 | bar (pr/progress-bar 100)]
|
24 | 25 | (when (Thread/interrupted) (throw (InterruptedException.)))
|
25 | 26 | (if (= len -1)
|
|
32 | 33 |
|
33 | 34 | (defn http-download!
|
34 | 35 | "Downloads from the url via HTTP/HTTPS and saves it to local as f."
|
35 |
| - [url f & {:keys [auth]}] |
| 36 | + [url f & {:keys [auth resume]}] |
36 | 37 | (let [option (merge {:as :stream}
|
37 | 38 | (if-let [{:keys [type user password]} auth]
|
38 |
| - {(keyword (str (name type) "-auth")) [user password]})) |
| 39 | + {(keyword (str (name type) "-auth")) [user password]}) |
| 40 | + (when resume |
| 41 | + {:headers {:range (str "bytes=" resume "-")}})) |
39 | 42 | response (client/get url option)
|
40 | 43 | content-len (if-let [content-len (get-in response [:headers "content-length"])]
|
41 | 44 | (str->int content-len) -1)
|
42 | 45 | is (:body response)]
|
43 |
| - (with-open [os (io/output-stream f)] |
44 |
| - (download! is os content-len)))) |
| 46 | + (with-open [os (io/output-stream f :append (boolean resume))] |
| 47 | + (download! is os content-len resume)))) |
45 | 48 |
|
46 | 49 | (defn- ftp-content-len
|
47 | 50 | [^FTPClient ftp-client path]
|
|
69 | 72 | content-len (ftp-content-len client* (:path u))]
|
70 | 73 | (with-open [is ^InputStream (.retrieveFileStream client* (:path u))
|
71 | 74 | os (io/output-stream f)]
|
72 |
| - (download! is os content-len))) |
| 75 | + (download! is os content-len 0))) |
73 | 76 | (try
|
74 | 77 | (complete-pending-command client*)
|
75 | 78 | (catch java.net.SocketTimeoutException e
|
|
88 | 91 | content-len (.. entry getAttrs getSize)]
|
89 | 92 | (with-open [is (.get channel (:path u))
|
90 | 93 | os (io/output-stream f)]
|
91 |
| - (download! is os content-len))))) |
| 94 | + (download! is os content-len 0))))) |
0 commit comments