-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrange.go
254 lines (235 loc) · 5.19 KB
/
arrange.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
244
245
246
247
248
249
250
251
252
253
254
package arrange
import (
"crypto/md5"
"fmt"
"image/gif"
"image/jpeg"
"image/png"
"io"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
var exts map[string]bool
func init() {
exts = map[string]bool{
// images
".jpg": true,
".jpeg": true,
".png": true,
".gif": true,
// videos
".mov": true,
".mp4": true,
".m4v": true,
".avi": true,
}
}
func mtime(path string) (time.Time, error) {
ti := time.Time{}
s, err := os.Stat(path)
if err != nil {
return ti, fmt.Errorf("failure to collect times from stat: %v", err)
}
return s.ModTime(), nil
}
// PrepOutput creates all possible content-address prefix directories.
func PrepOutput(root string) error {
for i := 0; i <= 0xff; i++ {
dirname := filepath.Join(root, "content", fmt.Sprintf("%02x", i))
if err := os.MkdirAll(dirname, 0755); err != nil {
return err
}
}
if err := os.MkdirAll(filepath.Join(root, "date"), 0755); err != nil {
return err
}
return nil
}
// Source returns sends all files that match known extensions.
func Source(root string) <-chan string {
out := make(chan string)
go func() {
err := filepath.Walk(
root,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
ext := strings.ToLower(filepath.Ext(path))
if _, ok := exts[ext]; ok {
out <- path
}
return nil
},
)
if err != nil {
log.Printf("problem during crawl: %+v", err)
}
close(out)
}()
return out
}
// Parse runs the file parser for each file on input chan, and sends results
// down output chan.
//
// Exists so that it can be called many times concurrently.
func Parse(in <-chan string) <-chan Media {
out := make(chan Media)
go func() {
for path := range in {
f, err := ParseFile(path)
if err != nil {
switch err.(type) {
case NotMedia:
log.Printf("%+v", err)
default:
log.Printf("parse error: %+v", err)
}
continue
} else {
out <- f
}
}
close(out)
}()
return out
}
// MissingLink detects if the values coming from medias is a duplicate file
// rather than a hardlink to the content store.
func MissingLink(medias <-chan Media, root string) (<-chan Media, <-chan error) {
out := make(chan Media)
errs := make(chan error)
go func() {
for m := range medias {
var d, c os.FileInfo
var err error
if d, err = os.Stat(m.Path); err != nil {
errs <- err
}
if c, err = os.Stat(m.Content(root)); err != nil {
errs <- err
}
if !os.SameFile(d, c) {
out <- m
}
}
close(errs)
close(out)
}()
return out, errs
}
// Move calls Move on each Media on input chan. It is the first step in the
// pipeline after fan-in.
func Move(in <-chan Media, root string) <-chan error {
out := make(chan error)
go func() {
for i := range in {
out <- i.Move(root)
}
close(out)
}()
return out
}
// ParseFile extracts metadata from single file.
func ParseFile(path string) (Media, error) {
ext := strings.ToLower(filepath.Ext(path))
var r Media
hash := md5.New()
var t time.Time
f, err := os.Open(path)
if err != nil {
return r, fmt.Errorf("problem opening file: %v", err)
}
defer f.Close()
switch ext {
default:
return r, NotMedia{path}
case ".jpg", ".jpeg":
if _, err := jpeg.DecodeConfig(f); err != nil {
return r, NotMedia{path}
}
if _, err := f.Seek(0, 0); err != nil {
return r, fmt.Errorf("couldn't seek back in file: %v", err)
}
// try a few things for a time value
{
success := false
if t, err = parseExif(f); err == nil {
success = true
}
if !success {
t, err = mtime(path)
}
if err != nil {
return r, fmt.Errorf("unable to calculate reasonble time for jpg %q: %v", path, err)
}
}
case ".png":
if _, err := png.DecodeConfig(f); err != nil {
return r, NotMedia{path}
}
if _, err := f.Seek(0, 0); err != nil {
return r, fmt.Errorf("couldn't seek back in file: %v", err)
}
t, err = mtime(path)
if err != nil {
return r, fmt.Errorf("unable to calculate reasonble time for media %q: %v", path, err)
}
case ".gif":
if _, err := gif.DecodeConfig(f); err != nil {
return r, NotMedia{path}
}
if _, err := f.Seek(0, 0); err != nil {
return r, fmt.Errorf("couldn't seek back in file: %v", err)
}
t, err = mtime(path)
if err != nil {
return r, fmt.Errorf("unable to calculate reasonble time for media %q: %v", path, err)
}
case ".mov", ".mp4", ".m4v", ".avi":
t, err = mtime(path)
if err != nil {
return r, fmt.Errorf("unable to calculate reasonble time for media %q: %v", path, err)
}
}
if _, err := f.Seek(0, 0); err != nil {
return r, fmt.Errorf("couldn't seek back in file: %v", err)
}
if _, err := io.Copy(hash, f); err != nil {
return r, fmt.Errorf("problem calculating checksum on %q: %v", path, err)
}
r = Media{
Path: path,
Hash: fmt.Sprintf("%x", hash.Sum(nil)),
Extension: ext,
Time: t,
}
return r, nil
}
// Merge implements fan-in.
func Merge(cs []<-chan Media) <-chan Media {
out := make(chan Media)
var wg sync.WaitGroup
output := func(c <-chan Media) {
for n := range c {
out <- n
}
wg.Done()
}
for _, c := range cs {
go output(c)
}
wg.Add(len(cs))
go func() {
wg.Wait()
close(out)
}()
return out
}