gres_func.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/encoding/gbase64"
  13. "github.com/gogf/gf/encoding/gcompress"
  14. "github.com/gogf/gf/text/gstr"
  15. "github.com/gogf/gf/os/gfile"
  16. )
  17. const (
  18. packedGoSouceTemplate = `
  19. package %s
  20. import "github.com/gogf/gf/os/gres"
  21. func init() {
  22. if err := gres.Add("%s"); err != nil {
  23. panic("add binary content to resource manager failed: " + err.Error())
  24. }
  25. }
  26. `
  27. )
  28. // Pack packs the path specified by <srcPaths> into bytes.
  29. // The unnecessary parameter <keyPrefix> indicates the prefix for each file
  30. // packed into the result bytes.
  31. //
  32. // Note that parameter <srcPaths> supports multiple paths join with ','.
  33. func Pack(srcPaths string, keyPrefix ...string) ([]byte, error) {
  34. var (
  35. buffer = bytes.NewBuffer(nil)
  36. headerPrefix = ""
  37. )
  38. if len(keyPrefix) > 0 && keyPrefix[0] != "" {
  39. headerPrefix = keyPrefix[0]
  40. }
  41. err := zipPathWriter(srcPaths, buffer, headerPrefix)
  42. if err != nil {
  43. return nil, err
  44. }
  45. // Gzip the data bytes to reduce the size.
  46. return gcompress.Gzip(buffer.Bytes(), 9)
  47. }
  48. // PackToFile packs the path specified by <srcPaths> to target file <dstPath>.
  49. // The unnecessary parameter <keyPrefix> indicates the prefix for each file
  50. // packed into the result bytes.
  51. //
  52. // Note that parameter <srcPaths> supports multiple paths join with ','.
  53. func PackToFile(srcPaths, dstPath string, keyPrefix ...string) error {
  54. data, err := Pack(srcPaths, keyPrefix...)
  55. if err != nil {
  56. return err
  57. }
  58. return gfile.PutBytes(dstPath, data)
  59. }
  60. // PackToGoFile packs the path specified by <srcPaths> to target go file <goFilePath>
  61. // with given package name <pkgName>.
  62. //
  63. // The unnecessary parameter <keyPrefix> indicates the prefix for each file
  64. // packed into the result bytes.
  65. //
  66. // Note that parameter <srcPaths> supports multiple paths join with ','.
  67. func PackToGoFile(srcPath, goFilePath, pkgName string, keyPrefix ...string) error {
  68. data, err := Pack(srcPath, keyPrefix...)
  69. if err != nil {
  70. return err
  71. }
  72. return gfile.PutContents(
  73. goFilePath,
  74. fmt.Sprintf(gstr.TrimLeft(packedGoSouceTemplate), pkgName, gbase64.EncodeToString(data)),
  75. )
  76. }
  77. // Unpack unpacks the content specified by <path> to []*File.
  78. func Unpack(path string) ([]*File, error) {
  79. realPath, err := gfile.Search(path)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return UnpackContent(gfile.GetContents(realPath))
  84. }
  85. // UnpackContent unpacks the content to []*File.
  86. func UnpackContent(content string) ([]*File, error) {
  87. var data []byte
  88. var err error
  89. if isHexStr(content) {
  90. // It here keeps compatible with old version packing string using hex string.
  91. // TODO remove this support in the future.
  92. data, err = gcompress.UnGzip(hexStrToBytes(content))
  93. if err != nil {
  94. return nil, err
  95. }
  96. } else if isBase64(content) {
  97. // New version packing string using base64.
  98. b, err := gbase64.DecodeString(content)
  99. if err != nil {
  100. return nil, err
  101. }
  102. data, err = gcompress.UnGzip(b)
  103. if err != nil {
  104. return nil, err
  105. }
  106. } else {
  107. data, err = gcompress.UnGzip([]byte(content))
  108. if err != nil {
  109. return nil, err
  110. }
  111. }
  112. reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
  113. if err != nil {
  114. return nil, err
  115. }
  116. array := make([]*File, len(reader.File))
  117. for i, file := range reader.File {
  118. array[i] = &File{file: file}
  119. }
  120. return array, nil
  121. }
  122. // isBase64 checks and returns whether given content <s> is base64 string.
  123. // It returns true if <s> is base64 string, or false if not.
  124. func isBase64(s string) bool {
  125. var r bool
  126. for i := 0; i < len(s); i++ {
  127. r = (s[i] >= '0' && s[i] <= '9') ||
  128. (s[i] >= 'a' && s[i] <= 'z') ||
  129. (s[i] >= 'A' && s[i] <= 'Z') ||
  130. (s[i] == '+' || s[i] == '-') ||
  131. (s[i] == '_' || s[i] == '/') || s[i] == '='
  132. if !r {
  133. return false
  134. }
  135. }
  136. return true
  137. }
  138. // isHexStr checks and returns whether given content <s> is hex string.
  139. // It returns true if <s> is hex string, or false if not.
  140. func isHexStr(s string) bool {
  141. var r bool
  142. for i := 0; i < len(s); i++ {
  143. r = (s[i] >= '0' && s[i] <= '9') ||
  144. (s[i] >= 'a' && s[i] <= 'f') ||
  145. (s[i] >= 'A' && s[i] <= 'F')
  146. if !r {
  147. return false
  148. }
  149. }
  150. return true
  151. }
  152. // hexStrToBytes converts hex string content to []byte.
  153. func hexStrToBytes(s string) []byte {
  154. src := []byte(s)
  155. dst := make([]byte, hex.DecodedLen(len(src)))
  156. hex.Decode(dst, src)
  157. return dst
  158. }