Skip to content

Commit

Permalink
Closing response readers in several functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
mspalti committed Jun 17, 2022
1 parent 9d47484 commit 7699086
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
24 changes: 24 additions & 0 deletions app/process/dspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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}
Expand Down
15 changes: 13 additions & 2 deletions app/process/solr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
. "github.com/mspalti/ocrprocessor/err"
"github.com/mspalti/ocrprocessor/model"
"io"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 7699086

Please sign in to comment.