Skip to content

Commit

Permalink
Better receive progress for directories
Browse files Browse the repository at this point in the history
We now account for the decompression as well and not just set it to done.
  • Loading branch information
Jacalz committed Dec 27, 2021
1 parent 0251276 commit 6cc27e7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
8 changes: 4 additions & 4 deletions internal/transport/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (c *Client) NewReceive(code string, pathname chan string, progress *util.Pr
return
}

// We want both the compressed download and the extraction.
progress.Max += float64(msg.UncompressedBytes64)
// We are reading the data twice. First from msg to temp file and then from temp.
progress.Max += float64(msg.TransferBytes64)

tmp, err := ioutil.TempFile("", msg.Name+"-*.zip.tmp")
if err != nil {
Expand All @@ -113,13 +113,13 @@ func (c *Client) NewReceive(code string, pathname chan string, progress *util.Pr
return err
}

err = zip.Extract(tmp, n, path)
err = zip.Extract(util.TeeReaderAt(tmp, progress), n, path)
if err != nil {
fyne.LogError("Error on unzipping contents", err)
return err
}

// TODO: Can we update this as we extract it, instead of all at once?
// TODO: Progress sometimes stops at 99%. Is it the offset that isn't accounted for?
progress.Done()

return
Expand Down
27 changes: 27 additions & 0 deletions internal/util/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package util

import (
"io"
)

type teeReaderAt struct {
r io.ReaderAt
w io.Writer
}

// ReadAt wraps the reader and calls write on it.
func (t *teeReaderAt) ReadAt(p []byte, off int64) (int, error) {
n, err := t.r.ReadAt(p, off)
if n > 0 {
if n, err := t.w.Write(p[:n]); err != nil {
return n, err
}
}

return n, err
}

// TeeReaderAt returns a wrapped ReaderAt that writes what is being read.
func TeeReaderAt(r io.ReaderAt, w io.Writer) io.ReaderAt {
return &teeReaderAt{r, w}
}

0 comments on commit 6cc27e7

Please sign in to comment.