MCPcopy
hub / github.com/nirui/sshwifty / staticFileGen

Function staticFileGen

application/controller/static_pages.go:37–85  ·  view source on GitHub ↗

WARNING: THIS GENERATION IS FOR DEBUG / DEVELOPMENT ONLY, DO NOT USE IT IN PRODUCTION!

(filePath string)

Source from the content-addressed store, hash-verified

35// USE IT IN PRODUCTION!
36
37func staticFileGen(filePath string) staticData {
38 content, readErr := ioutil.ReadFile(filePath)
39
40 if readErr != nil {
41 panic(fmt.Sprintln("Cannot read file:", readErr))
42 }
43
44 compressed := bytes.NewBuffer(make([]byte, 0, 1024))
45
46 compresser, compresserBuildErr := gzip.NewWriterLevel(
47 compressed, gzip.BestCompression)
48
49 if compresserBuildErr != nil {
50 panic(fmt.Sprintln("Cannot build data compresser:", compresserBuildErr))
51 }
52
53 contentLen := len(content)
54
55 _, compressErr := compresser.Write(content)
56
57 if compressErr != nil {
58 panic(fmt.Sprintln("Cannot write compressed data:", compressErr))
59 }
60
61 compressErr = compresser.Flush()
62
63 if compressErr != nil {
64 panic(fmt.Sprintln("Cannot write compressed data:", compressErr))
65 }
66
67 content = append(content, compressed.Bytes()...)
68
69 getHash := func(b []byte) []byte {
70 h := sha256.New()
71 h.Write(b)
72
73 return h.Sum(nil)
74 }
75
76 return staticData{
77 data: content[0:contentLen],
78 dataHash: base64.StdEncoding.EncodeToString(
79 getHash(content[0:contentLen])[:8]),
80 compressd: content[contentLen:],
81 compressdHash: base64.StdEncoding.EncodeToString(
82 getHash(content[contentLen:])[:8]),
83 created: time.Now(),
84 }
85}
86
87var (
88 staticPages = map[string]staticData{

Callers 1

static_pages.goFile · 0.85

Calls 3

getHashFunction · 0.85
NewMethod · 0.80
WriteMethod · 0.65

Tested by

no test coverage detected