-
Notifications
You must be signed in to change notification settings - Fork 0
/
folder.go
218 lines (210 loc) · 6.01 KB
/
folder.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"time"
"golang.org/x/text/unicode/norm"
)
func getInfo(folder string) (OldDir, error) {
myOutFolder := outFolder
if folder != "" {
if folder[0] == '/' {
folder = folder[1:]
}
myOutFolder += "/" + folder
}
data, err := ioutil.ReadFile(myOutFolder + "/index.json")
if err != nil {
return Dir{}.MakeOld(), fmt.Errorf("file \"index.json\" in \"%v\" could not be read: %w", myOutFolder, err)
}
p := Dir{}
err = json.Unmarshal(data, &p)
if err != nil {
return Dir{}.MakeOld(), fmt.Errorf("file \"index.json\" in \"%v\" could not be decoded: %w", myOutFolder, err)
}
return p.MakeOld(), nil
}
func StripLeadingSlash(text string) string {
if text[0] == '/' {
text = text[1:]
}
return text
}
func CheckFolderAge(name string) (bool, error) {
in, err := os.Stat(name)
if err != nil {
return false, fmt.Errorf("stat \"%v\" failed: %w", name, err)
}
return in.ModTime().After(minFolderDate), nil
}
func Walk(name string, folder string) (Dir, Corners, error) {
var corners []Corners
myInFolder := inFolder
myOutFolder := outFolder
if folder != "" {
folder = StripLeadingSlash(folder)
myInFolder += "/" + folder
myOutFolder += "/" + folder
}
myDir := Dir{MinDir: MinDir{N: name, Oldest: time.Now()}, Path: folder}
if err := os.MkdirAll(myOutFolder, os.ModePerm); err != nil {
return myDir, Corners{}, fmt.Errorf("could not create folder structure for \"%v\": %w", myOutFolder, err)
}
files, err := ioutil.ReadDir(myInFolder)
if err != nil {
return myDir, Corners{}, fmt.Errorf("could not list contents of folder \"%v\": %w", myInFolder, err)
}
oldDir, _ := getInfo(folder)
corners, err = WalkSubdirectories(files, folder, oldDir.Subs, myOutFolder, &myDir, corners)
if err != nil {
return myDir, Corners{}, err
}
stats.CurrentFolder = myInFolder
stats.CurrentFolderFiles = len(files)
stats.CurrentFolderProgress = 0
corners = WalkFiles(files, oldDir.Imgs, myInFolder, myOutFolder, &myDir, corners)
newDir := myDir.MakeOld()
for k := range oldDir.Subs {
if strings.ReplaceAll(k, "/", "") == "" || strings.Contains(k, "/") {
return myDir, Corners{}, fmt.Errorf("invalid Subs entry found in old index.json in folder \"%v\"", myOutFolder)
}
if _, ok := newDir.Subs[k]; !ok {
folder := myOutFolder + "/" + k
err := os.RemoveAll(folder)
if err != nil {
printStats(fmt.Errorf("deleting folder \"%v\" failed: %w", folder, err))
}
}
}
for k := range oldDir.Imgs {
if strings.ReplaceAll(k, "/", "") == "" || strings.Contains(k, "/") {
return myDir, Corners{}, fmt.Errorf("invalid Imgs entry found in old index.json in folder \"%v\"", myOutFolder)
}
if _, ok := newDir.Imgs[k]; !ok {
file := myOutFolder + "/" + k
err := os.RemoveAll(file + ".h.webp")
if err != nil {
printStats(fmt.Errorf("deleting file \"%v.h.webp\" failed: %w", file, err))
}
err = os.RemoveAll(file + ".s.webp")
if err != nil {
printStats(fmt.Errorf("deleting file \"%v.s.webp\" failed: %w", file, err))
}
}
}
myCorners := SumCorners(corners)
myDir.C = myCorners.String()
myDir.Sort()
txt, _ := json.Marshal(myDir)
err = ioutil.WriteFile(myOutFolder+"/index.json", txt, os.ModePerm)
return myDir, myCorners, err
}
func WalkFiles(files []os.FileInfo, oldImgs map[string]Img, myInFolder string, myOutFolder string, myDir *Dir, corners []Corners) []Corners {
var wg sync.WaitGroup
for _, f := range files {
name := norm.NFC.String(f.Name())
if name[0] == '.' {
stats.CurrentFolderProgress++
continue
}
if !f.IsDir() {
var img Img
var ok bool
if img, ok = oldImgs[name]; ok {
ok, _ = CheckFileAge(f, myOutFolder+"/"+name)
if alwaysProcessRotatedImages {
format := strings.ToLower(filepath.Ext(name))
if format == ".cr2" {
img, _, err := Info(myInFolder+"/"+name, f)
if err == nil && img.Orientation > 1 {
ok = false
}
}
}
}
if ok {
myDir.AddImage(img)
if c, err := NewCorners(img.C); err == nil {
corners = append(corners, c)
}
stats.BytesSkipped += f.Size()
stats.ImagesSkipped++
} else {
wg.Add(1)
go func(f os.FileInfo, name string) {
defer wg.Done()
concurrency <- struct{}{}
defer func() {
<-concurrency
stats.CurrentFolderProgress++
}()
stats.BytesRead += f.Size()
img, err := Run(myInFolder+"/"+name, myOutFolder+"/"+name, f)
if err != nil || (img == Img{}) {
if err != nil {
stats.LastErrorName = myInFolder + "/" + name
printStats(fmt.Errorf("processing \"%v\" failed: %w", myInFolder+"/"+name, err))
}
myDir.AddMisc(name)
stats.ImagesIgnored++
myDir.Files++
} else {
myDir.AddImage(img)
if c, err := NewCorners(img.C); err == nil {
corners = append(corners, c)
}
stats.ImagesProcessed++
}
}(f, name)
}
} else {
stats.CurrentFolderProgress++
}
}
wg.Wait()
return corners
}
func WalkSubdirectories(files []os.FileInfo, folder string, oldSubs map[string]MinDir, myOutFolder string, myDir *Dir, corners []Corners) ([]Corners, error) {
for _, f := range files {
if f.IsDir() {
if f.Name()[0] == '.' {
continue
}
name := norm.NFC.String(f.Name())
if _, ok := exclusions[StripLeadingSlash(folder+"/"+name)]; ok {
stats.FoldersIgnored++
continue
}
_, ok := oldSubs[name]
var maxage bool
if ok {
maxage, _ = CheckFolderAge(myOutFolder + "/" + name + "/" + "index.json")
}
if !ok || !maxage {
subDir, c, err := Walk(name, folder+"/"+name)
if err != nil {
printStats(fmt.Errorf("walking through \"%v\" failed: %w", StripLeadingSlash(folder+"/"+name), err))
if ok {
return nil, err
}
}
myDir.AddFolder(subDir.MinDir)
corners = append(corners, c)
stats.FoldersProcessed++
} else {
dir := oldSubs[name]
myDir.AddFolder(dir)
c, _ := NewCorners(dir.C)
c.Multiply(uint64(dir.TotalImages))
corners = append(corners, c)
stats.FoldersSkipped++
}
}
}
return corners, nil
}