blob: e2d0fba74350d87faecfb35ec2a152214dede922 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
(ns weepinbell.http.downloader
(:require [taoensso.timbre :as timbre]
[clojure.java.io :as io]
[weepinbell.http.client :as client]))
(defn download-file
"Download file to destination. Return true if success, false if failed."
[url dest-path]
(try
(let [file (io/file dest-path)]
(io/make-parents file)
(let [response (client/get! url {:as :stream})
status (:status response)]
(if (and status (> status 199) (< status 300))
(do
(timbre/info "Downloading [" url "] -> [" dest-path "]")
;; write to output stream
(with-open [in (:body response)
out (io/output-stream file)]
(io/copy in out))
(timbre/info "Download success. File size:" (.length file) "bytes")
true)
(do
(timbre/warn "Download fail. Status: " status)
(when-let [error (:error response)]
(timbre/error error))
false))))
;; when download failed
(catch Throwable e
(timbre/error "Download error:" (.getMessage e))
(timbre/debug e)
(when (.exists (io/file dest-path))
(io/delete-file (io/file dest-path)))
false)))
|