Files
obitools4/pkg/obiutils/download.go
2025-01-29 12:38:39 +01:00

46 lines
747 B
Go

package obiutils
import (
"fmt"
"io"
"net/http"
"os"
"github.com/schollz/progressbar/v3"
)
func DownloadFile(url string, filepath string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Check server response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Create progress bar
bar := progressbar.DefaultBytes(
resp.ContentLength,
"downloading",
)
// Write the body to file while updating the progress bar
_, err = io.Copy(io.MultiWriter(out, bar), resp.Body)
if err != nil {
return err
}
return nil
}