Skip to content

Commit

Permalink
Call log.Fatalf() instead of panic() if an operation times out. Make …
Browse files Browse the repository at this point in the history
…PDF, SVG, and TIFF support optional and defaulting to off.
  • Loading branch information
aaron42net committed Jan 2, 2020
1 parent 8ed70fa commit 694fc67
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
6 changes: 6 additions & 0 deletions cmd/fotomat/handle.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
) )


var ( var (
allowPdf = flag.Bool("allow_pdf", false, "Allow PDF as an input format")
allowSvg = flag.Bool("allow_svg", false, "Allow SVG as an input format")
allowTiff = flag.Bool("allow_tiff", false, "Allow TIFF as an input format")
fetchTimeout = flag.Duration("fetch_timeout", 30*time.Second, "How long to wait to receive original image from source (0=disable).") fetchTimeout = flag.Duration("fetch_timeout", 30*time.Second, "How long to wait to receive original image from source (0=disable).")
localImageDirectory = flag.String("local_image_directory", "", "Enable local image serving from this path (\"\"=proxy instead).") localImageDirectory = flag.String("local_image_directory", "", "Enable local image serving from this path (\"\"=proxy instead).")
lossless = flag.Bool("lossless", true, "Allow saving as PNG even without transparency.") lossless = flag.Bool("lossless", true, "Allow saving as PNG even without transparency.")
Expand Down Expand Up @@ -79,6 +82,9 @@ func director(req *http.Request) (thumbnail.Options, int) {
Crop: crop, Crop: crop,
MaxQueueDuration: *maxQueueDuration, MaxQueueDuration: *maxQueueDuration,
MaxProcessingDuration: *maxProcessingDuration, MaxProcessingDuration: *maxProcessingDuration,
AllowPdf: *allowPdf,
AllowSvg: *allowSvg,
AllowTiff: *allowTiff,
Save: format.SaveOptions{ Save: format.SaveOptions{
Lossless: *lossless, Lossless: *lossless,
LossyIfPhoto: *lossyIfPhoto, LossyIfPhoto: *lossyIfPhoto,
Expand Down
2 changes: 1 addition & 1 deletion cmd/fotomat/version.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package main


const ( const (
// FotomatVersion is updated by git-hooks/pre-commit // FotomatVersion is updated by git-hooks/pre-commit
FotomatVersion = "2.9.276" FotomatVersion = "2.9.277"
) )
29 changes: 27 additions & 2 deletions thumbnail/options.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ type Options struct {
MaxProcessingDuration time.Duration MaxProcessingDuration time.Duration
// Save specifies the format.SaveOptions to use when compressing the modified image. // Save specifies the format.SaveOptions to use when compressing the modified image.
Save format.SaveOptions Save format.SaveOptions
// Optional input formats
AllowPdf bool
AllowSvg bool
AllowTiff bool
} }


// Check verifies Options against Metadata and returns a modified // Check verifies Options against Metadata and returns a modified
// Options or an error. // Options or an error.
func (o Options) Check(m format.Metadata) (Options, error) { func (o Options) Check(m format.Metadata) (Options, error) {
// Input format must be set. if !o.allowedFormat(m) {
if m.Format == format.Unknown {
return Options{}, format.ErrUnknownFormat return Options{}, format.ErrUnknownFormat
} }


Expand Down Expand Up @@ -101,3 +104,25 @@ func (o Options) Check(m format.Metadata) (Options, error) {


return o, nil return o, nil
} }

func (o Options) allowedFormat(m format.Metadata) bool {
switch m.Format {
case format.Unknown:
// Input format must be set.
return false
case format.Pdf:
if !o.AllowPdf {
return false
}
case format.Svg:
if !o.AllowSvg {
return false
}
case format.Tiff:
if !o.AllowTiff {
return false
}
}

return true
}
4 changes: 2 additions & 2 deletions thumbnail/thumbnail.go
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,7 @@
package thumbnail package thumbnail


import ( import (
"fmt" "log"
"math" "math"
"time" "time"


Expand All @@ -19,7 +19,7 @@ const (
func Thumbnail(blob []byte, o Options) ([]byte, error) { func Thumbnail(blob []byte, o Options) ([]byte, error) {
if o.MaxProcessingDuration > 0 { if o.MaxProcessingDuration > 0 {
timer := time.AfterFunc(o.MaxProcessingDuration, func() { timer := time.AfterFunc(o.MaxProcessingDuration, func() {
panic(fmt.Sprintf("Thumbnail took longer than %v", o.MaxProcessingDuration)) log.Fatalf("Thumbnail took longer than %v", o.MaxProcessingDuration)
}) })
defer timer.Stop() defer timer.Stop()
} }
Expand Down
6 changes: 3 additions & 3 deletions thumbnail/thumbnail_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestValidation(t *testing.T) {
} }


func tryNew(filename string) error { func tryNew(filename string) error {
_, err := Thumbnail(image(filename), Options{Width: 200, Height: 200}) _, err := Thumbnail(image(filename), Options{Width: 200, Height: 200, AllowPdf: true, AllowSvg: true, AllowTiff: true})
return err return err
} }


Expand Down Expand Up @@ -213,15 +213,15 @@ func TestConversion(t *testing.T) {


for _, of := range []format.Format{format.Png, format.Jpeg, format.Webp} { for _, of := range []format.Format{format.Png, format.Jpeg, format.Webp} {
// If we ask for a specific format, it should return that. // If we ask for a specific format, it should return that.
thumb, err := Thumbnail(img, Options{Width: 1024, Height: 1024, Save: format.SaveOptions{Format: of}}) thumb, err := Thumbnail(img, Options{Width: 1024, Height: 1024, AllowPdf: true, AllowSvg: true, AllowTiff: true, Save: format.SaveOptions{Format: of}})
if assert.Nil(t, err, "formats: %s -> %s", f.in, of) { if assert.Nil(t, err, "formats: %s -> %s", f.in, of) {
alpha := f.in == format.Svg && of != format.Jpeg alpha := f.in == format.Svg && of != format.Jpeg
assert.Nil(t, isSize(thumb, of, 2, 3, alpha), "formats: %s -> %s", f.in, of) assert.Nil(t, isSize(thumb, of, 2, 3, alpha), "formats: %s -> %s", f.in, of)
} }
} }


// If we ask for lossless, it should match the outLossless format. // If we ask for lossless, it should match the outLossless format.
thumb, err := Thumbnail(img, Options{Width: 1024, Height: 1024, Save: format.SaveOptions{Lossless: true}}) thumb, err := Thumbnail(img, Options{Width: 1024, Height: 1024, AllowPdf: true, AllowSvg: true, AllowTiff: true, Save: format.SaveOptions{Lossless: true}})
if assert.Nil(t, err, "lossless: %s", f.in) { if assert.Nil(t, err, "lossless: %s", f.in) {
alpha := f.in == format.Svg alpha := f.in == format.Svg
assert.Nil(t, isSize(thumb, f.outLossless, 2, 3, alpha), "lossless: %s", f.in) assert.Nil(t, isSize(thumb, f.outLossless, 2, 3, alpha), "lossless: %s", f.in)
Expand Down

0 comments on commit 694fc67

Please sign in to comment.