Attempt to redownload cached files when errors are found

This commit is contained in:
comp500 2023-06-23 19:45:44 +01:00
parent dd5520e471
commit 0df199852f

View File

@ -72,20 +72,22 @@ func (d *downloadSessionInternal) StartDownloads() chan CompletedDownload {
downloads <- found downloads <- found
} }
for _, task := range d.downloadTasks { for _, task := range d.downloadTasks {
warnings := make([]error, 0)
// Get handle for mod // Get handle for mod
cacheHandle := d.cacheIndex.GetHandleFromHash(task.hashFormat, task.hash) cacheHandle := d.cacheIndex.GetHandleFromHash(task.hashFormat, task.hash)
if cacheHandle != nil { if cacheHandle != nil {
download, err := reuseExistingFile(cacheHandle, d.hashesToObtain, task.mod) download, err := reuseExistingFile(cacheHandle, d.hashesToObtain, task.mod)
if err != nil { if err != nil {
downloads <- CompletedDownload{ // Remove handle and try again
Error: err, cacheHandle.Remove()
Mod: task.mod, cacheHandle = nil
} warnings = append(warnings, fmt.Errorf("redownloading cached file: %w", err))
} else { } else {
downloads <- download downloads <- download
}
continue continue
} }
}
download, err := downloadNewFile(&task, d.cacheFolder, d.hashesToObtain, &d.cacheIndex) download, err := downloadNewFile(&task, d.cacheFolder, d.hashesToObtain, &d.cacheIndex)
if err != nil { if err != nil {
@ -94,6 +96,7 @@ func (d *downloadSessionInternal) StartDownloads() chan CompletedDownload {
Mod: task.mod, Mod: task.mod,
} }
} else { } else {
download.Warnings = warnings
downloads <- download downloads <- download
} }
} }
@ -538,6 +541,16 @@ func (h *CacheIndexHandle) UpdateIndex() (warnings []error) {
return return
} }
func (h *CacheIndexHandle) Remove() {
for hashFormat := range h.Hashes {
hashList := h.index.Hashes[hashFormat]
if h.hashIdx < len(hashList) {
h.index.Hashes[hashFormat] = slices.Delete(hashList, h.hashIdx, h.hashIdx+1)
}
}
return
}
func CreateDownloadSession(mods []*Mod, hashesToObtain []string) (DownloadSession, error) { func CreateDownloadSession(mods []*Mod, hashesToObtain []string) (DownloadSession, error) {
// Load cache index // Load cache index
cacheIndex := CacheIndex{Version: 1, Hashes: make(map[string][]string)} cacheIndex := CacheIndex{Version: 1, Hashes: make(map[string][]string)}