-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
182 lines (162 loc) · 3.61 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
178
179
180
181
182
package main
import (
"fmt"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
)
func removeJPGExif(filePath string) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer func() {
if cErr := file.Close(); cErr != nil && err == nil {
err = cErr
}
}()
img, err := jpeg.Decode(file)
if err != nil {
return err
}
output, err := os.Create(filePath)
if err != nil {
return err
}
defer func() {
if oErr := file.Close(); oErr != nil && err == nil {
err = oErr
}
}()
options := jpeg.Options{Quality: 100}
return jpeg.Encode(output, img, &options)
}
func removePNGExif(filePath string) (err error) {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer func() {
if cErr := file.Close(); cErr != nil && err == nil {
err = cErr
}
}()
img, err := png.Decode(file)
if err != nil {
return err
}
output, err := os.Create(filePath)
if err != nil {
return err
}
defer func() {
if oErr := file.Close(); oErr != nil && err == nil {
err = oErr
}
}()
return png.Encode(output, img)
}
func worker(id int, jobs <-chan string, results chan<- string, wg *sync.WaitGroup) {
defer wg.Done()
for path := range jobs {
var err error
ext := strings.ToLower(filepath.Ext(path))
switch ext {
case ".jpg", ".jpeg":
err = removeJPGExif(path)
case ".png":
err = removePNGExif(path)
}
if err != nil {
results <- fmt.Sprintf("Worker %d: Error processing file %s: %v", id, path, err)
} else {
results <- fmt.Sprintf("Worker %d: Successfully processed file: %s", id, path)
}
}
}
func processFolder(folderPath string) error {
//return filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
// if err != nil {
// return err
// }
//
// if !info.IsDir() {
// ext := strings.ToLower(filepath.Ext(path))
// var err error
// switch ext {
// case ".jpg", ".jpeg":
// err = removeJPGExif(path)
// case ".png":
// err = removePNGExif(path)
// }
// if err != nil {
// fmt.Printf("Error processing file %s: %v", path, err)
// } else {
// fmt.Printf("Successfully processed file: %s", path)
// }
// }
// return nil
//})
var wg sync.WaitGroup
jobs := make(chan string, 1000)
results := make(chan string, 1000)
for i := 0; i < runtime.NumCPU(); i++ {
wg.Add(1)
go worker(i, jobs, results, &wg)
}
var walkErr error
go func() {
defer close(jobs)
walkErr = filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
ext := strings.ToLower(filepath.Ext(path))
if ext == ".jpg" || ext == ".jpeg" || ext == ".png" {
jobs <- path
} else {
fmt.Printf("Skipping non-target file: %s\n", path)
}
}
return nil
})
}()
go func() {
wg.Wait()
close(results)
}()
var errs []string
if walkErr != nil {
errs = append(errs, fmt.Sprintf("Error walking the path: %v", walkErr))
}
for result := range results {
if strings.HasPrefix(result, "Worker") && strings.Contains(result, "Error") {
errs = append(errs, result)
}
//fmt.Println(result)
}
if len(errs) > 0 {
return fmt.Errorf("errors occurred:\n%s", strings.Join(errs, "\n"))
}
return nil
}
func main() {
timeStart := time.Now().Unix()
if len(os.Args) < 2 {
fmt.Println("Please provide a folder path.")
return
}
folderPath := os.Args[1]
if err := processFolder(folderPath); err != nil {
fmt.Printf("Error processing folder: %v\n", err)
} else {
fmt.Println("Successfully removed EXIF data from all JPG and PNG files.")
}
fmt.Printf("Time spent: %vs\n", time.Now().Unix()-timeStart)
}