Skip to content

Commit efde0ea

Browse files
authored
feat(backend gallery): display download progress (#5687)
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent add8fc3 commit efde0ea

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

core/gallery/backends.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func InstallBackend(basePath string, config *GalleryBackend, downloadStatus func
4444
return fmt.Errorf("failed to create backend path %q: %v", backendPath, err)
4545
}
4646

47-
if err := oci.ExtractOCIImage(img, backendPath); err != nil {
47+
if err := oci.ExtractOCIImage(img, backendPath, downloadStatus); err != nil {
4848
return fmt.Errorf("failed to extract image %q: %v", config.URI, err)
4949
}
5050

pkg/downloader/uri.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (uri URI) DownloadFile(filePath, sha string, fileN, total int, downloadStat
256256
return fmt.Errorf("failed to get image %q: %v", url, err)
257257
}
258258

259-
return oci.ExtractOCIImage(img, filepath.Dir(filePath))
259+
return oci.ExtractOCIImage(img, filepath.Dir(filePath), downloadStatus)
260260
}
261261

262262
// Check if the file already exists

pkg/oci/image.go

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"net/http"
99
"runtime"
10+
"strconv"
1011
"strings"
1112
"syscall"
1213
"time"
@@ -59,11 +60,64 @@ var defaultRetryPredicate = func(err error) bool {
5960
return false
6061
}
6162

63+
type progressWriter struct {
64+
written int64
65+
total int64
66+
fileName string
67+
downloadStatus func(string, string, string, float64)
68+
}
69+
70+
func formatBytes(bytes int64) string {
71+
const unit = 1024
72+
if bytes < unit {
73+
return strconv.FormatInt(bytes, 10) + " B"
74+
}
75+
div, exp := int64(unit), 0
76+
for n := bytes / unit; n >= unit; n /= unit {
77+
div *= unit
78+
exp++
79+
}
80+
return fmt.Sprintf("%.1f %ciB", float64(bytes)/float64(div), "KMGTPE"[exp])
81+
}
82+
83+
func (pw *progressWriter) Write(p []byte) (int, error) {
84+
n := len(p)
85+
pw.written += int64(n)
86+
if pw.total > 0 {
87+
percentage := float64(pw.written) / float64(pw.total) * 100
88+
//log.Debug().Msgf("Downloading %s: %s/%s (%.2f%%)", pw.fileName, formatBytes(pw.written), formatBytes(pw.total), percentage)
89+
pw.downloadStatus(pw.fileName, formatBytes(pw.written), formatBytes(pw.total), percentage)
90+
} else {
91+
pw.downloadStatus(pw.fileName, formatBytes(pw.written), "", 0)
92+
}
93+
94+
return n, nil
95+
}
96+
6297
// ExtractOCIImage will extract a given targetImage into a given targetDestination
63-
func ExtractOCIImage(img v1.Image, targetDestination string) error {
64-
reader := mutate.Extract(img)
98+
func ExtractOCIImage(img v1.Image, targetDestination string, downloadStatus func(string, string, string, float64)) error {
99+
var reader io.Reader
100+
reader = mutate.Extract(img)
101+
102+
if downloadStatus != nil {
103+
var totalSize int64
104+
layers, err := img.Layers()
105+
if err != nil {
106+
return err
107+
}
108+
for _, layer := range layers {
109+
size, err := layer.Size()
110+
if err != nil {
111+
return err
112+
}
113+
totalSize += size
114+
}
115+
reader = io.TeeReader(reader, &progressWriter{total: totalSize, downloadStatus: downloadStatus})
116+
}
65117

66-
_, err := archive.Apply(context.Background(), targetDestination, reader, archive.WithNoSameOwner())
118+
_, err := archive.Apply(context.Background(),
119+
targetDestination, reader,
120+
archive.WithNoSameOwner())
67121

68122
return err
69123
}

pkg/oci/image_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var _ = Describe("OCI", func() {
3030
Expect(err).NotTo(HaveOccurred())
3131
defer os.RemoveAll(dir)
3232

33-
err = ExtractOCIImage(img, dir)
33+
err = ExtractOCIImage(img, dir, nil)
3434
Expect(err).NotTo(HaveOccurred())
3535
})
3636
})

0 commit comments

Comments
 (0)