zip.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package decoder
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "io/fs"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "strconv"
  13. "strings"
  14. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  15. "github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
  16. )
  17. type ZipPluginDecoder struct {
  18. PluginDecoder
  19. PluginDecoderHelper
  20. reader *zip.Reader
  21. err error
  22. sig string
  23. createTime int64
  24. }
  25. func NewZipPluginDecoder(binary []byte) (*ZipPluginDecoder, error) {
  26. reader, err := zip.NewReader(bytes.NewReader(binary), int64(len(binary)))
  27. if err != nil {
  28. return nil, errors.New(strings.ReplaceAll(err.Error(), "zip", "difypkg"))
  29. }
  30. decoder := &ZipPluginDecoder{
  31. reader: reader,
  32. err: err,
  33. }
  34. err = decoder.Open()
  35. if err != nil {
  36. return nil, err
  37. }
  38. if _, err := decoder.Manifest(); err != nil {
  39. return nil, err
  40. }
  41. return decoder, nil
  42. }
  43. // NewZipPluginDecoderWithSizeLimit is a helper function to create a ZipPluginDecoder with a size limit
  44. // It checks the total uncompressed size of the plugin package and returns an error if it exceeds the max size
  45. func NewZipPluginDecoderWithSizeLimit(binary []byte, maxSize int64) (*ZipPluginDecoder, error) {
  46. reader, err := zip.NewReader(bytes.NewReader(binary), int64(len(binary)))
  47. if err != nil {
  48. return nil, errors.New(strings.ReplaceAll(err.Error(), "zip", "difypkg"))
  49. }
  50. totalSize := int64(0)
  51. for _, file := range reader.File {
  52. totalSize += int64(file.UncompressedSize64)
  53. if totalSize > maxSize {
  54. return nil, errors.New(
  55. "plugin package size is too large, please ensure the uncompressed size is less than " +
  56. strconv.FormatInt(maxSize, 10) + " bytes",
  57. )
  58. }
  59. }
  60. return NewZipPluginDecoder(binary)
  61. }
  62. func (z *ZipPluginDecoder) Stat(filename string) (fs.FileInfo, error) {
  63. f, err := z.reader.Open(filename)
  64. if err != nil {
  65. return nil, err
  66. }
  67. defer f.Close()
  68. return f.Stat()
  69. }
  70. func (z *ZipPluginDecoder) Open() error {
  71. if z.reader == nil {
  72. return z.err
  73. }
  74. return nil
  75. }
  76. func (z *ZipPluginDecoder) Walk(fn func(filename string, dir string) error) error {
  77. if z.reader == nil {
  78. return z.err
  79. }
  80. for _, file := range z.reader.File {
  81. // split the path into directory and filename
  82. dir, filename := path.Split(file.Name)
  83. if err := fn(filename, dir); err != nil {
  84. return err
  85. }
  86. }
  87. return nil
  88. }
  89. func (z *ZipPluginDecoder) Close() error {
  90. return nil
  91. }
  92. func (z *ZipPluginDecoder) ReadFile(filename string) ([]byte, error) {
  93. if z.reader == nil {
  94. return nil, z.err
  95. }
  96. file, err := z.reader.Open(filename)
  97. if err != nil {
  98. return nil, err
  99. }
  100. defer file.Close()
  101. data := new(bytes.Buffer)
  102. _, err = data.ReadFrom(file)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return data.Bytes(), nil
  107. }
  108. func (z *ZipPluginDecoder) ReadDir(dirname string) ([]string, error) {
  109. if z.reader == nil {
  110. return nil, z.err
  111. }
  112. files := make([]string, 0)
  113. dirNameWithSlash := strings.TrimSuffix(dirname, "/") + "/"
  114. for _, file := range z.reader.File {
  115. if strings.HasPrefix(file.Name, dirNameWithSlash) {
  116. files = append(files, file.Name)
  117. }
  118. }
  119. return files, nil
  120. }
  121. func (z *ZipPluginDecoder) FileReader(filename string) (io.ReadCloser, error) {
  122. return z.reader.Open(filename)
  123. }
  124. func (z *ZipPluginDecoder) decode() error {
  125. if z.reader == nil {
  126. return z.err
  127. }
  128. signatureData, err := parser.UnmarshalJson[struct {
  129. Signature string `json:"signature"`
  130. Time int64 `json:"time"`
  131. }](z.reader.Comment)
  132. if err != nil {
  133. return err
  134. }
  135. pluginSig := signatureData.Signature
  136. pluginTime := signatureData.Time
  137. z.sig = pluginSig
  138. z.createTime = pluginTime
  139. return nil
  140. }
  141. func (z *ZipPluginDecoder) Signature() (string, error) {
  142. if z.sig != "" {
  143. return z.sig, nil
  144. }
  145. if z.reader == nil {
  146. return "", z.err
  147. }
  148. err := z.decode()
  149. if err != nil {
  150. return "", err
  151. }
  152. return z.sig, nil
  153. }
  154. func (z *ZipPluginDecoder) CreateTime() (int64, error) {
  155. if z.createTime != 0 {
  156. return z.createTime, nil
  157. }
  158. if z.reader == nil {
  159. return 0, z.err
  160. }
  161. err := z.decode()
  162. if err != nil {
  163. return 0, err
  164. }
  165. return z.createTime, nil
  166. }
  167. func (z *ZipPluginDecoder) Manifest() (plugin_entities.PluginDeclaration, error) {
  168. return z.PluginDecoderHelper.Manifest(z)
  169. }
  170. func (z *ZipPluginDecoder) Assets() (map[string][]byte, error) {
  171. return z.PluginDecoderHelper.Assets(z)
  172. }
  173. func (z *ZipPluginDecoder) Checksum() (string, error) {
  174. return z.PluginDecoderHelper.Checksum(z)
  175. }
  176. func (z *ZipPluginDecoder) UniqueIdentity() (plugin_entities.PluginUniqueIdentifier, error) {
  177. return z.PluginDecoderHelper.UniqueIdentity(z)
  178. }
  179. func (z *ZipPluginDecoder) ExtractTo(dst string) error {
  180. // copy to working directory
  181. if err := z.Walk(func(filename, dir string) error {
  182. workingPath := path.Join(dst, dir)
  183. // check if directory exists
  184. if err := os.MkdirAll(workingPath, 0755); err != nil {
  185. return err
  186. }
  187. bytes, err := z.ReadFile(filepath.Join(dir, filename))
  188. if err != nil {
  189. return err
  190. }
  191. filename = filepath.Join(workingPath, filename)
  192. // copy file
  193. if err := os.WriteFile(filename, bytes, 0644); err != nil {
  194. return err
  195. }
  196. return nil
  197. }); err != nil {
  198. // if error, delete the working directory
  199. os.RemoveAll(dst)
  200. return errors.Join(fmt.Errorf("copy plugin to working directory error: %v", err), err)
  201. }
  202. return nil
  203. }
  204. func (z *ZipPluginDecoder) CheckAssetsValid() error {
  205. return z.PluginDecoderHelper.CheckAssetsValid(z)
  206. }