Skip to content

Commit

Permalink
Restructured file download code
Browse files Browse the repository at this point in the history
  • Loading branch information
David authored and David committed Sep 16, 2019
1 parent 19fab07 commit 6698126
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,10 @@ func (dl *Downloader) listAvailableFiles(audioLibrary *AudioFileLibrary, referen
func (dl *Downloader) validateSoundFile(fileNameOnDisk string, fileSize int) bool {

// Check size on disk is the same as the size the api-server tells us this file should be.
file, err := os.Open(fileNameOnDisk)
fileInfo, err := os.Stat(fileNameOnDisk)
if err != nil {
return false
}

fileInfo, err := file.Stat()
if err != nil {
return false
}

if fileInfo.Size() != int64(fileSize) {
return false
}
Expand All @@ -214,10 +208,10 @@ func (dl *Downloader) removeAudioFile(audioLibrary *AudioFileLibrary, fileID int
func (dl *Downloader) downloadAudioFile(audioLibrary *AudioFileLibrary, fileID int, fileResp *api.FileResponse) {

fileNameOnDisk := MakeFileName(fileResp.File.Details.OriginalName, fileResp.File.Details.Name, fileID)
log.Printf("Processing file with id %d and name %s.", fileID, fileNameOnDisk)

log.Printf("Processing file with id %d and name %s", fileID, fileNameOnDisk)

for i := 1; i <= maxDownloadRetries; i++ {
var i int
for {
if err := dl.api.DownloadFile(fileResp, filepath.Join(dl.audioDir, fileNameOnDisk)); err != nil {
log.Printf("Error dowloading sound file id %d and name %s. Error is %s.", fileID, fileNameOnDisk, err)
} else {
Expand All @@ -229,9 +223,12 @@ func (dl *Downloader) downloadAudioFile(audioLibrary *AudioFileLibrary, fileID i
log.Printf("File with ID %d and name %s is not valid. Removing from disk.", fileID, fileNameOnDisk)
dl.removeAudioFile(audioLibrary, fileID, filepath.Join(dl.audioDir, fileNameOnDisk))
}
if i < maxRetries {
if i < maxDownloadRetries {
log.Println("Trying again in", retryDownloadInterval)
time.Sleep(retryDownloadInterval)
i++
} else {
break
}
}

Expand All @@ -245,14 +242,23 @@ func (dl *Downloader) downloadAllNewFiles(audioLibrary *AudioFileLibrary, refere

for _, fileID := range referencedFiles {

fileResp, err := dl.api.GetFileDetails(fileID)
if err != nil {
log.Printf("Could not download file with id %d. Error is %s", fileID, err)
continue
var i int
for {
if fileResp, err := dl.api.GetFileDetails(fileID); err != nil {
log.Printf("Error getting file details for file with ID %d. Error is %s", fileID, err)
} else {
dl.downloadAudioFile(audioLibrary, fileID, fileResp)
break
}
if i < maxDownloadRetries {
log.Println("Trying again in", retryDownloadInterval)
time.Sleep(retryDownloadInterval)
i++
} else {
break
}
}

dl.downloadAudioFile(audioLibrary, fileID, fileResp)

}

log.Println("Downloading audio files complete.")
Expand Down

0 comments on commit 6698126

Please sign in to comment.