Commit 1b9308fa authored by Jürgen Enge's avatar Jürgen Enge
Browse files

exiftool added

parent a3e0e3e6
......@@ -26,6 +26,10 @@ type ConfigFFMPEG struct {
FFPROBE string
}
type ConfigExiftool struct {
Exiftool string
}
type ConfigHistogram struct {
Prefix string
Colormap map[string]string
......@@ -45,6 +49,12 @@ type ConfigValidateAV struct {
Timeout duration
}
type ConfigExif struct {
Prefix string
Timeout duration
Params string
}
type Config struct {
Logfile string
Loglevel string
......@@ -56,9 +66,11 @@ type Config struct {
JwtAlg []string
ImageMagick ConfigImageMagick
FFMPEG ConfigFFMPEG
Exiftool ConfigExiftool
Histogram ConfigHistogram
ValidateImage ConfigValidateImage
ValidateAV ConfigValidateAV
Exif ConfigExif
Wsl bool
}
......
......@@ -4,6 +4,7 @@ import (
"context"
"flag"
"fmt"
exif2 "gitlab.switch.ch/memoriav/memobase-2020/services/histogram/pkg/exif"
"gitlab.switch.ch/memoriav/memobase-2020/services/histogram/pkg/histogram"
"gitlab.switch.ch/memoriav/memobase-2020/services/histogram/pkg/service"
"gitlab.switch.ch/memoriav/memobase-2020/services/histogram/pkg/validate"
......@@ -69,6 +70,17 @@ func main() {
return
}
exif, err := exif2.NewExif(
config.Exiftool.Exiftool,
config.Exif.Params,
config.Exif.Timeout.Duration,
config.Wsl,
log)
if err != nil {
log.Fatalf("cannot initialize Exif: %v", err)
return
}
hist, err := histogram.NewHistogram(
config.ImageMagick.Convert,
config.Histogram.Resize,
......@@ -90,6 +102,7 @@ func main() {
config.Histogram.Prefix: hist,
config.ValidateImage.Prefix: imgval,
config.ValidateAV.Prefix: avval,
config.Exif.Prefix: exif,
},
config.Wsl,
)
......
......@@ -14,6 +14,14 @@ wsl = true # true, if executable is within linux subsystem on windows
ffmpeg = "/usr/local/bin/ffmpeg2"
ffprobe = "/usr/local/bin/ffprobe2"
[exiftool]
exiftool = "/usr/bin/exiftool"
[EXIF]
prefix = "exif"
timeout = "10s"
params = "-json [[PATH]]"
[ValidateImage]
prefix = "validateimage"
timeout = "10s"
......
package exif
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/goph/emperror"
"github.com/op/go-logging"
"gitlab.switch.ch/memoriav/memobase-2020/services/histogram/pkg/service"
"os/exec"
"regexp"
"strings"
"time"
)
type Exif struct {
log *logging.Logger
exiftool string
timeout time.Duration
wsl bool
params []string
}
var paramregexp = regexp.MustCompile(`([a-zA-Z0-9]+:([^ ']+|'[^']+'))|([^ ']+)|'([^']+)'`)
func NewExif(exiftool string, params string, timeout time.Duration, wsl bool, log *logging.Logger) (*Exif, error) {
h := &Exif{
exiftool: exiftool,
timeout: timeout,
wsl: wsl,
log: log,
}
h.params = paramregexp.FindAllString(params, -1)
if h.params == nil {
h.params = []string{}
}
return h, nil
}
func (h *Exif) Exec(file string, args ...interface{}) (interface{}, error) {
if h.wsl {
file = service.Path2Wsl(file)
}
cmdparam := []string{}
for _, p := range h.params {
if p == "[[PATH]]" {
p = file
}
cmdparam = append(cmdparam, p)
}
cmdfile := h.exiftool
if h.wsl {
cmdparam = append([]string{cmdfile}, cmdparam...)
cmdfile = "wsl"
}
var out bytes.Buffer
out.Grow(1024 * 1024) // 1MB size
var errbuf bytes.Buffer
errbuf.Grow(1024 * 1024) // 1MB size
ctx, cancel := context.WithTimeout(context.Background(), h.timeout)
defer cancel()
cmd := exec.CommandContext(ctx, cmdfile, cmdparam...)
// cmd.Stdin = dataOut
cmd.Stdout = &out
cmd.Stderr = &errbuf
h.log.Infof("executing %v %v", cmdfile, cmdparam)
isError := false
if err := cmd.Run(); err != nil {
exiterr, ok := err.(*exec.ExitError)
if ok && exiterr.ExitCode() == 1 {
isError = true
} else {
outStr := out.String()
errstr := strings.TrimSpace(errbuf.String())
return nil, emperror.Wrapf(err, "error executing (%s %s): %v - %v", cmdfile, cmdparam, outStr, errstr)
}
}
outStr := out.String()
errstr := strings.TrimSpace(errbuf.String())
// data := out.String()
if isError {
return struct {
Status string `json:"status"`
Message string `json:"message"`
}{
Status: "error",
Message: errstr,
}, nil
}
var result interface{}
if err := json.Unmarshal([]byte(outStr), &result); err != nil {
return nil, emperror.Wrapf(err, "cannot unmarshal data - %s", outStr)
}
ilist, ok := result.([]interface{})
if ok {
if len(ilist) != 1 {
return nil, fmt.Errorf("invalid number of objects in json %v - %s", len(ilist), outStr)
}
result = ilist[0]
}
return result, nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment