/
assets.go
292 lines (238 loc) · 5.61 KB
/
assets.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// 打包资源文件
package goassets
import (
"bytes"
"compress/gzip"
"encoding/base64"
"errors"
"go/format"
"html/template"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/zhuomouren/gohelpers/gofile"
"github.com/zhuomouren/gohelpers/gostring"
)
var (
Assets = NewGoAssets()
)
type GoAssets struct {
packageName string // 包名
assetPaths []string // 资源路径
assetsSavePath string
root string
exts []string
assets []Asset
}
type Asset struct {
Path string
Mode os.FileMode
ModTime time.Time
Data string
// Info os.FileInfo
}
func (this *Asset) GetRawData() ([]byte, error) {
return decodeAsset(this.Data)
}
//
func NewGoAssets() *GoAssets {
goAssets := &GoAssets{
assets: []Asset{},
}
return goAssets
}
func (this *GoAssets) Exists() bool {
return len(this.assets) > 0
}
func (this *GoAssets) GetAsset(name string) ([]byte, error) {
name = filepath.ToSlash(name)
for _, asset := range this.assets {
if strings.EqualFold(name, asset.Path) {
data, err := this.DecodeAsset(asset.Data)
if err != nil {
return nil, err
}
return data, nil
}
}
return nil, os.ErrNotExist
}
func (this *GoAssets) ReadAsset(name string) (Asset, error) {
name = filepath.ToSlash(name)
for _, asset := range this.assets {
if strings.EqualFold(name, asset.Path) {
data, err := this.DecodeAsset(asset.Data)
if err != nil {
return Asset{}, err
}
asset.Data = string(data)
return asset, nil
}
}
return Asset{}, os.ErrNotExist
}
func (this *GoAssets) DecodeAsset(assetData string) ([]byte, error) {
return decodeAsset(assetData)
}
func (this *GoAssets) AddAsset(path string, modTime time.Time, data string) {
asset := Asset{
Path: path,
ModTime: modTime,
Data: data,
}
this.assets = append(this.assets, asset)
}
func (this *GoAssets) SetAssets(assets []Asset) {
this.assets = assets
}
func (this *GoAssets) GetAssets() []Asset {
return this.assets
}
func (this *GoAssets) GetAssetPaths() []string {
return this.assetPaths
}
func (this *GoAssets) SetAssetPaths(assetPaths []string) {
this.assetPaths = assetPaths
}
func (this *GoAssets) AddAssetPath(assetPath string) {
this.assetPaths = append(this.assetPaths, assetPath)
}
func (this *GoAssets) RemoveAssetPath(assetPath string) {
this.assetPaths = gostring.Helper.RemoveSlice(this.assetPaths, assetPath)
}
func (this *GoAssets) SetExts(exts []string) {
this.exts = exts
}
func (this *GoAssets) AddExt(ext string) {
this.exts = append(this.exts, ext)
}
func (this *GoAssets) RemoveExt(ext string) {
this.exts = gostring.Helper.RemoveSlice(this.exts, ext)
}
func (this *GoAssets) Build(root, packageName, savePath string) error {
this.root = cleanPath(root)
this.packageName = packageName
this.assetsSavePath = savePath
if this.root == "" || this.packageName == "" || this.assetsSavePath == "" {
return errors.New("root or packageName or savePath cannot be empty.")
}
var err error
t := template.New("asset.tpl").Funcs(template.FuncMap{
"output": func(data string) template.HTML {
return template.HTML(data)
},
})
t, err = t.Parse(assetTmpl)
if err != nil {
return err
}
err = this.parse()
if err != nil {
return err
}
w := &bytes.Buffer{}
t.Execute(w, map[string]interface{}{
"PackageName": this.packageName,
"Assets": this.assets,
})
source, err := format.Source(w.Bytes())
if err != nil {
return err
}
if err := ioutil.WriteFile(this.assetsSavePath, source, os.ModePerm); err != nil {
return err
}
return nil
}
func (this *GoAssets) parse() error {
var files []string
for _, assetPath := range this.assetPaths {
var err error
var allowedFiles []string
path := cleanJoinPath(this.root, assetPath)
if len(this.exts) > 0 {
_, allowedFiles, err = gofile.Helper.AllowedFiles(path, this.exts)
} else {
_, allowedFiles, err = gofile.Helper.Files(path)
}
if err != nil {
return err
}
files = append(files, allowedFiles...)
}
for _, file := range files {
if strings.HasSuffix(file, ".DS_Store") {
continue
}
file = cleanPath(file)
asset, err := this.readFile(file)
if err != nil {
return err
}
asset.Path = strings.TrimPrefix(file, this.root)
this.assets = append(this.assets, asset)
}
return nil
}
func (this *GoAssets) readFile(assetFile string) (Asset, error) {
asset := Asset{}
f, err := os.Open(assetFile)
if err != nil {
return asset, err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return asset, err
}
// asset.Info = fi
asset.Mode = fi.Mode()
asset.ModTime = fi.ModTime()
data, err := ioutil.ReadAll(f)
if err != nil {
return asset, err
}
var buf bytes.Buffer
gw, err := gzip.NewWriterLevel(&buf, gzip.BestCompression)
if err != nil {
return asset, err
}
if _, err := gw.Write(data); err != nil {
return asset, err
}
if err := gw.Close(); err != nil {
return asset, err
}
var b bytes.Buffer
b64 := base64.NewEncoder(base64.StdEncoding, &b)
b64.Write(buf.Bytes())
b64.Close()
res := "\n"
// 每行 80 个字符
chunk := make([]byte, 80)
for n, _ := b.Read(chunk); n > 0; n, _ = b.Read(chunk) {
res += string(chunk[0:n]) + "\n"
}
asset.Data = res
return asset, nil
}
func cleanJoinPath(paths ...string) string {
return cleanPath(filepath.Join(paths...))
}
func cleanPath(path string) string {
return filepath.ToSlash(filepath.Clean(path))
}
func decodeAsset(assetData string) ([]byte, error) {
b64 := base64.NewDecoder(base64.StdEncoding, bytes.NewBufferString(assetData))
gr, err := gzip.NewReader(b64)
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(gr)
if err != nil {
return nil, err
}
return data, nil
}