This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
77 lines (70 loc) · 1.69 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
package main
import (
"bytes"
"errors"
"fmt"
"image"
"io/ioutil"
"os"
"path/filepath"
"github.com/scottleedavis/go-exif-remove"
)
func main() {
if len(os.Args) == 1 {
var files []string
root := "img"
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if path != "img" && path != "img/png" && path != "img/jpg" {
files = append(files, path)
}
return nil
})
if err != nil {
panic(err)
}
pass := 0
fail := 0
for _, file := range files {
fmt.Println(file)
if b, err := handleFile(file); err != nil {
fail += 1
} else {
pass += 1
f := filepath.Base(file)
ioutil.WriteFile("img_output/"+f, b, 0644)
}
fmt.Println()
}
percentage := 100 * pass / (pass + fail)
fmt.Printf("Results (%v%%): %v pass, %v fail \n", int(percentage), pass, fail)
} else {
path := os.Args[1]
if b, err := handleFile(path); err != nil {
fmt.Printf(err.Error())
} else {
file := filepath.Base(path)
ioutil.WriteFile("img_output/"+file, b, 0644)
}
}
}
func handleFile(filepath string) ([]byte, error) {
if data, err := ioutil.ReadFile(filepath); err != nil {
fmt.Printf(err.Error())
return nil, err
} else {
if _, _, err := image.Decode(bytes.NewReader(data)); err != nil {
fmt.Printf("ERROR: original image is corrupt" + err.Error() + "\n")
return nil, err
}
if filtered, err := exifremove.Remove(data); err != nil {
fmt.Printf("* " + err.Error() + "\n")
return nil, errors.New(err.Error())
} else {
if _, _, err = image.Decode(bytes.NewReader(filtered)); err != nil {
fmt.Printf("ERROR: filtered image is corrupt" + err.Error() + "\n")
return nil, err
}
return filtered, nil
}
}
}