zip.go 4.4 KB

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