diff options
author | Nick White <git@njw.name> | 2020-10-13 15:55:34 +0100 |
---|---|---|
committer | Nick White <git@njw.name> | 2020-10-13 15:55:34 +0100 |
commit | d8423accbb0e0ec4c564a2f024d3fc376c258098 (patch) | |
tree | 4c31f6e27412fa21cceacb5821dcaa6e71b186ca | |
parent | c0396d2c280b6532d2bb4f62ca6267e35c9703f4 (diff) |
Improve error handling, and ensure incomplete page downloads are removed
-rw-r--r-- | cmd/iiifdownloader/main.go | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/cmd/iiifdownloader/main.go b/cmd/iiifdownloader/main.go index 172a496..eeb253e 100644 --- a/cmd/iiifdownloader/main.go +++ b/cmd/iiifdownloader/main.go @@ -245,6 +245,8 @@ func dlPage(bookdir, u string) error { } _, err = io.Copy(f, resp.Body) if err != nil { + _ = f.Close() + _ = os.Remove(fn) return fmt.Errorf("Error writing file %s: %v\n", fn, err) } @@ -262,11 +264,11 @@ func dlNoPgNums(bookdir, pgurlStart, pgurlEnd, pgurlAltStart, pgurlAltEnd string u := fmt.Sprintf("%s%d%s", pgurlStart, pgnum, pgurlEnd) err := dlPage(bookdir, u) - if err != nil && strings.Index(err.Error(), "Error downloading page - 404 not found") == -1 { + if err != nil && strings.Index(err.Error(), "Error downloading page - 404 not found") == 0 { fmt.Printf("Got 404, assuming end of pages, for page %d, %s\n", pgnum, u) return nil } - if err != nil && strings.Index(err.Error(), "Error downloading page") == -1 { + if err != nil && strings.Index(err.Error(), "Error downloading page") == 0 { if pgurlAltStart == "" && pgurlAltEnd == "" { return fmt.Errorf("No alternative URL to try, book failed (or ended, hopefully)") } @@ -274,9 +276,9 @@ func dlNoPgNums(bookdir, pgurlStart, pgurlEnd, pgurlAltStart, pgurlAltEnd string fmt.Printf("Trying to redownload page %d at lower quality\n", pgnum) u = fmt.Sprintf("%s%d%s", pgurlAltStart, pgnum, pgurlAltEnd) err = dlPage(bookdir, u) - if err != nil { - return fmt.Errorf("Error downloading page %d, %s: %v\n", pgnum, u, err) - } + } + if err != nil { + return fmt.Errorf("Error downloading page %d, %s: %v\n", pgnum, u, err) } // Check that the last two downloaded files aren't identical, as this |