-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
191 lines (166 loc) · 3.95 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
183
184
185
186
187
188
189
190
191
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/alecthomas/kingpin/v2"
)
var (
inFile = kingpin.Arg("in", "Input file").Required().String()
outFile = kingpin.Flag("out", "Output file (dont overwrite input)").Short('o').String()
verbose = kingpin.Flag("verbose", "Verbose").Short('v').Bool()
)
func init() {
log.SetFlags(log.Lshortfile)
}
func main() {
// support -h for --help
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.Parse()
if !exists(*inFile) {
log.Fatal("File", *inFile, " not found")
}
inFileSize := fileSize(*inFile)
if err := pngcrushCompress(*inFile); err != nil {
log.Fatal(err)
}
if err := optipngCompress(*inFile); err != nil {
log.Fatal(err)
}
outFileSize := fileSize(*inFile)
diffSize := inFileSize - outFileSize
pctShrunk := 100 - ((float64(outFileSize) / float64(inFileSize)) * 100)
if diffSize > 0 || *verbose {
fmt.Printf("%s: %d -> %d (shrunk by %d bytes, %0.1f%%)",
*inFile, inFileSize, outFileSize, diffSize, pctShrunk)
fmt.Println()
}
}
func pngcrushCompress(file string) error {
inFileSize := fileSize(file)
pngcrushFile := findFreeOutFileName(file)
pngcrushPath, err := lookPath("pngcrush")
if err != nil {
return err
}
err = runCommand(pngcrushPath, "-s", "-brute", "-rem", "alla", file, pngcrushFile)
if err != nil {
return fmt.Errorf("pngcrush: error occured while processing %s: %v", file, err)
}
outSize := fileSize(pngcrushFile)
diffSize := inFileSize - outSize
pctShrunk := 100 - ((float64(outSize) / float64(inFileSize)) * 100)
if *verbose {
fmt.Printf("pngcrush: %d -> %d (shrunk by %d bytes, %0.1f%%)",
inFileSize, outSize, diffSize, pctShrunk)
fmt.Println()
}
if diffSize < 0 {
if *verbose {
fmt.Println("pngcrush: throwing away non-shrunk file", pngcrushFile)
}
os.Remove(pngcrushFile)
return nil
}
err = os.Remove(file)
if err != nil {
return err
}
return os.Rename(pngcrushFile, file)
}
func optipngCompress(file string) error {
inFileSize := fileSize(file)
optipngPath, err := lookPath("optipng")
if err != nil {
return err
}
optipngFile := findFreeOutFileName(file)
err = runCommand(optipngPath, "-o7", "-out", optipngFile, file)
if err != nil {
return fmt.Errorf("optipng: error occured while processing %s: %v", file, err)
}
outSize := fileSize(optipngFile)
diffSize := inFileSize - outSize
pctShrunk := 100 - ((float64(outSize) / float64(inFileSize)) * 100)
if *verbose {
fmt.Printf("optipng: %d -> %d (shrunk by %d bytes, %0.1f%%)",
inFileSize, outSize, diffSize, pctShrunk)
fmt.Println()
}
if diffSize < 0 {
if *verbose {
fmt.Println("optipng: throwing away non-shrunk file", optipngFile)
}
os.Remove(optipngFile)
return nil
}
err = os.Remove(file)
if err != nil {
return err
}
return os.Rename(optipngFile, file)
}
// exists reports whether the named file or directory exists.
func exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func baseNameWithoutExt(filename string) string {
s := filepath.Base(filename)
n := strings.LastIndexByte(s, '.')
if n >= 0 {
return s[:n]
}
return s
}
func findFreeOutFileName(file string) string {
cnt := 0
res := ""
ext := path.Ext(file)
for {
res = path.Join(filepath.Dir(file), baseNameWithoutExt(file))
if cnt > 0 {
res += "-" + fmt.Sprintf("%02d", cnt)
}
res += ext
if !exists(res) {
break
}
cnt++
}
return res
}
func runCommand(name string, arg ...string) error {
if *verbose {
fmt.Println("EXEC:", name, strings.Join(arg, " "))
}
cmd := exec.Command(name, arg...)
return cmd.Run()
}
func fileSize(filepath string) int64 {
file, err := os.Open(filepath)
defer file.Close()
if err != nil {
log.Fatal(err)
}
fi, err := file.Stat()
if err != nil {
log.Fatal(err)
}
return fi.Size()
}
func lookPath(file string) (string, error) {
if runtime.GOOS == "windows" {
file += ".exe"
}
return exec.LookPath(file)
}