-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.go
159 lines (143 loc) · 3.6 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
package main
import (
"flag"
"fmt"
"github.com/rwcarlsen/goexif/exif"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// 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
}
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)
}
var timestampValue time.Time
x, err := exif.Decode(file)
if err != nil {
fmt.Printf("No EXIF data found (%s), using ModTime\n", err)
timestampValue = f.ModTime()
} else {
timestampValue, _ = x.DateTime()
}
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)
}
}
}