forked from greenpau/go-authcrunch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
98 lines (89 loc) · 3.24 KB
/
static.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
// Copyright 2022 Paul Greenberg greenpau@outlook.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ui
import (
"crypto/sha1"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
)
// StaticAssets is an instance of StaticAssetLibrary.
var StaticAssets *StaticAssetLibrary
// StaticAsset is a single static web asset.
type StaticAsset struct {
Path string `json:"path,omitempty" xml:"path,omitempty" yaml:"path,omitempty"`
Restricted bool `json:"restricted,omitempty" xml:"restricted,omitempty" yaml:"restricted,omitempty"`
ContentType string `json:"content_type,omitempty" xml:"content_type,omitempty" yaml:"content_type,omitempty"`
Content string `json:"content,omitempty" xml:"content,omitempty" yaml:"content,omitempty"`
EncodedContent string `json:"encoded_content,omitempty" xml:"encoded_content,omitempty" yaml:"encoded_content,omitempty"`
Checksum string `json:"checksum,omitempty" xml:"checksum,omitempty" yaml:"checksum,omitempty"`
}
// StaticAssetLibrary contains a collection of static assets.
type StaticAssetLibrary struct {
items map[string]*StaticAsset
}
func init() {
var err error
StaticAssets, err = NewStaticAssetLibrary()
if err != nil {
panic(err)
}
}
// NewStaticAssetLibrary returns an instance of StaticAssetLibrary.
func NewStaticAssetLibrary() (*StaticAssetLibrary, error) {
sal := &StaticAssetLibrary{}
sal.items = make(map[string]*StaticAsset)
for path, item := range defaultStaticAssets {
s, err := base64.StdEncoding.DecodeString(item.EncodedContent)
if err != nil {
return nil, fmt.Errorf("static asset %s decoding error: %s", path, err)
}
item.Content = string(s)
h := sha1.New()
io.WriteString(h, item.Content)
item.Checksum = base64.URLEncoding.EncodeToString(h.Sum(nil))
sal.items[path] = item
}
return sal, nil
}
// GetAsset returns an asset from path
func (sal *StaticAssetLibrary) GetAsset(path string) (*StaticAsset, error) {
if item, exists := sal.items[path]; exists {
return item, nil
}
return nil, fmt.Errorf("static asset %s not found", path)
}
// AddAsset adds asset to StaticAssetLibrary
func (sal *StaticAssetLibrary) AddAsset(path, contentType, fsPath string) error {
rawContent, err := ioutil.ReadFile(fsPath)
if err != nil {
return fmt.Errorf("failed to load asset file %s: %s", fsPath, err)
}
item := &StaticAsset{
Path: path,
ContentType: contentType,
EncodedContent: base64.StdEncoding.EncodeToString(rawContent),
}
s, err := base64.StdEncoding.DecodeString(item.EncodedContent)
if err != nil {
return fmt.Errorf("static asset %s decoding error: %s", path, err)
}
item.Content = string(s)
h := sha1.New()
io.WriteString(h, item.Content)
item.Checksum = base64.URLEncoding.EncodeToString(h.Sum(nil))
sal.items[path] = item
return nil
}