From d8423accbb0e0ec4c564a2f024d3fc376c258098 Mon Sep 17 00:00:00 2001 From: Nick White Date: Tue, 13 Oct 2020 15:55:34 +0100 Subject: Improve error handling, and ensure incomplete page downloads are removed --- cmd/iiifdownloader/main.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'cmd') 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 -- cgit v1.2.1-24-ge1ad