From 9c0d959181ec3e26c6d3a1980f38e8ff9ceb00a4 Mon Sep 17 00:00:00 2001 From: Sebastian Frank Date: Sun, 10 Mar 2019 16:36:20 +0100 Subject: [PATCH] dont read local image file if target exists --- helper/template_filters.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/helper/template_filters.go b/helper/template_filters.go index 3307137..a14b437 100644 --- a/helper/template_filters.go +++ b/helper/template_filters.go @@ -152,6 +152,12 @@ func ImageProcessFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, * if strings.HasPrefix(imgSource, "http://") || strings.HasPrefix(imgSource, "https://") { // remote file img, p.Format, err = getImageFromURL(imgSource) + if err != nil { + return nil, &pongo2.Error{ + Sender: "filter:image_resize", + OrigError: fmt.Errorf("could not open image '%s': %s", imgSource, err), + } + } // build filename if p.Filename == "" { var fBase string @@ -170,7 +176,6 @@ func ImageProcessFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, * } else { // local file imgSource = ResolveInputPath(imgSource) - img, err = imaging.Open(imgSource, imaging.AutoOrientation(true)) if p.Filename == "" { p.Filename = fmt.Sprintf( "%s_%s", @@ -179,12 +184,6 @@ func ImageProcessFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, * ) } } - if err != nil { - return nil, &pongo2.Error{ - Sender: "filter:image_resize", - OrigError: fmt.Errorf("could not open image '%s': %s", imgSource, err), - } - } imgTarget := ResolveOutputPath(p.Filename) @@ -192,6 +191,17 @@ func ImageProcessFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, * Log.Noticef("skipped processing image from %s to %s, file already exists", imgSource, imgTarget) } else { Log.Noticef("processing image from %s to %s", imgSource, imgTarget) + if strings.HasPrefix(imgSource, "http://") || strings.HasPrefix(imgSource, "https://") { + // webrequest before finding target filename, because of file format in filename + } else { + img, err = imaging.Open(imgSource, imaging.AutoOrientation(true)) + if err != nil { + return nil, &pongo2.Error{ + Sender: "filter:image_resize", + OrigError: fmt.Errorf("could not open image '%s': %s", imgSource, err), + } + } + } switch p.Process { case "resize":