/
etag.go
43 lines (38 loc) · 876 Bytes
/
etag.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
package web
import (
"crypto/sha256"
"encoding/base64"
"errors"
"io/ioutil"
"os"
"path/filepath"
)
var EtagNotFoundError = errors.New("etag not found")
type EtagFactory struct {
filemap map[string]string
}
func NewEtagFactory(staticFileDir string) (*EtagFactory, error) {
ef := &EtagFactory{make(map[string]string)}
err := filepath.Walk(staticFileDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
hash := sha256.Sum256(content)
ef.filemap["/"+path] = `"` + base64.StdEncoding.EncodeToString(hash[:]) + `"`
}
return nil
})
return ef, err
}
func (ef *EtagFactory) GetEtag(filePath string) (string, error) {
etag, ok := ef.filemap[filePath]
if !ok {
return "", EtagNotFoundError
}
return etag, nil
}