Downloads are working and the goroutine is reporting updates appropriately.
This commit is contained in:
@@ -6,15 +6,16 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"rhemawavetech.com/rhemawave/internal/logger"
|
||||
)
|
||||
|
||||
// DownloadStatus holds information related to downloads and is
|
||||
// the container for information returned via channels within this
|
||||
// package.
|
||||
type DownloadStatus struct {
|
||||
Min int64 // Current file's bytes downloaded
|
||||
Max int64 // Current file's total size
|
||||
Cur int64 // Overall bytes downloaded
|
||||
Max int64 // Total bytes to download
|
||||
Cur int64 // Total bytes downloaded
|
||||
Msg string // Status message
|
||||
Err error // Error, if any
|
||||
}
|
||||
@@ -25,11 +26,14 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
defer close(progressChan)
|
||||
|
||||
urlBase := "https://files.rhemawavetech.com/audio/"
|
||||
totalDownloaded := int64(0)
|
||||
totalSize := int64(0)
|
||||
|
||||
// First, get the total size of all files
|
||||
// First, calculate the total size of files that need to be downloaded
|
||||
totalSize := int64(0)
|
||||
filesToDownload := []string{}
|
||||
|
||||
for _, filename := range filenames {
|
||||
destPath := filepath.Join(destDir, filename)
|
||||
// Check if the file already exists
|
||||
url := urlBase + filename
|
||||
resp, err := http.Head(url)
|
||||
if err != nil {
|
||||
@@ -39,7 +43,6 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
}
|
||||
return
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
progressChan <- DownloadStatus{
|
||||
Msg: fmt.Sprintf("Failed to get size for %s: status %s", filename, resp.Status),
|
||||
@@ -47,19 +50,39 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
}
|
||||
return
|
||||
}
|
||||
totalSize += resp.ContentLength
|
||||
fileStat, err := os.Stat(destPath)
|
||||
// If the file is incomplete, or does not exist
|
||||
if os.IsNotExist(err) || fileStat.Size() != resp.ContentLength {
|
||||
// Oh, and log a warning if the file is not complete
|
||||
if !os.IsNotExist(err) {
|
||||
logger.Warning("%s incomplete/corrupt. Adding to download queue.", filename)
|
||||
} else {
|
||||
logger.Info("%s not found. Adding to download queue.", filename)
|
||||
}
|
||||
|
||||
// Append it to the list to download
|
||||
filesToDownload = append(filesToDownload, filename)
|
||||
// And increase the total bytes to download
|
||||
totalSize += resp.ContentLength
|
||||
} else if err != nil {
|
||||
logger.Error("unknown error: %v", err)
|
||||
} else {
|
||||
logger.Error("File %s exists, skipping...", filename)
|
||||
}
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
// Send initial status
|
||||
progressChan <- DownloadStatus{
|
||||
Min: 0,
|
||||
Max: totalSize,
|
||||
Cur: 0,
|
||||
Msg: "Starting download...",
|
||||
}
|
||||
|
||||
// Download each file
|
||||
for _, filename := range filenames {
|
||||
totalDownloaded := int64(0)
|
||||
|
||||
// Download each file that doesn't exist
|
||||
for _, filename := range filesToDownload {
|
||||
url := urlBase + filename
|
||||
destPath := filepath.Join(destDir, filename)
|
||||
|
||||
@@ -96,10 +119,6 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
return
|
||||
}
|
||||
|
||||
// Track progress for this file
|
||||
fileSize := resp.ContentLength
|
||||
downloaded := int64(0)
|
||||
|
||||
// Use a buffered reader for efficiency
|
||||
buf := make([]byte, 8192)
|
||||
for {
|
||||
@@ -129,13 +148,11 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
}
|
||||
|
||||
// Update progress
|
||||
downloaded += int64(n)
|
||||
totalDownloaded += int64(n)
|
||||
progressChan <- DownloadStatus{
|
||||
Min: downloaded,
|
||||
Max: fileSize,
|
||||
Max: totalSize,
|
||||
Cur: totalDownloaded,
|
||||
Msg: fmt.Sprintf("Downloading %s...", filename),
|
||||
Msg: fmt.Sprintf("%s", filename),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,8 +160,7 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
|
||||
// Send completion for this file
|
||||
progressChan <- DownloadStatus{
|
||||
Min: fileSize,
|
||||
Max: fileSize,
|
||||
Max: totalSize,
|
||||
Cur: totalDownloaded,
|
||||
Msg: fmt.Sprintf("Finished downloading %s", filename),
|
||||
}
|
||||
@@ -152,9 +168,8 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
|
||||
// Send final status
|
||||
progressChan <- DownloadStatus{
|
||||
Min: totalSize,
|
||||
Max: totalSize,
|
||||
Cur: totalDownloaded,
|
||||
Msg: "All downloads completed!",
|
||||
Msg: "All audio downloads completed!",
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user