zip.go 4.3 KB

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