Skip to content
Permalink
Browse files

Call log.Fatalf() instead of panic() if an operation times out. Make …

…PDF, SVG, and TIFF support optional and defaulting to off.
  • Loading branch information
aaron42net committed Jan 2, 2020
1 parent 8ed70fa commit 694fc674ade1a441626b94d0ff3532d7e8814d33
Showing with 39 additions and 8 deletions.
  1. +6 −0 cmd/fotomat/handle.go
  2. +1 −1 cmd/fotomat/version.go
  3. +27 −2 thumbnail/options.go
  4. +2 −2 thumbnail/thumbnail.go
  5. +3 −3 thumbnail/thumbnail_test.go
@@ -12,6 +12,9 @@ import (
)

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).")
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.")
@@ -79,6 +82,9 @@ func director(req *http.Request) (thumbnail.Options, int) {
Crop: crop,
MaxQueueDuration: *maxQueueDuration,
MaxProcessingDuration: *maxProcessingDuration,
AllowPdf: *allowPdf,
AllowSvg: *allowSvg,
AllowTiff: *allowTiff,
Save: format.SaveOptions{
Lossless: *lossless,
LossyIfPhoto: *lossyIfPhoto,
@@ -2,5 +2,5 @@ package main

const (
// FotomatVersion is updated by git-hooks/pre-commit
FotomatVersion = "2.9.276"
FotomatVersion = "2.9.277"
)
@@ -46,13 +46,16 @@ type Options struct {
MaxProcessingDuration time.Duration
// Save specifies the format.SaveOptions to use when compressing the modified image.
Save format.SaveOptions
// Optional input formats
AllowPdf bool
AllowSvg bool
AllowTiff bool
}

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

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

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
}
@@ -1,7 +1,7 @@
package thumbnail

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

@@ -19,7 +19,7 @@ const (
func Thumbnail(blob []byte, o Options) ([]byte, error) {
if o.MaxProcessingDuration > 0 {
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()
}
@@ -58,7 +58,7 @@ func TestValidation(t *testing.T) {
}

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
}

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

for _, of := range []format.Format{format.Png, format.Jpeg, format.Webp} {
// 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) {
alpha := f.in == format.Svg && of != format.Jpeg
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.
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) {
alpha := f.in == format.Svg
assert.Nil(t, isSize(thumb, f.outLossless, 2, 3, alpha), "lossless: %s", f.in)

0 comments on commit 694fc67

Please sign in to comment.
You can’t perform that action at this time.