-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddcopyright.go
129 lines (110 loc) · 2.68 KB
/
addcopyright.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/urfave/cli/v2"
)
type Config struct {
ImageMagickPath string `json:"imagemagick_path"`
ExifToolPath string `json:"exiftool_path"`
Copyright string `json:"copyright"`
}
func (c *Config) readConfig(path string) (*Config, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
decoder := json.NewDecoder(file)
if err := decoder.Decode(c); err != nil {
return nil, err
}
return c, nil
}
var config Config
func main() {
app := &cli.App{
Name: "addcopyright",
Usage: "Add copyright information to an image's EXIF data",
Version: "v1.1.1",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Load configuration from `FILE`",
Value: "addcopyright.json",
},
&cli.StringFlag{
Name: "input",
Aliases: []string{"i"},
Usage: "Path to the input image file",
Required: true,
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "Path to the output image file",
Required: true,
},
},
Action: func(c *cli.Context) error {
inputFilePath := c.String("input")
outputFilePath := c.String("output")
configPath := c.String("config")
if configPath == "" {
configPath = "addcopyright.json"
}
config, err := config.readConfig(configPath)
if err != nil {
return fmt.Errorf("error reading config file: %w", err)
}
if _, err := os.Stat(outputFilePath); os.IsNotExist(err) {
if err := copyFile(inputFilePath, outputFilePath); err != nil {
return fmt.Errorf("error copying file: %w", err)
}
}
cmd := exec.Command(config.ExifToolPath, "-Copyright="+config.Copyright, "-Artist="+config.Copyright, outputFilePath)
if err := cmd.Run(); err != nil {
return fmt.Errorf("error executing exiftool: %w", err)
}
fmt.Println("EXIF data updated successfully!")
return nil
},
}
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
}
}
func copyFile(src, dst string) error {
if src == dst {
return errors.New("source and destination files are the same")
}
srcExt := strings.ToLower(filepath.Ext(src))
dstExt := strings.ToLower(filepath.Ext(dst))
if srcExt != dstExt {
cmd := exec.Command(config.ImageMagickPath, src, dst)
err := cmd.Run()
if err != nil {
return err
}
return nil
}
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
destinationFile, err := os.Create(dst)
if err != nil {
return err
}
defer destinationFile.Close()
_, err = io.Copy(destinationFile, sourceFile)
return err
}