gres_func.go 4.5 KB

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