zip.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package decoder
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "io"
  6. "io/fs"
  7. "path"
  8. "strings"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  11. )
  12. type ZipPluginDecoder struct {
  13. PluginDecoder
  14. PluginDecoderHelper
  15. reader *zip.Reader
  16. err error
  17. sig string
  18. create_time int64
  19. }
  20. func NewZipPluginDecoder(binary []byte) (*ZipPluginDecoder, error) {
  21. reader, err := zip.NewReader(bytes.NewReader(binary), int64(len(binary)))
  22. decoder := &ZipPluginDecoder{
  23. reader: reader,
  24. err: err,
  25. }
  26. err = decoder.Open()
  27. if err != nil {
  28. return nil, err
  29. }
  30. if _, err := decoder.Manifest(); err != nil {
  31. return nil, err
  32. }
  33. return decoder, nil
  34. }
  35. func (z *ZipPluginDecoder) Stat(filename string) (fs.FileInfo, error) {
  36. f, err := z.reader.Open(filename)
  37. if err != nil {
  38. return nil, err
  39. }
  40. defer f.Close()
  41. return f.Stat()
  42. }
  43. func (z *ZipPluginDecoder) Open() error {
  44. if z.reader == nil {
  45. return z.err
  46. }
  47. return nil
  48. }
  49. func (z *ZipPluginDecoder) Walk(fn func(filename string, dir string) error) error {
  50. if z.reader == nil {
  51. return z.err
  52. }
  53. for _, file := range z.reader.File {
  54. // split the path into directory and filename
  55. dir, filename := path.Split(file.Name)
  56. if err := fn(filename, dir); err != nil {
  57. return err
  58. }
  59. }
  60. return nil
  61. }
  62. func (z *ZipPluginDecoder) Close() error {
  63. return nil
  64. }
  65. func (z *ZipPluginDecoder) ReadFile(filename string) ([]byte, error) {
  66. if z.reader == nil {
  67. return nil, z.err
  68. }
  69. file, err := z.reader.Open(filename)
  70. if err != nil {
  71. return nil, err
  72. }
  73. defer file.Close()
  74. data := new(bytes.Buffer)
  75. _, err = data.ReadFrom(file)
  76. if err != nil {
  77. return nil, err
  78. }
  79. return data.Bytes(), nil
  80. }
  81. func (z *ZipPluginDecoder) ReadDir(dirname string) ([]string, error) {
  82. if z.reader == nil {
  83. return nil, z.err
  84. }
  85. files := make([]string, 0)
  86. dir_name_with_slash := strings.TrimSuffix(dirname, "/") + "/"
  87. for _, file := range z.reader.File {
  88. if strings.HasPrefix(file.Name, dir_name_with_slash) {
  89. files = append(files, file.Name)
  90. }
  91. }
  92. return files, nil
  93. }
  94. func (z *ZipPluginDecoder) FileReader(filename string) (io.ReadCloser, error) {
  95. return z.reader.Open(filename)
  96. }
  97. func (z *ZipPluginDecoder) decode() error {
  98. if z.reader == nil {
  99. return z.err
  100. }
  101. type signatureData struct {
  102. Signature string `json:"signature"`
  103. Time int64 `json:"time"`
  104. }
  105. signature_data, err := parser.UnmarshalJson[signatureData](z.reader.Comment)
  106. if err != nil {
  107. return err
  108. }
  109. plugin_sig := signature_data.Signature
  110. plugin_time := signature_data.Time
  111. z.sig = plugin_sig
  112. z.create_time = plugin_time
  113. return nil
  114. }
  115. func (z *ZipPluginDecoder) Signature() (string, error) {
  116. if z.sig != "" {
  117. return z.sig, nil
  118. }
  119. if z.reader == nil {
  120. return "", z.err
  121. }
  122. err := z.decode()
  123. if err != nil {
  124. return "", err
  125. }
  126. return z.sig, nil
  127. }
  128. func (z *ZipPluginDecoder) CreateTime() (int64, error) {
  129. if z.create_time != 0 {
  130. return z.create_time, nil
  131. }
  132. if z.reader == nil {
  133. return 0, z.err
  134. }
  135. err := z.decode()
  136. if err != nil {
  137. return 0, err
  138. }
  139. return z.create_time, nil
  140. }
  141. func (z *ZipPluginDecoder) Manifest() (plugin_entities.PluginDeclaration, error) {
  142. return z.PluginDecoderHelper.Manifest(z)
  143. }
  144. func (z *ZipPluginDecoder) Assets() (map[string][]byte, error) {
  145. return z.PluginDecoderHelper.Assets(z)
  146. }