Skip to content

Commit

Permalink
User defined http error for loader, upgraded imagick, added go vet
Browse files Browse the repository at this point in the history
  • Loading branch information
dooman87 committed Jun 15, 2024
1 parent 18cdf94 commit 3d764cf
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ require (
github.com/dooman87/glogi v0.0.0-20180107233622-68f3443d07f1
github.com/dooman87/kolibri v0.0.0-20170117194222-c194ff118b67
github.com/gorilla/mux v1.8.1
gopkg.in/gographics/imagick.v3 v3.5.1
gopkg.in/gographics/imagick.v3 v3.7.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
gopkg.in/gographics/imagick.v3 v3.5.1 h1:58JqK0UCx5RfvbRggF5FKuK6jHwAtTQopUxK8mzFa40=
gopkg.in/gographics/imagick.v3 v3.5.1/go.mod h1:+Q9nyA2xRZXrDyTtJ/eko+8V/5E7bWYs08ndkZp8UmA=
gopkg.in/gographics/imagick.v3 v3.7.0 h1:w8iQa58ikuqjX4l2OVML3pgqFcDMD8ywXJ9/cXa33fk=
gopkg.in/gographics/imagick.v3 v3.7.0/go.mod h1:+Q9nyA2xRZXrDyTtJ/eko+8V/5E7bWYs08ndkZp8UmA=
8 changes: 1 addition & 7 deletions img/processor/imagemagick.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,8 @@ const (
func init() {
imagick.Initialize()

mw := imagick.NewMagickWand()
// time resource limit is static and doesn't work with long-running processes, hence disabling it
err := mw.SetResourceLimit(imagick.RESOURCE_TIME, -1)
if err != nil {
img.Log.Errorf("failed to init ImageMagick, could not set resource limit: %s, exiting...", err)
os.Exit(1)
}
mw.Destroy()
imagick.SetResourceLimit(imagick.RESOURCE_TIME, 0)

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
Expand Down
48 changes: 26 additions & 22 deletions img/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,23 +196,23 @@ func (r *Service) AsIs(resp http.ResponseWriter, req *http.Request) {
result, err := r.Loader.Load(imgUrl, req.Context())

if err != nil {
http.Error(resp, fmt.Sprintf("Error reading image: '%s'", err.Error()), http.StatusInternalServerError)
sendError(resp, err)
return
} else {
if len(result.MimeType) > 0 {
resp.Header().Add("Content-Type", result.MimeType)
}
}

r.execOp(&Command{
Config: &TransformationConfig{
Src: &Image{
Id: imgUrl,
},
},
Result: result,
Resp: resp,
})
if len(result.MimeType) > 0 {
resp.Header().Add("Content-Type", result.MimeType)
}

r.execOp(&Command{
Config: &TransformationConfig{
Src: &Image{
Id: imgUrl,
},
},
Result: result,
Resp: resp,
})
}

func (r *Service) execOp(op *Command) {
Expand Down Expand Up @@ -352,14 +352,7 @@ func (r *Service) transformUrl(resp http.ResponseWriter, req *http.Request, tran

srcImage, err := r.Loader.Load(imgUrl, req.Context())
if err != nil {
var httpErr *HttpError
if errors.As(err, &httpErr) {
http.Error(resp, httpErr.Error(), httpErr.Code())
} else {
http.Error(resp, fmt.Sprintf("Error reading image: '%s'", err.Error()), http.StatusInternalServerError)
}

return
sendError(resp, err)
}
Log.Printf("Source image [%s] loaded successfully, adding to the queue\n", imgUrl)

Expand Down Expand Up @@ -387,3 +380,14 @@ func getQuality(saveDataHeader string, saveDataParam string, dppx float64) Quali

return DEFAULT
}

func sendError(resp http.ResponseWriter, err error) {
if err != nil {
var httpErr *HttpError
if errors.As(err, &httpErr) {
http.Error(resp, httpErr.Error(), httpErr.Code())
} else {
http.Error(resp, fmt.Sprintf("Error reading image: '%s'", err.Error()), http.StatusInternalServerError)
}
}
}
5 changes: 5 additions & 0 deletions img/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,11 @@ func TestService_AsIs(t *testing.T) {
ExpectedCode: http.StatusBadRequest,
Description: "Source image URL is required",
},
{
Url: fmt.Sprintf("http://localhost/img/http%%3A%%2F%%2Fsite.com/custom_error.png/asis"),
ExpectedCode: http.StatusTeapot,
Description: "Uh oh :(",
},
}

test.RunRequests(testCases)
Expand Down
4 changes: 3 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ set -e
# See https://github.blog/2022-04-18-highlights-from-git-2-36/#stricter-repository-ownership-checks

echo 'Running Tests'
go test $(go list -buildvcs=false ./... | grep -v /vendor/) -v -bench . -benchmem -race -coverprofile=coverage.txt -covermode=atomic
go test $(go list -buildvcs=false ./... | grep -v '/vendor/') -v -bench . -benchmem -race -coverprofile=coverage.txt -covermode=atomic
go test -fuzz=FuzzCalculateTargetSizeForResize -fuzztime 30s ./img/processor/internal/
go test -fuzz=FuzzCalculateTargetSizeForFit -fuzztime 30s ./img/processor/internal/
go test -fuzz=FuzzHttp_LoadImg -fuzztime 30s ./img/loader/
go test -fuzz=FuzzService_ResizeUrl -fuzztime 30s ./img/

echo 'Running go vet'
go vet $(go list -buildvcs=false ./... | grep -v '/vendor/')

0 comments on commit 3d764cf

Please sign in to comment.