Add verbose logging to download.DownloadAudioFiles
This commit is contained in:
@@ -49,6 +49,8 @@ func ensureDir(path string) error {
|
||||
func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string, destDir string) {
|
||||
defer close(progressChan)
|
||||
|
||||
logger.Info("Starting downloads")
|
||||
|
||||
if err := ensureDir(destDir); err != nil {
|
||||
logger.Error("failed to ensureDir %s %v", destDir, err)
|
||||
progressChan <- DownloadStatus{
|
||||
@@ -65,11 +67,13 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
totalDownloaded := int64(0)
|
||||
|
||||
for _, filename := range filenames {
|
||||
logger.Info("Downloading %s", filename)
|
||||
destPath := filepath.Join(destDir, filename)
|
||||
// Check if the file already exists
|
||||
url := urlBase + filename
|
||||
resp, err := http.Head(url)
|
||||
if err != nil {
|
||||
logger.Warning("Failed to get size for %s: %v", filename, err)
|
||||
progressChan <- DownloadStatus{
|
||||
Msg: fmt.Sprintf("Failed to get size for %s: %v", filename, err),
|
||||
Err: err,
|
||||
@@ -77,6 +81,7 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
return
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
logger.Error("Failed to get size for %s: status %s", filename, resp.Status)
|
||||
progressChan <- DownloadStatus{
|
||||
Msg: fmt.Sprintf("Failed to get size for %s: status %s", filename, resp.Status),
|
||||
Err: fmt.Errorf("bad status: %s", resp.Status),
|
||||
@@ -86,9 +91,9 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
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
|
||||
// log a warning if the file is not complete
|
||||
if !os.IsNotExist(err) {
|
||||
logger.Warning("%s incomplete/corrupt. Adding to download queue.", filename)
|
||||
logger.Warning("%s corrupt. Adding to download queue.", filename)
|
||||
} else {
|
||||
logger.Info("%s not found. Adding to download queue.", filename)
|
||||
}
|
||||
@@ -124,6 +129,7 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
// Create the destination file
|
||||
out, err := os.Create(destPath)
|
||||
if err != nil {
|
||||
logger.Error("Failed to create file %s: %v", destPath, err)
|
||||
progressChan <- DownloadStatus{
|
||||
Msg: fmt.Sprintf("Failed to create file %s: %v", destPath, err),
|
||||
Err: err,
|
||||
@@ -136,6 +142,7 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
if err != nil {
|
||||
out.Close()
|
||||
os.Remove(destPath)
|
||||
logger.Error("Failed to download %s: %v", filename, err)
|
||||
progressChan <- DownloadStatus{
|
||||
Msg: fmt.Sprintf("Failed to download %s: %v", filename, err),
|
||||
Err: err,
|
||||
@@ -147,6 +154,7 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
out.Close()
|
||||
os.Remove(destPath)
|
||||
logger.Error("Failed to download %s: status %s", filename, resp.Status)
|
||||
progressChan <- DownloadStatus{
|
||||
Msg: fmt.Sprintf("Failed to download %s: status %s", filename, resp.Status),
|
||||
Err: fmt.Errorf("bad status: %s", resp.Status),
|
||||
@@ -161,6 +169,7 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
if err != nil && err != io.EOF {
|
||||
out.Close()
|
||||
os.Remove(destPath)
|
||||
logger.Error("Failed to read %s: %v", filename, err)
|
||||
progressChan <- DownloadStatus{
|
||||
Msg: fmt.Sprintf("Failed to read %s: %v", filename, err),
|
||||
Err: err,
|
||||
@@ -175,6 +184,7 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
if _, err := out.Write(buf[:n]); err != nil {
|
||||
out.Close()
|
||||
os.Remove(destPath)
|
||||
logger.Error("Failed to write %s: %v", filename, err)
|
||||
progressChan <- DownloadStatus{
|
||||
Msg: fmt.Sprintf("Failed to write %s: %v", filename, err),
|
||||
Err: err,
|
||||
@@ -200,4 +210,5 @@ func DownloadAudioFiles(progressChan chan<- DownloadStatus, filenames []string,
|
||||
Cur: totalDownloaded,
|
||||
Msg: "All audio downloads completed!",
|
||||
}
|
||||
logger.Info("All audio downloads complete!")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user