-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
gres_func.go
219 lines (201 loc) · 6.08 KB
/
gres_func.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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gres
import (
"archive/zip"
"bytes"
"encoding/hex"
"fmt"
"github.com/gogf/gf/v2/encoding/gbase64"
"github.com/gogf/gf/v2/encoding/gcompress"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gstr"
)
const (
packedGoSourceTemplate = `
package %s
import "github.com/gogf/gf/v2/os/gres"
func init() {
if err := gres.Add("%s"); err != nil {
panic("add binary content to resource manager failed: " + err.Error())
}
}
`
)
// Option contains the extra options for Pack functions.
type Option struct {
Prefix string // The file path prefix for each file item in resource manager.
KeepPath bool // Keep the passed path when packing, usually for relative path.
}
// Pack packs the path specified by `srcPaths` into bytes.
// The unnecessary parameter `keyPrefix` indicates the prefix for each file
// packed into the result bytes.
//
// Note that parameter `srcPaths` supports multiple paths join with ','.
//
// Deprecated: use PackWithOption instead.
func Pack(srcPaths string, keyPrefix ...string) ([]byte, error) {
option := Option{}
if len(keyPrefix) > 0 && keyPrefix[0] != "" {
option.Prefix = keyPrefix[0]
}
return PackWithOption(srcPaths, option)
}
// PackWithOption packs the path specified by `srcPaths` into bytes.
//
// Note that parameter `srcPaths` supports multiple paths join with ','.
func PackWithOption(srcPaths string, option Option) ([]byte, error) {
var buffer = bytes.NewBuffer(nil)
err := zipPathWriter(srcPaths, buffer, option)
if err != nil {
return nil, err
}
// Gzip the data bytes to reduce the size.
return gcompress.Gzip(buffer.Bytes(), 9)
}
// PackToFile packs the path specified by `srcPaths` to target file `dstPath`.
// The unnecessary parameter `keyPrefix` indicates the prefix for each file
// packed into the result bytes.
//
// Note that parameter `srcPaths` supports multiple paths join with ','.
//
// Deprecated: use PackToFileWithOption instead.
func PackToFile(srcPaths, dstPath string, keyPrefix ...string) error {
data, err := Pack(srcPaths, keyPrefix...)
if err != nil {
return err
}
return gfile.PutBytes(dstPath, data)
}
// PackToFileWithOption packs the path specified by `srcPaths` to target file `dstPath`.
//
// Note that parameter `srcPaths` supports multiple paths join with ','.
func PackToFileWithOption(srcPaths, dstPath string, option Option) error {
data, err := PackWithOption(srcPaths, option)
if err != nil {
return err
}
return gfile.PutBytes(dstPath, data)
}
// PackToGoFile packs the path specified by `srcPaths` to target go file `goFilePath`
// with given package name `pkgName`.
//
// The unnecessary parameter `keyPrefix` indicates the prefix for each file
// packed into the result bytes.
//
// Note that parameter `srcPaths` supports multiple paths join with ','.
//
// Deprecated: use PackToGoFileWithOption instead.
func PackToGoFile(srcPath, goFilePath, pkgName string, keyPrefix ...string) error {
data, err := Pack(srcPath, keyPrefix...)
if err != nil {
return err
}
return gfile.PutContents(
goFilePath,
fmt.Sprintf(gstr.TrimLeft(packedGoSourceTemplate), pkgName, gbase64.EncodeToString(data)),
)
}
// PackToGoFileWithOption packs the path specified by `srcPaths` to target go file `goFilePath`
// with given package name `pkgName`.
//
// Note that parameter `srcPaths` supports multiple paths join with ','.
func PackToGoFileWithOption(srcPath, goFilePath, pkgName string, option Option) error {
data, err := PackWithOption(srcPath, option)
if err != nil {
return err
}
return gfile.PutContents(
goFilePath,
fmt.Sprintf(gstr.TrimLeft(packedGoSourceTemplate), pkgName, gbase64.EncodeToString(data)),
)
}
// Unpack unpacks the content specified by `path` to []*File.
func Unpack(path string) ([]*File, error) {
realPath, err := gfile.Search(path)
if err != nil {
return nil, err
}
return UnpackContent(gfile.GetContents(realPath))
}
// UnpackContent unpacks the content to []*File.
func UnpackContent(content string) ([]*File, error) {
var (
err error
data []byte
)
if isHexStr(content) {
// It here keeps compatible with old version packing string using hex string.
// TODO remove this support in the future.
data, err = gcompress.UnGzip(hexStrToBytes(content))
if err != nil {
return nil, err
}
} else if isBase64(content) {
// New version packing string using base64.
b, err := gbase64.DecodeString(content)
if err != nil {
return nil, err
}
data, err = gcompress.UnGzip(b)
if err != nil {
return nil, err
}
} else {
data, err = gcompress.UnGzip([]byte(content))
if err != nil {
return nil, err
}
}
reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
err = gerror.Wrapf(err, `create zip reader failed`)
return nil, err
}
array := make([]*File, len(reader.File))
for i, file := range reader.File {
array[i] = &File{file: file}
}
return array, nil
}
// isBase64 checks and returns whether given content `s` is base64 string.
// It returns true if `s` is base64 string, or false if not.
func isBase64(s string) bool {
var r bool
for i := 0; i < len(s); i++ {
r = (s[i] >= '0' && s[i] <= '9') ||
(s[i] >= 'a' && s[i] <= 'z') ||
(s[i] >= 'A' && s[i] <= 'Z') ||
(s[i] == '+' || s[i] == '-') ||
(s[i] == '_' || s[i] == '/') || s[i] == '='
if !r {
return false
}
}
return true
}
// isHexStr checks and returns whether given content `s` is hex string.
// It returns true if `s` is hex string, or false if not.
func isHexStr(s string) bool {
var r bool
for i := 0; i < len(s); i++ {
r = (s[i] >= '0' && s[i] <= '9') ||
(s[i] >= 'a' && s[i] <= 'f') ||
(s[i] >= 'A' && s[i] <= 'F')
if !r {
return false
}
}
return true
}
// hexStrToBytes converts hex string content to []byte.
func hexStrToBytes(s string) []byte {
src := []byte(s)
dst := make([]byte, hex.DecodedLen(len(src)))
_, _ = hex.Decode(dst, src)
return dst
}