job manager, jobm pkg
This commit is contained in:
@@ -2,6 +2,7 @@ package filter
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
@@ -15,16 +16,14 @@ import (
|
||||
// RegisterFilters reads a directory and register filters from files within it
|
||||
func RegisterFilters(dir string) {
|
||||
files, err := ioutil.ReadDir(dir)
|
||||
if err != nil {
|
||||
logger.Log.Panicf("could not read from template filters dir '%s': %s", dir, err)
|
||||
}
|
||||
logger.Eexit(err, "could not read from template filters dir '%s'", dir)
|
||||
for _, f := range files {
|
||||
if !f.IsDir() {
|
||||
switch path.Ext(f.Name()) {
|
||||
case ".js":
|
||||
fileBase := strings.TrimSuffix(f.Name(), ".js")
|
||||
jsFile := dir + "/" + f.Name()
|
||||
logger.Log.Debugf("trying to register filter from: %s", jsFile)
|
||||
logger.D("trying to register filter from: %s", jsFile)
|
||||
/*
|
||||
jsStr, err := ioutil.ReadFile(jsFile)
|
||||
if err != nil {
|
||||
@@ -33,38 +32,31 @@ func RegisterFilters(dir string) {
|
||||
*/
|
||||
vm := motto.New()
|
||||
fn, err := vm.Run(jsFile)
|
||||
if err != nil {
|
||||
logger.Log.Panicf("error in javascript vm for '%s': %s", jsFile, err)
|
||||
}
|
||||
logger.Eexit(err, "error in javascript vm for '%s'", jsFile)
|
||||
if !fn.IsFunction() {
|
||||
logger.Log.Panicf("%s does not contain a function code", jsFile)
|
||||
logger.E("%s does not contain a function code", jsFile)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = pongo2.RegisterFilter(
|
||||
fileBase,
|
||||
func(in, param *pongo2.Value) (out *pongo2.Value, erro *pongo2.Error) {
|
||||
thisObj, _ := vm.Object("({})")
|
||||
var err error
|
||||
if mark2web.CurrentContext != nil {
|
||||
thisObj.Set("context", *mark2web.CurrentContext)
|
||||
err = thisObj.Set("context", *mark2web.CurrentContext)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
logger.Log.Panicf("could not set context as in '%s': %s", jsFile, err)
|
||||
}
|
||||
logger.Perr(err, "could not set context in '%s': %s", jsFile)
|
||||
ret, err := fn.Call(thisObj.Value(), in.Interface(), param.Interface())
|
||||
if err != nil {
|
||||
logger.Log.Panicf("error in javascript file '%s' while calling returned function: %s", jsFile, err)
|
||||
}
|
||||
logger.Eexit(err, "error in javascript file '%s' while calling returned function", jsFile)
|
||||
|
||||
retGo, err := ret.Export()
|
||||
if err != nil {
|
||||
logger.Log.Panicf("export error for '%s': %s", jsFile, err)
|
||||
}
|
||||
logger.Perr(err, "export error for '%s'", jsFile)
|
||||
return pongo2.AsValue(retGo), nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
logger.Log.Panicf("could not register filter from '%s': %s", jsFile, err)
|
||||
}
|
||||
logger.Perr(err, "could not register filter from '%s'", jsFile)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"gitbase.de/apairon/mark2web/pkg/helper"
|
||||
"gitbase.de/apairon/mark2web/pkg/jobm"
|
||||
"gitbase.de/apairon/mark2web/pkg/logger"
|
||||
"gitbase.de/apairon/mark2web/pkg/mark2web"
|
||||
"github.com/disintegration/imaging"
|
||||
@@ -168,63 +169,67 @@ func ImageProcessFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *
|
||||
if f, err := os.Stat(imgTarget); err == nil && !f.IsDir() {
|
||||
logger.Log.Noticef("skipped processing image from %s to %s, file already exists", imgSource, imgTarget)
|
||||
} else {
|
||||
mark2web.ThreadStart(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
|
||||
} 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)
|
||||
jobm.Enqueue(jobm.Job{
|
||||
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
|
||||
} 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch p.Process {
|
||||
case "resize":
|
||||
img = imaging.Resize(img, p.Width, p.Height, imaging.Lanczos)
|
||||
case "fit":
|
||||
img = imaging.Fit(img, p.Width, p.Height, imaging.Lanczos)
|
||||
case "fill":
|
||||
var anchor imaging.Anchor
|
||||
switch strings.ToLower(p.Anchor) {
|
||||
case "":
|
||||
fallthrough
|
||||
case "center":
|
||||
anchor = imaging.Center
|
||||
case "topleft":
|
||||
anchor = imaging.TopLeft
|
||||
case "top":
|
||||
anchor = imaging.Top
|
||||
case "topright":
|
||||
anchor = imaging.TopRight
|
||||
case "left":
|
||||
anchor = imaging.Left
|
||||
case "right":
|
||||
anchor = imaging.Right
|
||||
case "bottomleft":
|
||||
anchor = imaging.BottomLeft
|
||||
case "bottom":
|
||||
anchor = imaging.Bottom
|
||||
case "bottomright":
|
||||
anchor = imaging.BottomRight
|
||||
switch p.Process {
|
||||
case "resize":
|
||||
img = imaging.Resize(img, p.Width, p.Height, imaging.Lanczos)
|
||||
case "fit":
|
||||
img = imaging.Fit(img, p.Width, p.Height, imaging.Lanczos)
|
||||
case "fill":
|
||||
var anchor imaging.Anchor
|
||||
switch strings.ToLower(p.Anchor) {
|
||||
case "":
|
||||
fallthrough
|
||||
case "center":
|
||||
anchor = imaging.Center
|
||||
case "topleft":
|
||||
anchor = imaging.TopLeft
|
||||
case "top":
|
||||
anchor = imaging.Top
|
||||
case "topright":
|
||||
anchor = imaging.TopRight
|
||||
case "left":
|
||||
anchor = imaging.Left
|
||||
case "right":
|
||||
anchor = imaging.Right
|
||||
case "bottomleft":
|
||||
anchor = imaging.BottomLeft
|
||||
case "bottom":
|
||||
anchor = imaging.Bottom
|
||||
case "bottomright":
|
||||
anchor = imaging.BottomRight
|
||||
default:
|
||||
logger.Log.Panicf("filter:image_resize, unknown anchor a=%s definition", p.Anchor)
|
||||
}
|
||||
img = imaging.Fill(img, p.Width, p.Height, anchor, imaging.Lanczos)
|
||||
default:
|
||||
logger.Log.Panicf("filter:image_resize, unknown anchor a=%s definition", p.Anchor)
|
||||
logger.Log.Panicf("filter:image_resize, invalid p parameter '%s'", p.Process)
|
||||
}
|
||||
img = imaging.Fill(img, p.Width, p.Height, anchor, imaging.Lanczos)
|
||||
default:
|
||||
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))
|
||||
}
|
||||
var encodeOptions = make([]imaging.EncodeOption, 0)
|
||||
if p.Quality > 0 {
|
||||
encodeOptions = append(encodeOptions, imaging.JPEGQuality(p.Quality))
|
||||
}
|
||||
|
||||
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)
|
||||
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,
|
||||
Category: "image process",
|
||||
})
|
||||
}
|
||||
return pongo2.AsValue(mark2web.CurrentTreeNode.ResolveNavPath(p.Filename)), nil
|
||||
|
||||
Reference in New Issue
Block a user