-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessDirEntry.go
68 lines (52 loc) · 1.19 KB
/
processDirEntry.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
package main
import (
"io/fs"
"log"
"os"
"slices"
"strings"
"github.com/vitali-fedulov/images4"
)
func processDirEntry(path string, d fs.DirEntry, result *images4.IconT) error {
if d.IsDir() {
if dirThreaded {
limit.Add(path)
} else if options.Walk {
processDir(path)
}
return nil
}
// not a (supported) image
if !isSupportedImage(d) {
return nil
}
defer imagesRead.Add(1)
img, err := images4.Open(path)
if err != nil {
logToFile.Printf("[corrupted image] failed to read image at %s", path)
return nil
}
icon := images4.Icon(img)
if result != nil {
*result = icon
}
if matchAggregator(icon) {
logToFile.Printf("[aggregator image] found at %s", path)
deleteDirEntry(path)
}
return err
}
func deleteDirEntry(path string) {
dirEntriesFound.Add(1)
if options.Delete {
if err := os.Remove(path); err != nil {
log.Panicf("failed to delete file, error:\n%s", err.Error())
} else {
logToFile.Printf("file deleted successfully")
}
}
}
var imageExtensions = []string{".png", ".jpg", ".jpeg", ".gif"}
func isSupportedImage(d fs.DirEntry) bool {
return slices.ContainsFunc(imageExtensions, func(e string) bool { return strings.HasSuffix(d.Name(), e) })
}