-
Notifications
You must be signed in to change notification settings - Fork 0
/
exiv_embedded.go
91 lines (83 loc) · 2.27 KB
/
exiv_embedded.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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
// Support EXIF data in embedded images
import (
"fmt"
"os/exec"
"strings"
)
type exivEmbedded struct {
file string
exif map[int]string
Exif
}
func newExivEmbedded(file string, buf []byte) (Exif, error) {
e := &exivEmbedded{file: file, exif: map[int]string{}}
cmd := exec.Command("exiv2", "-q", "-P", "EkIXv", "-K", "Xmp.xmp.Rating",
"-K", "Iptc.Application2.Caption",
"-K", "Exif.Image.Orientation",
"-K", "Iptc.Application2.Headline",
"-K", "Iptc.Application2.ObjectName",
file)
outp, err := cmd.Output()
if *verbose {
fmt.Printf("Running: %s\noutput: %s\n", strings.Join(cmd.Args, " "), outp)
}
if err != nil {
// No exif in file.
return e, nil
}
e.exif = readExif(e.file, string(outp))
return e, nil
}
func (e *exivEmbedded) Set(tag int, value string) error {
etag, ok := exivToSet[tag]
if !ok {
return fmt.Errorf("Unknown EXIF tag: %d", tag)
}
cmd := exec.Command("exiv2", "-q")
cmd.Args = append(cmd.Args, fmt.Sprintf("-Mset %s %s", etag, value))
cmd.Args = append(cmd.Args, e.file)
if *verbose {
fmt.Printf("Running: %s\n", strings.Join(cmd.Args, " "))
}
if err := cmd.Run(); err != nil {
return err
}
// Update local copy.
e.exif[tag] = value
return nil
}
func (e *exivEmbedded) Get(tag int) (string, bool) {
val, ok := e.exif[tag]
return val, ok
}
func (e *exivEmbedded) Delete(tag int) error {
if _, ok := e.exif[tag]; !ok {
// No tag saved
return nil
}
etag, ok := exivToSet[tag]
if !ok {
return fmt.Errorf("Unknown EXIF tag: %d", tag)
}
cmd := exec.Command("exiv2", "-q", fmt.Sprintf("-Mdel %s", etag), e.file)
if *verbose {
fmt.Printf("Running: %s\n", strings.Join(cmd.Args, " "))
}
if err := cmd.Run(); err != nil {
return err
}
delete(e.exif, tag)
return nil
}