zip.go 2.5 KB

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