gres_func.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gres
  7. import (
  8. "archive/zip"
  9. "bytes"
  10. "encoding/hex"
  11. "fmt"
  12. "github.com/gogf/gf/v2/encoding/gbase64"
  13. "github.com/gogf/gf/v2/encoding/gcompress"
  14. "github.com/gogf/gf/v2/errors/gerror"
  15. "github.com/gogf/gf/v2/os/gfile"
  16. "github.com/gogf/gf/v2/text/gstr"
  17. )
  18. const (
  19. packedGoSourceTemplate = `
  20. package %s
  21. import "github.com/gogf/gf/v2/os/gres"
  22. func init() {
  23. if err := gres.Add("%s"); err != nil {
  24. panic("add binary content to resource manager failed: " + err.Error())
  25. }
  26. }
  27. `
  28. )
  29. // Option contains the extra options for Pack functions.
  30. type Option struct {
  31. Prefix string // The file path prefix for each file item in resource manager.
  32. KeepPath bool // Keep the passed path when packing, usually for relative path.
  33. }
  34. // Pack packs the path specified by `srcPaths` into bytes.
  35. // The unnecessary parameter `keyPrefix` indicates the prefix for each file
  36. // packed into the result bytes.
  37. //
  38. // Note that parameter `srcPaths` supports multiple paths join with ','.
  39. //
  40. // Deprecated: use PackWithOption instead.
  41. func Pack(srcPaths string, keyPrefix ...string) ([]byte, error) {
  42. option := Option{}
  43. if len(keyPrefix) > 0 && keyPrefix[0] != "" {
  44. option.Prefix = keyPrefix[0]
  45. }
  46. return PackWithOption(srcPaths, option)
  47. }
  48. // PackWithOption packs the path specified by `srcPaths` into bytes.
  49. //
  50. // Note that parameter `srcPaths` supports multiple paths join with ','.
  51. func PackWithOption(srcPaths string, option Option) ([]byte, error) {
  52. var buffer = bytes.NewBuffer(nil)
  53. err := zipPathWriter(srcPaths, buffer, option)
  54. if err != nil {
  55. return nil, err
  56. }
  57. // Gzip the data bytes to reduce the size.
  58. return gcompress.Gzip(buffer.Bytes(), 9)
  59. }
  60. // PackToFile packs the path specified by `srcPaths` to target file `dstPath`.
  61. // The unnecessary parameter `keyPrefix` indicates the prefix for each file
  62. // packed into the result bytes.
  63. //
  64. // Note that parameter `srcPaths` supports multiple paths join with ','.
  65. //
  66. // Deprecated: use PackToFileWithOption instead.
  67. func PackToFile(srcPaths, dstPath string, keyPrefix ...string) error {
  68. data, err := Pack(srcPaths, keyPrefix...)
  69. if err != nil {
  70. return err
  71. }
  72. return gfile.PutBytes(dstPath, data)
  73. }
  74. // PackToFileWithOption packs the path specified by `srcPaths` to target file `dstPath`.
  75. //
  76. // Note that parameter `srcPaths` supports multiple paths join with ','.
  77. func PackToFileWithOption(srcPaths, dstPath string, option Option) error {
  78. data, err := PackWithOption(srcPaths, option)
  79. if err != nil {
  80. return err
  81. }
  82. return gfile.PutBytes(dstPath, data)
  83. }
  84. // PackToGoFile packs the path specified by `srcPaths` to target go file `goFilePath`
  85. // with given package name `pkgName`.
  86. //
  87. // The unnecessary parameter `keyPrefix` indicates the prefix for each file
  88. // packed into the result bytes.
  89. //
  90. // Note that parameter `srcPaths` supports multiple paths join with ','.
  91. //
  92. // Deprecated: use PackToGoFileWithOption instead.
  93. func PackToGoFile(srcPath, goFilePath, pkgName string, keyPrefix ...string) error {
  94. data, err := Pack(srcPath, keyPrefix...)
  95. if err != nil {
  96. return err
  97. }
  98. return gfile.PutContents(
  99. goFilePath,
  100. fmt.Sprintf(gstr.TrimLeft(packedGoSourceTemplate), pkgName, gbase64.EncodeToString(data)),
  101. )
  102. }
  103. // PackToGoFileWithOption packs the path specified by `srcPaths` to target go file `goFilePath`
  104. // with given package name `pkgName`.
  105. //
  106. // Note that parameter `srcPaths` supports multiple paths join with ','.
  107. func PackToGoFileWithOption(srcPath, goFilePath, pkgName string, option Option) error {
  108. data, err := PackWithOption(srcPath, option)
  109. if err != nil {
  110. return err
  111. }
  112. return gfile.PutContents(
  113. goFilePath,
  114. fmt.Sprintf(gstr.TrimLeft(packedGoSourceTemplate), pkgName, gbase64.EncodeToString(data)),
  115. )
  116. }
  117. // Unpack unpacks the content specified by `path` to []*File.
  118. func Unpack(path string) ([]*File, error) {
  119. realPath, err := gfile.Search(path)
  120. if err != nil {
  121. return nil, err
  122. }
  123. return UnpackContent(gfile.GetContents(realPath))
  124. }
  125. // UnpackContent unpacks the content to []*File.
  126. func UnpackContent(content string) ([]*File, error) {
  127. var (
  128. err error
  129. data []byte
  130. )
  131. if isHexStr(content) {
  132. // It here keeps compatible with old version packing string using hex string.
  133. // TODO remove this support in the future.
  134. data, err = gcompress.UnGzip(hexStrToBytes(content))
  135. if err != nil {
  136. return nil, err
  137. }
  138. } else if isBase64(content) {
  139. // New version packing string using base64.
  140. b, err := gbase64.DecodeString(content)
  141. if err != nil {
  142. return nil, err
  143. }
  144. data, err = gcompress.UnGzip(b)
  145. if err != nil {
  146. return nil, err
  147. }
  148. } else {
  149. data, err = gcompress.UnGzip([]byte(content))
  150. if err != nil {
  151. return nil, err
  152. }
  153. }
  154. reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
  155. if err != nil {
  156. err = gerror.Wrapf(err, `create zip reader failed`)
  157. return nil, err
  158. }
  159. array := make([]*File, len(reader.File))
  160. for i, file := range reader.File {
  161. array[i] = &File{file: file}
  162. }
  163. return array, nil
  164. }
  165. // isBase64 checks and returns whether given content `s` is base64 string.
  166. // It returns true if `s` is base64 string, or false if not.
  167. func isBase64(s string) bool {
  168. var r bool
  169. for i := 0; i < len(s); i++ {
  170. r = (s[i] >= '0' && s[i] <= '9') ||
  171. (s[i] >= 'a' && s[i] <= 'z') ||
  172. (s[i] >= 'A' && s[i] <= 'Z') ||
  173. (s[i] == '+' || s[i] == '-') ||
  174. (s[i] == '_' || s[i] == '/') || s[i] == '='
  175. if !r {
  176. return false
  177. }
  178. }
  179. return true
  180. }
  181. // isHexStr checks and returns whether given content `s` is hex string.
  182. // It returns true if `s` is hex string, or false if not.
  183. func isHexStr(s string) bool {
  184. var r bool
  185. for i := 0; i < len(s); i++ {
  186. r = (s[i] >= '0' && s[i] <= '9') ||
  187. (s[i] >= 'a' && s[i] <= 'f') ||
  188. (s[i] >= 'A' && s[i] <= 'F')
  189. if !r {
  190. return false
  191. }
  192. }
  193. return true
  194. }
  195. // hexStrToBytes converts hex string content to []byte.
  196. func hexStrToBytes(s string) []byte {
  197. src := []byte(s)
  198. dst := make([]byte, hex.DecodedLen(len(src)))
  199. _, _ = hex.Decode(dst, src)
  200. return dst
  201. }