From 7699086cd619c2b2d0fab5134b69c0d52a9c9af1 Mon Sep 17 00:00:00 2001 From: Michael Spalti Date: Fri, 17 Jun 2022 15:55:55 -0700 Subject: [PATCH] Closing response readers in several functions. --- app/process/dspace.go | 24 ++++++++++++++++++++++++ app/process/solr.go | 15 +++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/app/process/dspace.go b/app/process/dspace.go index 23fa6f6..6dbfaa5 100644 --- a/app/process/dspace.go +++ b/app/process/dspace.go @@ -15,6 +15,12 @@ func GetManifest(host string, uuid string, log *log.Logger) ([]byte, error) { log.Println(err.Error()) return nil, err } + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + log.Printf("Unable to close DSpace Manifest response.") + } + }(resp.Body) if resp.StatusCode != 200 { errorMessage := UnProcessableEntity{CAUSE: "Could not retrieve manifest. Status: " + resp.Status} return nil, errorMessage @@ -29,6 +35,12 @@ func GetAnnotationList(id string, log *log.Logger) ([]byte, error) { log.Println(err.Error()) return nil, err } + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + log.Printf("Unable to close DSpace AnnotationList response.") + } + }(resp.Body) if resp.StatusCode != 200 { errorMessage := UnProcessableEntity{CAUSE: "Could not retrieve annotations. Status: " + resp.Status} return nil, errorMessage @@ -43,6 +55,12 @@ func GetMetsXml(url string, log *log.Logger) ([]byte, error) { log.Println(err.Error()) return nil, err } + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + log.Printf("Unable to close DSpace METS response.") + } + }(resp.Body) if resp.StatusCode != 200 { errorMessage := UnProcessableEntity{CAUSE: "Could not retrieve mets xml. Status: " + resp.Status} return nil, errorMessage @@ -56,6 +74,12 @@ func GetOcrXml(url string, log *log.Logger) ([]byte, error) { if err != nil { return nil, err } + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + log.Printf("Unable to close DSpace OCR response.") + } + }(resp.Body) if resp.StatusCode != 200 { log.Println(err.Error()) errorMessage := UnProcessableEntity{CAUSE: "Could not retrieve OCR file. Status: " + resp.Status} diff --git a/app/process/solr.go b/app/process/solr.go index dde0504..3259fac 100644 --- a/app/process/solr.go +++ b/app/process/solr.go @@ -7,6 +7,7 @@ import ( "fmt" . "github.com/mspalti/ocrprocessor/err" "github.com/mspalti/ocrprocessor/model" + "io" "io/ioutil" "log" "net/http" @@ -153,7 +154,12 @@ func PostToSolrLazyLoad(uuid *string, fileName string, altoFile *string, manifes log.Println(err.Error()) return UnProcessableEntity{CAUSE: "Solr update problem. See log."} } - defer resp.Body.Close() + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + log.Printf("Unable to close Solr response.") + } + }(resp.Body) if settings.VerboseLogging { log.Printf("Added %s to Solr index", *uuid) } @@ -183,7 +189,12 @@ func PostToSolr(uuid *string, fileName string, miniOcr *string, manifestId strin log.Println(err.Error()) return UnProcessableEntity{CAUSE: "Solr update problem. See log."} } - defer resp.Body.Close() + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + log.Printf("Unable to close Solr response.") + } + }(resp.Body) if settings.VerboseLogging { log.Printf("Added %s to Solr index", *uuid) }