mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-06-29 16:20:46 +00:00
46 lines
747 B
Go
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
|
|
}
|