-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
convert_sidecar_json.go
67 lines (53 loc) Β· 1.49 KB
/
convert_sidecar_json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package photoprism
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/fs"
)
// ToJson uses exiftool to export metadata to a json file.
func (c *Convert) ToJson(f *MediaFile, force bool) (jsonName string, err error) {
if f == nil {
return "", fmt.Errorf("exiftool: file is nil - you may have found a bug")
}
jsonName, err = f.ExifToolJsonName()
if err != nil {
return "", nil
}
if fs.FileExists(jsonName) {
return jsonName, nil
}
log.Debugf("exiftool: extracting metadata from %s", clean.Log(f.RootRelName()))
cmd := exec.Command(c.conf.ExifToolBin(), "-n", "-m", "-api", "LargeFileSupport", "-j", f.FileName())
// Fetch command output.
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
cmd.Env = []string{fmt.Sprintf("HOME=%s", c.conf.CmdCachePath())}
// Log exact command for debugging in trace mode.
log.Trace(cmd.String())
// Run convert command.
if err := cmd.Run(); err != nil {
if stderr.String() != "" {
return "", errors.New(stderr.String())
} else {
return "", err
}
}
// Write output to file.
if err := os.WriteFile(jsonName, []byte(out.String()), fs.ModeFile); err != nil {
return "", err
}
// Check if file exists.
if fs.FileExists(jsonName) {
log.Debugf("cache: created %s", filepath.Base(jsonName))
} else {
return "", fmt.Errorf("exiftool: failed to create %s", filepath.Base(jsonName))
}
return jsonName, err
}