zip.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. }
  19. func NewZipPluginDecoder(binary []byte) (*ZipPluginDecoder, error) {
  20. reader, err := zip.NewReader(bytes.NewReader(binary), int64(len(binary)))
  21. decoder := &ZipPluginDecoder{
  22. reader: reader,
  23. err: err,
  24. }
  25. err = decoder.Open()
  26. if err != nil {
  27. return nil, err
  28. }
  29. if _, err := decoder.Manifest(); err != nil {
  30. return nil, err
  31. }
  32. return decoder, nil
  33. }
  34. func (z *ZipPluginDecoder) Stat(filename string) (fs.FileInfo, error) {
  35. f, err := z.reader.Open(filename)
  36. if err != nil {
  37. return nil, err
  38. }
  39. defer f.Close()
  40. return f.Stat()
  41. }
  42. func (z *ZipPluginDecoder) Open() error {
  43. if z.reader == nil {
  44. return z.err
  45. }
  46. return nil
  47. }
  48. func (z *ZipPluginDecoder) Walk(fn func(filename string, dir string) error) error {
  49. if z.reader == nil {
  50. return z.err
  51. }
  52. for _, file := range z.reader.File {
  53. // split the path into directory and filename
  54. dir, filename := path.Split(file.Name)
  55. if err := fn(filename, dir); err != nil {
  56. return err
  57. }
  58. }
  59. return nil
  60. }
  61. func (z *ZipPluginDecoder) Close() error {
  62. return nil
  63. }
  64. func (z *ZipPluginDecoder) ReadFile(filename string) ([]byte, error) {
  65. if z.reader == nil {
  66. return nil, z.err
  67. }
  68. file, err := z.reader.Open(filename)
  69. if err != nil {
  70. return nil, err
  71. }
  72. defer file.Close()
  73. data := new(bytes.Buffer)
  74. _, err = data.ReadFrom(file)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return data.Bytes(), nil
  79. }
  80. func (z *ZipPluginDecoder) FileReader(filename string) (io.ReadCloser, error) {
  81. return z.reader.Open(filename)
  82. }
  83. func (z *ZipPluginDecoder) decode() error {
  84. if z.reader == nil {
  85. return z.err
  86. }
  87. type signatureData struct {
  88. Signature string `json:"signature"`
  89. Time int64 `json:"time"`
  90. }
  91. signature_data, err := parser.UnmarshalJson[signatureData](z.reader.Comment)
  92. if err != nil {
  93. return err
  94. }
  95. plugin_sig := signature_data.Signature
  96. plugin_time := signature_data.Time
  97. z.sig = plugin_sig
  98. z.create_time = plugin_time
  99. return nil
  100. }
  101. func (z *ZipPluginDecoder) Signature() (string, error) {
  102. if z.sig != "" {
  103. return z.sig, nil
  104. }
  105. if z.reader == nil {
  106. return "", z.err
  107. }
  108. err := z.decode()
  109. if err != nil {
  110. return "", err
  111. }
  112. return z.sig, nil
  113. }
  114. func (z *ZipPluginDecoder) CreateTime() (int64, error) {
  115. if z.create_time != 0 {
  116. return z.create_time, nil
  117. }
  118. if z.reader == nil {
  119. return 0, z.err
  120. }
  121. err := z.decode()
  122. if err != nil {
  123. return 0, err
  124. }
  125. return z.create_time, nil
  126. }
  127. func (z *ZipPluginDecoder) Manifest() (plugin_entities.PluginDeclaration, error) {
  128. return z.PluginDecoderHelper.Manifest(z)
  129. }