diff options
| author | eltrac <hi@eltr.ac> | 2026-10-30 11:35:45 +0800 |
|---|---|---|
| committer | eltrac <hi@eltr.ac> | 2026-10-30 11:35:45 +0800 |
| commit | dd4c7ca7850f166af4997912a3ded5639bd14a56 (patch) | |
| tree | 889994e1b5b413fc50bc7e19c49b07546e44e626 /src | |
| parent | ae0fda1ee522cf04f7b843d4cb2803435546cfb8 (diff) | |
test, downloader: tidy up downloader and add tests
- simplify (.mkdirs parent-dir) and with (io/make-parents file)
- should return false when HTTP request fails, not nil
- check if corrputed file exitsts before deleting it
- add tests
Diffstat (limited to 'src')
| -rw-r--r-- | src/weepinbell/http/downloader.clj | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/src/weepinbell/http/downloader.clj b/src/weepinbell/http/downloader.clj index 3b5bdfb..e2d0fba 100644 --- a/src/weepinbell/http/downloader.clj +++ b/src/weepinbell/http/downloader.clj @@ -7,10 +7,8 @@ "Download file to destination. Return true if success, false if failed." [url dest-path] (try - (let [file (io/file dest-path) - parent-dir (.getParentFile file)] - (when (and parent-dir (not (.exists parent-dir))) - (.mkdirs parent-dir)) + (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)) @@ -25,11 +23,13 @@ (do (timbre/warn "Download fail. Status: " status) (when-let [error (:error response)] - (timbre/error error)))))) + (timbre/error error)) + false)))) ;; when download failed - (catch Exception e + (catch Throwable e (timbre/error "Download error:" (.getMessage e)) (timbre/debug e) - (io/delete-file (io/file dest-path)) + (when (.exists (io/file dest-path)) + (io/delete-file (io/file dest-path))) false))) |
