File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -52,6 +52,11 @@ type HTTPRequest interface {
5252type HTTPResponse interface {
5353 GetContent () (* StringBox , error )
5454 WriteTo (path string ) error
55+ WriteToWithProgress (path string , handler HTTPResponseWriteToProgressHandler ) error
56+ }
57+
58+ type HTTPResponseWriteToProgressHandler interface {
59+ Update (progress int64 , total int64 )
5560}
5661
5762var (
@@ -239,3 +244,31 @@ func (h *httpResponse) WriteTo(path string) error {
239244 defer file .Close ()
240245 return common .Error (bufio .Copy (file , h .Body ))
241246}
247+
248+ func (h * httpResponse ) WriteToWithProgress (path string , handler HTTPResponseWriteToProgressHandler ) error {
249+ defer h .Body .Close ()
250+ file , err := os .Create (path )
251+ if err != nil {
252+ return err
253+ }
254+ defer file .Close ()
255+ return common .Error (bufio .Copy (& progressWriter {
256+ writer : file ,
257+ handler : handler ,
258+ total : h .ContentLength ,
259+ }, h .Body ))
260+ }
261+
262+ type progressWriter struct {
263+ writer io.Writer
264+ handler HTTPResponseWriteToProgressHandler
265+ total int64
266+ written int64
267+ }
268+
269+ func (w * progressWriter ) Write (p []byte ) (int , error ) {
270+ n , err := w .writer .Write (p )
271+ w .written += int64 (n )
272+ w .handler .Update (w .written , w .total )
273+ return n , err
274+ }
You can’t perform that action at this time.
0 commit comments