-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexiftool.go
84 lines (75 loc) · 2.34 KB
/
exiftool.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
// ExiftoolProcess returns list of files successfully processed, and map of filename -> error.
func ExiftoolProcess(ctx context.Context, args []string, files []string, appConfig AppConfig, verbose bool, verbose2 bool) ([]string, map[string]error) {
var successes []string
errs := make(map[string]error)
startTime := time.Now()
for _, imgFilename := range files {
fmt.Printf("%s ...\n", imgFilename)
fullArgs := make([]string, len(args)+1)
copy(fullArgs, args)
fullArgs[len(args)] = imgFilename
if verbose2 {
fmt.Printf("%s %s\n", appConfig.ExiftoolBin, strings.Join(fullArgs, " "))
}
cmdOut, err := RunCmd(appConfig.ExiftoolBin, fullArgs)
if err != nil {
errs[imgFilename] = err
ErrPrint(ctx, errs[imgFilename])
continue
}
if verbose {
fmt.Println(cmdOut)
}
exiftoolBackupFilename := fmt.Sprintf("%s_original", imgFilename)
_, err = os.Stat(exiftoolBackupFilename)
if err != nil {
if os.IsNotExist(err) {
// backup file was not created; move on. (supports -s)
if verbose2 {
fmt.Printf("exiftool backup file '%s' does not exist; nothing to do\n", exiftoolBackupFilename)
}
} else {
ErrPrintln(ctx, "could not stat exiftool backup file '%s': %s\n", exiftoolBackupFilename, err)
}
} else {
backupsConfig, err := GetBackupConfig(imgFilename)
if err != nil {
errs[imgFilename] = fmt.Errorf("failed to get backups config: %w", err)
ErrPrint(ctx, errs[imgFilename])
continue
}
backupsPath, err := backupsConfig.PrepareBackupsDir(imgFilename, startTime)
if err != nil {
errs[imgFilename] = fmt.Errorf("failed to prepare backups folder: %w", err)
ErrPrint(ctx, errs[imgFilename])
continue
}
if backupsPath != "" {
newBackupFilePath := filepath.Join(backupsPath, filepath.Base(imgFilename))
err = os.Rename(
exiftoolBackupFilename,
newBackupFilePath,
)
if err != nil {
errs[imgFilename] = fmt.Errorf("failed to move backup file '%s' to the backups folder: %w", exiftoolBackupFilename, err)
ErrPrint(ctx, errs[imgFilename])
continue
}
if verbose2 {
fmt.Printf("Moved exiftool backup file '%s' to '%s'.", exiftoolBackupFilename, newBackupFilePath)
}
}
}
successes = append(successes, imgFilename)
}
return successes, errs
}