feat: initial project commit
commit to push first working snapshot to codeberg
This commit is contained in:
commit
09b1054588
25 changed files with 1405 additions and 0 deletions
60
pkg/download/download.go
Normal file
60
pkg/download/download.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package download
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"fyne.io/fyne/v2/widget"
|
||||
)
|
||||
|
||||
func WithProgressBar(downloadURL, filePath string, progressBar *widget.ProgressBar) error {
|
||||
client := &http.Client{
|
||||
Timeout: 15 * time.Minute,
|
||||
}
|
||||
|
||||
resp, err := client.Get(downloadURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("download failed: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
file, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
contentLength := resp.ContentLength
|
||||
if contentLength <= 0 {
|
||||
return fmt.Errorf("invalid content length")
|
||||
}
|
||||
|
||||
progressBar.Max = float64(contentLength)
|
||||
|
||||
buffer := make([]byte, 4096)
|
||||
var totalBytes int64
|
||||
|
||||
for {
|
||||
n, readErr := resp.Body.Read(buffer)
|
||||
if n > 0 {
|
||||
totalBytes += int64(n)
|
||||
progressBar.SetValue(float64(totalBytes))
|
||||
|
||||
if _, writeErr := file.Write(buffer[:n]); writeErr != nil {
|
||||
return fmt.Errorf("error writing to file: %v", writeErr)
|
||||
}
|
||||
}
|
||||
|
||||
if readErr == io.EOF {
|
||||
break
|
||||
}
|
||||
if readErr != nil {
|
||||
return fmt.Errorf("download aborted: %v", readErr)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue