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