-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.go
243 lines (216 loc) · 5.89 KB
/
import.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/dsoprea/go-exif/v3"
exifcommon "github.com/dsoprea/go-exif/v3/common"
)
// Copy a file from src to dst
func copyFile(src, dst string) (err error) {
sfi, err := os.Stat(src)
if err != nil {
return
}
if !sfi.Mode().IsRegular() {
// Cannot copy non-regular files (directories, symlinks, devices, etc.)
return fmt.Errorf("Non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
}
dfi, err := os.Stat(dst)
if err != nil {
if !os.IsNotExist(err) {
return
}
} else {
if !(dfi.Mode().IsRegular()) {
return fmt.Errorf("Non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
}
if os.SameFile(sfi, dfi) {
return
}
}
err = copyFileContents(src, dst, sfi.ModTime())
return
}
// Copy the contents of the file named src to the file named by dst setting the given mtime. If the
// destination file exists, all of its contents will be replaced by the contents of the source file.
func copyFileContents(src, dst string, mtime time.Time) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
// Update the timestamps
if err := os.Chtimes(dst, time.Now(), mtime); err != nil {
log.Fatal(err)
}
err = out.Sync()
return
}
// Return whether the given file or directory exists
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
// Convert an offset string to seconds
func offsetToSeconds(offset string) (int, error) {
if len(offset) != 6 {
return 0, fmt.Errorf("Invalid offset format")
}
sign := 1
if offset[0] == '-' {
sign = -1
} else if offset[0] != '+' {
return 0, fmt.Errorf("Invalid offset format")
}
hours, err := strconv.Atoi(offset[1:3])
if err != nil {
return 0, err
}
minutes, err := strconv.Atoi(offset[4:6])
if err != nil {
return 0, err
}
return sign * (hours*3600 + minutes*60), nil
}
// Find a tag in all IFDs and return the value as a string
func findTagInAllIfds(index *exif.IfdIndex, tagName string) (string, error) {
ifds := index.Ifds
for _, ifd := range ifds {
results, err := ifd.FindTagWithName(tagName)
if err == nil && len(results) > 0 {
valueRaw, err := results[0].Value()
if err != nil {
return "", err
}
return valueRaw.(string), nil
}
}
return "", fmt.Errorf("tag not found")
}
func main() {
var from, to, filter string
flag.StringVar(&from, "from", "", "Source path")
flag.StringVar(&to, "to", "", "Destination path")
flag.StringVar(&filter, "filter", "", "Optional file type filter")
var start, end uint
flag.UintVar(&start, "start", uint(0), "Start date")
flag.UintVar(&end, "end", ^uint(0), "End date")
flag.Parse()
if from == "" || to == "" {
fmt.Fprintf(os.Stderr, "Error: Need source and target directory (use '--from' and '--to')\n\n")
flag.Usage()
os.Exit(-1)
}
// Read the source directory
fmt.Printf("Importing files from %s -> %s\n", from, to)
files, err := ioutil.ReadDir(from)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
// Skip directories
if f.IsDir() {
continue
}
// Filter for file extension
ext := strings.Trim(filepath.Ext(f.Name()), ".")
if filter != "" && ext != filter {
continue
}
// Filter for EXIF DateTime if it exists, otherwise ModTime
file, err := os.Open(filepath.Join(from, f.Name()))
if err != nil {
log.Fatal(err)
}
defer file.Close()
var timestampValue time.Time
rawExif, err := exif.SearchAndExtractExifWithReader(file)
if err != nil {
fmt.Printf("%s: No EXIF data found (%s), using ModTime\n", f.Name(), err)
timestampValue = f.ModTime().In(time.UTC)
} else {
im, err := exifcommon.NewIfdMappingWithStandard()
if err != nil {
log.Fatal(err)
}
ti := exif.NewTagIndex()
_, index, err := exif.Collect(im, ti, rawExif)
if err != nil {
log.Fatal(err)
}
// Search for DateTimeOriginal tag
dateTimeString, err := findTagInAllIfds(&index, "DateTimeOriginal")
if err != nil {
fmt.Printf("%s: DateTimeOriginal not found (%s), using ModTime\n", f.Name(), err)
timestampValue = f.ModTime().In(time.UTC)
} else {
fmt.Printf("%s: DateTimeOriginal = %s\n", f.Name(), dateTimeString)
layout := "2006:01:02 15:04:05"
timestampValue, err = time.Parse(layout, dateTimeString)
if err != nil {
log.Fatal(err)
}
// Determine corresponding timezone from EXIF or use local timezone
// offsetString, err := findTagInAllIfds(&index, "OffsetTime")
// if err != nil {
// fmt.Printf("OffsetTime - tag not found (%s), using Local\n", err)
// timestampValue = timestampValue.In(time.Local)
// } else {
// fmt.Printf("OffsetTime = %s\n", offsetString)
// offsetSeconds, err := offsetToSeconds(offsetString)
// if err != nil {
// log.Fatal(err)
// }
// location := time.FixedZone("FixedZone", offsetSeconds)
// timestampValue = timestampValue.In(location)
// }
}
}
i, _ := strconv.Atoi(timestampValue.Format("20060102"))
if uint(i) < start || uint(i) > end {
continue
}
// Create folder if needed
timestamp := timestampValue.Format("2006-01-02")
folder := filepath.Join(to, timestamp+"-"+strings.ToLower(ext))
if value, err := pathExists(folder); value == false && err == nil {
fmt.Printf("Creating folder: %s\n", folder)
os.Mkdir(folder, 0755)
}
// Copy the file
fromFile := filepath.Join(from, f.Name())
toFile := filepath.Join(folder, f.Name())
fmt.Printf("Copying %s -> %s (%s)\n", fromFile, toFile, timestampValue)
err = copyFile(fromFile, toFile)
if err != nil {
fmt.Printf("Copy file failed: %q\n", err)
}
}
}