Skip to content

Commit

Permalink
Bring back update download progress (#8934)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlaaad committed May 15, 2024
1 parent dad9830 commit c992588
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions editor/src/clj/util/net.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@

(ns util.net
(:require [clojure.java.io :as io])
(:import
(java.net URI URL HttpURLConnection)))

(import '(java.net HttpURLConnection))
(:import [java.net HttpURLConnection]))

(set! *warn-on-reflection* true)

Expand All @@ -34,31 +31,30 @@
connect-timeout default-connect-timeout
chunk-size 0
cancelled-derefable default-cancelled-derefable
progress-callback default-progress-callback}}]
progress-callback default-progress-callback}
:as args}]
(let [^HttpURLConnection conn (doto (.openConnection (io/as-url url))
(.setRequestProperty "Connection" "close")
(.setConnectTimeout connect-timeout)
(.setReadTimeout read-timeout))
(.setRequestProperty "Connection" "close")
(.setConnectTimeout connect-timeout)
(.setReadTimeout read-timeout))
length (.getContentLengthLong conn)
response-code (.getResponseCode conn)]

(case response-code
301 (let [location (.getHeaderField conn "Location")]
(println (format "moved permanently '%s' -> '%s'" url location))
(download! location out))
200 (when-not @cancelled-derefable
(with-open [input (.getInputStream conn)
output (io/output-stream out)]
(if (pos? chunk-size)
(let [buf (byte-array chunk-size)]
(loop [count (.read input buf)
previous 0]
(when (and (<= 0 count) (not @cancelled-derefable))
(.write output buf 0 count)
(progress-callback (+ previous count) length)
(recur (.read input buf) (+ previous count)))))
(do
;; io/copy will use an internal buffer of 1024 bytes for transfer
(io/copy input output)
(when (not @cancelled-derefable)
(progress-callback length length)))))))))
301 (let [location (.getHeaderField conn "Location")]
(apply download! location out (mapcat identity args)))
200 (when-not @cancelled-derefable
(with-open [input (.getInputStream conn)
output (io/output-stream out)]
(if (pos? chunk-size)
(let [buf (byte-array chunk-size)]
(loop [count (.read input buf)
previous 0]
(when (and (<= 0 count) (not @cancelled-derefable))
(.write output buf 0 count)
(progress-callback (+ previous count) length)
(recur (.read input buf) (+ previous count)))))
(do
;; io/copy will use an internal buffer of 1024 bytes for transfer
(io/copy input output)
(when (not @cancelled-derefable)
(progress-callback length length)))))))))

0 comments on commit c992588

Please sign in to comment.