-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreport.go
115 lines (96 loc) · 2.14 KB
/
report.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
package main
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"io"
"io/ioutil"
"os"
"time"
)
type guetzliReport struct {
Quality int `json:"quality"`
ModTime time.Time `json:"revisionTime"`
Path string `json:"path"`
Sha1 string `json:"checksumSHA1"`
Version string `json:"guetzli-version"`
}
func (r guetzliReport) Empty() bool {
return r.Quality == 0 && r.ModTime.IsZero() && r.Path == "" && r.Sha1 == "" && r.Version == ""
}
func getReports(path string) map[string]guetzliReport {
buf := bytes.NewBuffer(nil)
file, err := os.Open(path + "guetzli.json")
if err != nil {
return make(map[string]guetzliReport)
}
_, err = io.Copy(buf, file)
if err != nil {
return make(map[string]guetzliReport)
}
file.Close()
reports := make(map[string]guetzliReport)
err = json.Unmarshal(buf.Bytes(), &reports)
if err != nil {
return make(map[string]guetzliReport)
}
return reports
}
func saveReports(version string, reports map[string]guetzliReport, path string) {
b, err := json.Marshal(reports)
if err != nil {
panic(err)
}
var out bytes.Buffer
err = json.Indent(&out, b, "", " ")
if err != nil {
panic(err)
}
err = ioutil.WriteFile(path+"guetzli.json", out.Bytes(), 0644)
if err != nil {
panic(err)
}
}
func timeModified(filePath string) time.Time {
info, err := os.Stat(filePath)
if err != nil {
panic(err)
}
return info.ModTime()
}
func sha1ForFile(filePath string) string {
var returnSHA1String string
file, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer file.Close()
hash := sha1.New()
if _, err = io.Copy(hash, file); err != nil {
panic(err)
}
hashInBytes := hash.Sum(nil)[:20]
returnSHA1String = hex.EncodeToString(hashInBytes)
return returnSHA1String
}
func writeFile(content []byte, fileName string, out string, quality int) {
err := ioutil.WriteFile(out+fileName, content, 0644)
if err != nil {
panic(err)
}
}
func isFile(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
return false
}
return !fileInfo.IsDir()
}
func exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
return !os.IsNotExist(err)
}
return true
}