Skip to content

Commit

Permalink
feat: 🔥 improve download function
Browse files Browse the repository at this point in the history
- Add support for partial content.
- Add progress bar for monitoring download progress.
  • Loading branch information
OnCloud125252 committed Mar 31, 2024
1 parent b5d0f3d commit b769a74
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"Distro"
"Distro",
"progressbar"
]
}
22 changes: 2 additions & 20 deletions module/cache.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package module

import (
"io"
"log"
"net/http"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -37,23 +35,7 @@ func DownloadFileToCache(url string) string {
}
}

file, err := os.Create(filepath.Join(cachePath, filepath.Base(url)))
if err != nil {
log.Fatal(err)
}
defer file.Close()

resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()

_, err = io.Copy(file, resp.Body)
if err != nil {
log.Fatal(err)
}

return file.Name()
fileName := Download(url, cachePath)

return fileName
}
77 changes: 77 additions & 0 deletions module/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package module

import (
"fmt"
"log"
"path/filepath"
"time"

"github.com/cavaliergopher/grab/v3"
"github.com/gookit/color"
"github.com/k0kubun/go-ansi"
"github.com/schollz/progressbar/v3"
)

func Download(url string, destination string) string {
client := grab.NewClient()
req, _ := grab.NewRequest(destination, url)

resp := client.Do(req)
filePath := resp.Filename
fileName := filepath.Base(filePath)

fmt.Printf("Package source: ")
color.Cyanln(req.URL())
fmt.Printf("Saving as: ")
color.Cyanln(fileName)
ProgressBar(resp)

if err := resp.Err(); err != nil {
log.Fatal("Download failed")
}

return filePath
}

func ProgressBar(resp *grab.Response) {
status := ""
if resp.HTTPResponse.Status == "200 OK" {
status = "Downloading"
} else if resp.HTTPResponse.Status == "206 Partial Content" {
status = "Resuming Download"
}

bar := progressbar.NewOptions(
int(resp.Size()),
progressbar.OptionSetWriter(ansi.NewAnsiStdout()), //you should install "github.com/k0kubun/go-ansi"
progressbar.OptionEnableColorCodes(true),
progressbar.OptionSpinnerType(1),
progressbar.OptionShowBytes(true),
progressbar.OptionShowCount(),
progressbar.OptionSetDescription("[magenta]"+status+"[reset]"),
progressbar.OptionOnCompletion(func() {
color.Greenln("\nDownload complete!")
}),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[green]█[reset]",
SaucerHead: "[green]▓▒░[reset]",
SaucerPadding: " ",
BarStart: "▐",
BarEnd: "▌",
}))

t := time.NewTicker(5 * time.Millisecond)
defer t.Stop()
Loop:
for {
select {
case <-t.C:
bar.Set(int(resp.BytesComplete()))

case <-resp.Done:
bar.Set(int(resp.Size()))
fmt.Println()
break Loop
}
}
}

0 comments on commit b769a74

Please sign in to comment.