Remove the progress bar and fix the downloader
The progress bar *might* be causing problems during the upload. So we remove it and allow the progress to be mysterious to the user Return a non-zero error code if there is a failure Make the cache directory first...
This commit is contained in:
Vendored
+13
-39
@@ -11,7 +11,6 @@ import (
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
)
|
||||
|
||||
func listBucketContents(minioClient *minio.Client, bucketName string) ([]string, error) {
|
||||
@@ -62,36 +61,22 @@ func processStdin() []IsoInfo {
|
||||
return isos
|
||||
}
|
||||
|
||||
// CustomReader wraps an io.Reader with a progress bar
|
||||
type CustomReader struct {
|
||||
Reader io.Reader
|
||||
ProgressBar *progressbar.ProgressBar
|
||||
}
|
||||
|
||||
func (r *CustomReader) Read(p []byte) (int, error) {
|
||||
n, err := r.Reader.Read(p)
|
||||
if n > 0 {
|
||||
r.ProgressBar.Add(n)
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func downloadAndUploadIso(minioClient *minio.Client, iso IsoInfo) error {
|
||||
// Create the destination file path
|
||||
destPath := "cache/" + iso.Hash + ".iso"
|
||||
// Create cache directory if it doesn't exist
|
||||
err := os.MkdirAll("cache", 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Println("Downloading " + iso.Url)
|
||||
// Download the ISO file
|
||||
resp, err := http.Get(iso.Url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
//
|
||||
// Create download progress bar
|
||||
downloadBar := progressbar.DefaultBytes(
|
||||
resp.ContentLength,
|
||||
"Downloading: "+iso.Url,
|
||||
)
|
||||
|
||||
// Create the destination file
|
||||
destFile, err := os.Create(destPath)
|
||||
@@ -100,11 +85,8 @@ func downloadAndUploadIso(minioClient *minio.Client, iso IsoInfo) error {
|
||||
}
|
||||
defer destFile.Close()
|
||||
|
||||
// Create a wrapped reader with progress bar
|
||||
reader := io.TeeReader(resp.Body, downloadBar)
|
||||
|
||||
// Copy the downloaded content to the file
|
||||
_, err = io.Copy(destFile, reader)
|
||||
_, err = io.Copy(destFile, resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -122,20 +104,9 @@ func downloadAndUploadIso(minioClient *minio.Client, iso IsoInfo) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create upload progress bar
|
||||
uploadBar := progressbar.DefaultBytes(
|
||||
fileInfo.Size(),
|
||||
"Uploading: "+destPath,
|
||||
)
|
||||
|
||||
// Create wrapped reader for upload progress
|
||||
uploadReader := &CustomReader{
|
||||
Reader: uploadFile,
|
||||
ProgressBar: uploadBar,
|
||||
}
|
||||
|
||||
log.Println("Uploading " + destPath)
|
||||
// Upload the file to MinIO
|
||||
_, err = minioClient.PutObject(context.Background(), "isos", "cache/"+iso.Hash+".iso", uploadReader, fileInfo.Size(), minio.PutObjectOptions{
|
||||
_, err = minioClient.PutObject(context.Background(), "isos", "cache/"+iso.Hash+".iso", uploadFile, fileInfo.Size(), minio.PutObjectOptions{
|
||||
ContentType: "application/x-iso9660-image",
|
||||
})
|
||||
if err != nil {
|
||||
@@ -184,7 +155,10 @@ func main() {
|
||||
}
|
||||
|
||||
if !exists {
|
||||
downloadAndUploadIso(minioClient, iso)
|
||||
if err := downloadAndUploadIso(minioClient, iso); err != nil {
|
||||
log.Println("Error downloading/uploading ISO:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user