-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
71 lines (55 loc) · 1.18 KB
/
parser.go
File metadata and controls
71 lines (55 loc) · 1.18 KB
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
package main
import (
"github.com/rwcarlsen/goexif/exif"
"github.com/schollz/progressbar/v3"
"os"
"path/filepath"
"time"
)
func parserProgressBar(ch <-chan CounterType, len int) {
fileBar := progressbar.Default(int64(len))
for cType := range ch {
switch cType {
case File:
fileBar.Add(1)
}
}
fileBar.Finish()
}
type Parser struct{}
func (p *Parser) Parse(srcs []string, dstFolder string) ([]*Copy, error) {
ch := make(chan CounterType)
defer close(ch)
go parserProgressBar(ch, len(srcs))
var copies []*Copy
for _, src := range srcs {
dst, err := p.parseFile(src)
if err != nil {
return nil, err
}
dst = filepath.Join(dstFolder, dst)
copies = append(copies, NewCopy(src, dst))
ch <- File
}
return copies, nil
}
func (p *Parser) parseFile(src string) (string, error) {
f, err := os.Open(src)
if err != nil {
return "", err
}
defer f.Close()
x, err := exif.Decode(f)
if err != nil {
return "", err
}
bTime, err := x.DateTime()
if err != nil {
return "", err
}
toDir := p.timeToDirName(bTime)
return filepath.Join(toDir, filepath.Base(src)), nil
}
func (p *Parser) timeToDirName(t time.Time) string {
return t.Format("2006-01-02")
}