@@ -10,6 +10,7 @@ import (
1010 "os"
1111 "path/filepath"
1212 "strings"
13+ "sync"
1314 "time"
1415 "whispering-tiger-ui/Logging"
1516 "whispering-tiger-ui/Updater"
@@ -18,7 +19,49 @@ import (
1819
1920const rootCacheFolder = ".cache"
2021
22+ // Global variables to track active downloads.
23+ var (
24+ activeDownloads = []string {}
25+ activeDownloadsMutex sync.Mutex
26+ )
27+
28+ func isDownloading (target string ) bool {
29+ activeDownloadsMutex .Lock ()
30+ defer activeDownloadsMutex .Unlock ()
31+ for _ , v := range activeDownloads {
32+ if v == target {
33+ return true
34+ }
35+ }
36+ return false
37+ }
38+
39+ func addDownload (target string ) {
40+ activeDownloadsMutex .Lock ()
41+ defer activeDownloadsMutex .Unlock ()
42+ activeDownloads = append (activeDownloads , target )
43+ }
44+
45+ func removeDownload (target string ) {
46+ activeDownloadsMutex .Lock ()
47+ defer activeDownloadsMutex .Unlock ()
48+ for i , v := range activeDownloads {
49+ if v == target {
50+ activeDownloads = append (activeDownloads [:i ], activeDownloads [i + 1 :]... )
51+ break
52+ }
53+ }
54+ }
55+
2156func DownloadFile (urls []string , targetDir string , checksum string , title string , extractFormat string ) error {
57+ // If the file is already being downloaded, skip and return.
58+ if isDownloading (targetDir ) {
59+ return fmt .Errorf ("File is already being downloaded: %s" , targetDir )
60+ }
61+ addDownload (targetDir )
62+ // Ensure removal on exit.
63+ defer removeDownload (targetDir )
64+
2265 // find active window
2366 window , _ := Utilities .GetCurrentMainWindow ("Downloading " + title )
2467
@@ -29,6 +72,15 @@ func DownloadFile(urls []string, targetDir string, checksum string, title string
2972 // get file name from download url
3073 filename := downloadUrl [strings .LastIndex (downloadUrl , "/" )+ 1 :]
3174
75+ // create downloader
76+ downloader := Updater.Download {
77+ Url : downloadUrl ,
78+ FallbackUrls : urls ,
79+ Filepath : targetDir ,
80+ ConcurrentDownloads : 4 ,
81+ ChunkSize : 15 * 1024 * 1024 , // 15 MB
82+ }
83+
3284 // create download dialog
3385 statusBar := widget .NewProgressBar ()
3486 statusBarContainer := container .NewVBox (statusBar )
@@ -73,14 +125,6 @@ func DownloadFile(urls []string, targetDir string, checksum string, title string
73125 os .MkdirAll (downloadTargetDir , 0755 )
74126 //downloadTargetFile := filepath.Join(downloadTargetDir, filename)
75127
76- // create downloader
77- downloader := Updater.Download {
78- Url : downloadUrl ,
79- FallbackUrls : urls ,
80- Filepath : targetDir ,
81- ConcurrentDownloads : 4 ,
82- ChunkSize : 15 * 1024 * 1024 , // 15 MB
83- }
84128 downloader .WriteCounter .OnProgress = func (progress , total uint64 , speed float64 ) {
85129 if int64 (total ) == - 1 {
86130 statusBarContainer .Remove (statusBar )
0 commit comments