zip.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package decoder
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "io"
  6. "io/fs"
  7. "path"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  10. )
  11. type ZipPluginDecoder struct {
  12. PluginDecoder
  13. PluginDecoderHelper
  14. reader *zip.Reader
  15. err error
  16. sig string
  17. create_time int64
  18. pluginDeclaration *plugin_entities.PluginDeclaration
  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) FileReader(filename string) (io.ReadCloser, error) {
  82. return z.reader.Open(filename)
  83. }
  84. func (z *ZipPluginDecoder) decode() error {
  85. if z.reader == nil {
  86. return z.err
  87. }
  88. type signatureData struct {
  89. Signature string `json:"signature"`
  90. Time int64 `json:"time"`
  91. }
  92. signature_data, err := parser.UnmarshalJson[signatureData](z.reader.Comment)
  93. if err != nil {
  94. return err
  95. }
  96. plugin_sig := signature_data.Signature
  97. plugin_time := signature_data.Time
  98. z.sig = plugin_sig
  99. z.create_time = plugin_time
  100. return nil
  101. }
  102. func (z *ZipPluginDecoder) Signature() (string, error) {
  103. if z.sig != "" {
  104. return z.sig, nil
  105. }
  106. if z.reader == nil {
  107. return "", z.err
  108. }
  109. err := z.decode()
  110. if err != nil {
  111. return "", err
  112. }
  113. return z.sig, nil
  114. }
  115. func (z *ZipPluginDecoder) CreateTime() (int64, error) {
  116. if z.create_time != 0 {
  117. return z.create_time, nil
  118. }
  119. if z.reader == nil {
  120. return 0, z.err
  121. }
  122. err := z.decode()
  123. if err != nil {
  124. return 0, err
  125. }
  126. return z.create_time, nil
  127. }
  128. func (z *ZipPluginDecoder) Manifest() (plugin_entities.PluginDeclaration, error) {
  129. return z.PluginDecoderHelper.Manifest(z)
  130. }