-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
177 lines (133 loc) · 3.99 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
)
var rawExtensions = make(map[string]struct{})
func main() {
// Remove timestamp prefix, we do not want this
log.SetFlags(0)
noDryMode := flag.Bool("d", false, "If set, this tool will actually delete pictures. Otherwise it will run in dry mode")
rawExtensionsFlag := flag.String("raw-exts", "arw,dng,nef", "Comma-separated list of extension that are considered to be RAW images")
flag.Parse()
for _, rawExtension := range strings.Split(*rawExtensionsFlag, ",") {
rawExtensions[strings.ToLower(rawExtension)] = struct{}{}
}
inputPath := flag.Arg(0)
if inputPath == "" {
log.Fatal("No input path given")
}
absPath, err := filepath.Abs(inputPath)
if err != nil {
log.Fatalf("Could not make given path %q absolute: %v", inputPath, err)
}
file, err := os.Open(absPath)
if err != nil {
log.Fatalf("Could not open given input path %q: %v", absPath, err)
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
log.Fatalf("Could not stat given input path %q: %v", absPath, err)
}
if !stat.IsDir() {
log.Fatalf("Given input path %q is required to point to a directory.", absPath)
}
if !*noDryMode {
log.Println("Running in dry mode, no files will actually be deleted. Run with '-d' to do so.")
}
log.Printf("Going to check %q and subdirectories for unnecessary JPEGs...", absPath)
var deleter Deleter = &DryRunner{}
if *noDryMode {
deleter = &Remover{}
}
filesDeleted, bytesDeleted, err := checkDir(absPath, deleter)
if err != nil {
log.Fatalf("Failed checking directory: %v", err)
}
if *noDryMode {
log.Printf("Successfully deleted %d file(s) and freed %.2f Mebibytes.", filesDeleted, float64(bytesDeleted)/1024/1024)
} else {
log.Printf("Would have deleted %d file(s) and freed %.2f Mebibytes, but was running in dry mode.", filesDeleted, float64(bytesDeleted)/1024/1024)
if filesDeleted > 0 {
log.Println("To actually delete files run with '-d'.")
}
}
}
func checkDir(curDir string, deleter Deleter) (int64, int64, error) {
var (
rawLookupTbl = make(map[string]struct{})
jpegList []string
filesDeleted int64
bytesDeleted int64
)
entries, err := os.ReadDir(curDir)
if err != nil {
return -1, -1, fmt.Errorf("reading dir %q: %w", curDir, err)
}
// 1. step: find JPEGs and RAWs
for _, entry := range entries {
name := entry.Name()
if entry.IsDir() {
subFilesDeleted, subBytesDeleted, err := checkDir(path.Join(curDir, name), deleter)
if err != nil {
return -1, -1, err
}
filesDeleted += subFilesDeleted
bytesDeleted += subBytesDeleted
}
if isRAW(name) {
rawLookupTbl[loweredNoExt(name)] = struct{}{}
}
if isJPEG(name) {
jpegList = append(jpegList, name)
}
}
// 2. step: Find a RAW for every, if there is one delete the JPEG
for _, jpeg := range jpegList {
if _, ok := rawLookupTbl[loweredNoExt(jpeg)]; ok {
fullPath := path.Join(curDir, jpeg)
stats, err := os.Stat(fullPath)
if err != nil {
return -1, -1, fmt.Errorf("stating file %q: %w", fullPath, err)
}
if err := deleter.Delete(fullPath); err != nil {
return -1, -1, fmt.Errorf("deleting file %q: %w", fullPath, err)
}
filesDeleted++
bytesDeleted += stats.Size()
}
}
return filesDeleted, bytesDeleted, nil
}
func loweredNoExt(name string) string {
lowered := strings.ToLower(name)
return strings.TrimSuffix(lowered, filepath.Ext(lowered))
}
func isJPEG(name string) bool {
return hasSuffix(name, ".jpg") || hasSuffix(name, ".jpeg")
}
func hasSuffix(name, suffix string) bool {
return strings.HasSuffix(strings.ToLower(name), strings.ToLower(suffix))
}
func isRAW(name string) bool {
_, found := rawExtensions[strings.ToLower(strings.TrimPrefix(path.Ext(name), "."))]
return found
}
type Deleter interface {
Delete(path string) error
}
type DryRunner struct{}
func (d *DryRunner) Delete(path string) error {
log.Println(path)
return nil
}
type Remover struct{}
func (r *Remover) Delete(path string) error {
return os.Remove(path)
}