zip.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. createTime 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. dirNameWithSlash := strings.TrimSuffix(dirname, "/") + "/"
  94. for _, file := range z.reader.File {
  95. if strings.HasPrefix(file.Name, dirNameWithSlash) {
  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. signatureData, err := parser.UnmarshalJson[struct {
  109. Signature string `json:"signature"`
  110. Time int64 `json:"time"`
  111. }](z.reader.Comment)
  112. if err != nil {
  113. return err
  114. }
  115. pluginSig := signatureData.Signature
  116. pluginTime := signatureData.Time
  117. z.sig = pluginSig
  118. z.createTime = pluginTime
  119. return nil
  120. }
  121. func (z *ZipPluginDecoder) Signature() (string, error) {
  122. if z.sig != "" {
  123. return z.sig, nil
  124. }
  125. if z.reader == nil {
  126. return "", z.err
  127. }
  128. err := z.decode()
  129. if err != nil {
  130. return "", err
  131. }
  132. return z.sig, nil
  133. }
  134. func (z *ZipPluginDecoder) CreateTime() (int64, error) {
  135. if z.createTime != 0 {
  136. return z.createTime, nil
  137. }
  138. if z.reader == nil {
  139. return 0, z.err
  140. }
  141. err := z.decode()
  142. if err != nil {
  143. return 0, err
  144. }
  145. return z.createTime, nil
  146. }
  147. func (z *ZipPluginDecoder) Manifest() (plugin_entities.PluginDeclaration, error) {
  148. return z.PluginDecoderHelper.Manifest(z)
  149. }
  150. func (z *ZipPluginDecoder) Assets() (map[string][]byte, error) {
  151. return z.PluginDecoderHelper.Assets(z)
  152. }
  153. func (z *ZipPluginDecoder) Checksum() (string, error) {
  154. return z.PluginDecoderHelper.Checksum(z)
  155. }
  156. func (z *ZipPluginDecoder) UniqueIdentity() (plugin_entities.PluginUniqueIdentifier, error) {
  157. return z.PluginDecoderHelper.UniqueIdentity(z)
  158. }
  159. func (z *ZipPluginDecoder) ExtractTo(dst string) error {
  160. // copy to working directory
  161. if err := z.Walk(func(filename, dir string) error {
  162. workingPath := path.Join(dst, dir)
  163. // check if directory exists
  164. if err := os.MkdirAll(workingPath, 0755); err != nil {
  165. return err
  166. }
  167. bytes, err := z.ReadFile(filepath.Join(dir, filename))
  168. if err != nil {
  169. return err
  170. }
  171. filename = filepath.Join(workingPath, filename)
  172. // copy file
  173. if err := os.WriteFile(filename, bytes, 0644); err != nil {
  174. return err
  175. }
  176. return nil
  177. }); err != nil {
  178. // if error, delete the working directory
  179. os.RemoveAll(dst)
  180. return errors.Join(fmt.Errorf("copy plugin to working directory error: %v", err), err)
  181. }
  182. return nil
  183. }