zip.go 4.3 KB

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