-progress cli param for bars
This commit is contained in:
@@ -5,6 +5,9 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/gif"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
@@ -107,27 +110,18 @@ func ImageProcessFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *
|
||||
p.Quality,
|
||||
)
|
||||
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
|
||||
if u, _ := url.Parse(imgSource); u != nil {
|
||||
fBase = strings.Split(path.Base(u.Path), ".")[0]
|
||||
fBase = path.Base(u.Path)
|
||||
}
|
||||
|
||||
p.Filename = fmt.Sprintf(
|
||||
"%s_%x_%s.%s",
|
||||
"%s_%x_%s",
|
||||
filePrefix,
|
||||
md5.Sum([]byte(imgSource)),
|
||||
fBase,
|
||||
p.Format,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
@@ -173,13 +167,12 @@ func ImageProcessFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *
|
||||
Function: func() {
|
||||
logger.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
|
||||
// remote file
|
||||
img, p.Format, err = getImageFromURL(imgSource)
|
||||
} else {
|
||||
img, err = imaging.Open(imgSource, imaging.AutoOrientation(true))
|
||||
if err != nil {
|
||||
logger.Log.Panicf("filter:image_resize, could not open image '%s': %s", imgSource, err)
|
||||
}
|
||||
}
|
||||
logger.Eexit(err, "filter:image_resize, could not open image '%s'", imgSource)
|
||||
|
||||
switch p.Process {
|
||||
case "resize":
|
||||
@@ -217,18 +210,47 @@ func ImageProcessFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *
|
||||
logger.Log.Panicf("filter:image_resize, invalid p parameter '%s'", p.Process)
|
||||
}
|
||||
|
||||
var encodeOptions = make([]imaging.EncodeOption, 0)
|
||||
if p.Quality > 0 {
|
||||
encodeOptions = append(encodeOptions, imaging.JPEGQuality(p.Quality))
|
||||
if p.Format == "" {
|
||||
switch strings.ToLower(path.Ext(imgTarget)) {
|
||||
case ".jpg", ".jpeg", ".gif", ".png":
|
||||
var encodeOptions = make([]imaging.EncodeOption, 0)
|
||||
if p.Quality > 0 {
|
||||
encodeOptions = append(encodeOptions, imaging.JPEGQuality(p.Quality))
|
||||
}
|
||||
|
||||
err = imaging.Save(img, imgTarget, encodeOptions...)
|
||||
logger.Eerr(err, "filter:image_resize, could save image '%s'", imgTarget)
|
||||
default:
|
||||
logger.E("filter:image_resize, invalid filename extension for image: %s", imgTarget)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
out, err := os.Create(imgTarget)
|
||||
defer out.Close()
|
||||
|
||||
logger.Eerr(err, "filter:image_resize, could not create image file '%s'", imgTarget)
|
||||
switch p.Format {
|
||||
case "jpeg", "jpg":
|
||||
var jpegOpt *jpeg.Options
|
||||
if p.Quality > 0 {
|
||||
jpegOpt = &jpeg.Options{
|
||||
Quality: p.Quality,
|
||||
}
|
||||
}
|
||||
err = jpeg.Encode(out, img, jpegOpt)
|
||||
case "png":
|
||||
err = png.Encode(out, img)
|
||||
case "gif":
|
||||
err = gif.Encode(out, img, nil)
|
||||
default:
|
||||
logger.E("filter:image_resize, unknown format '%s' for '%s'", p.Format, imgSource)
|
||||
}
|
||||
logger.Eerr(err, "filter:image_resize, could not encode image file '%s'", imgTarget)
|
||||
}
|
||||
|
||||
err = imaging.Save(img, imgTarget, encodeOptions...)
|
||||
if err != nil {
|
||||
logger.Log.Panicf("filter:image_resize, could save image '%s': %s", imgTarget, err)
|
||||
}
|
||||
logger.Log.Noticef("finished image: %s", imgTarget)
|
||||
},
|
||||
Description: "process image " + imgSource,
|
||||
Description: imgSource,
|
||||
Category: "image process",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"gitbase.de/apairon/mark2web/pkg/jobm"
|
||||
"gitbase.de/apairon/mark2web/pkg/mark2web"
|
||||
|
||||
"github.com/flosch/pongo2"
|
||||
@@ -19,7 +20,7 @@ func TestImageProcessFilter(t *testing.T) {
|
||||
}
|
||||
|
||||
// we want to check files after function calls, so no multithreading
|
||||
mark2web.SetNumCPU(1)
|
||||
jobm.SetNumCPU(1)
|
||||
|
||||
mark2web.Config.Directories.Input = "../../test/in"
|
||||
mark2web.Config.Directories.Output = "../../test/out"
|
||||
|
||||
Reference in New Issue
Block a user