plugin_decoder.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package service
  2. import (
  3. "errors"
  4. "io"
  5. "mime/multipart"
  6. "github.com/gin-gonic/gin"
  7. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
  8. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_packager/decoder"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  11. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  12. "github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
  13. )
  14. func UploadPluginFromPkg(
  15. config *app.Config,
  16. c *gin.Context,
  17. tenant_id string,
  18. dify_pkg_file multipart.File,
  19. verify_signature bool,
  20. ) *entities.Response {
  21. plugin_file, err := io.ReadAll(dify_pkg_file)
  22. if err != nil {
  23. return entities.NewErrorResponse(-500, err.Error())
  24. }
  25. decoder, err := decoder.NewZipPluginDecoder(plugin_file)
  26. if err != nil {
  27. return entities.NewErrorResponse(-500, err.Error())
  28. }
  29. plugin_unique_identifier, err := decoder.UniqueIdentity()
  30. if err != nil {
  31. return entities.NewErrorResponse(-500, err.Error())
  32. }
  33. manager := plugin_manager.Manager()
  34. declaration, err := manager.SavePackage(plugin_unique_identifier, plugin_file)
  35. if err != nil {
  36. return entities.NewErrorResponse(-500, err.Error())
  37. }
  38. if config.ForceVerifyingSignature || verify_signature {
  39. if !declaration.Verified {
  40. return entities.NewErrorResponse(-500, errors.Join(err, errors.New(
  41. "plugin verification has been enabled, and the plugin you want to install has a bad signature",
  42. )).Error())
  43. }
  44. }
  45. return entities.NewSuccessResponse(map[string]any{
  46. "unique_identifier": plugin_unique_identifier,
  47. "manifest": declaration,
  48. })
  49. }
  50. func FetchPluginManifest(
  51. tenant_id string,
  52. plugin_unique_identifier plugin_entities.PluginUniqueIdentifier,
  53. ) *entities.Response {
  54. type ManifestCache struct {
  55. Declaration plugin_entities.PluginDeclaration `json:"declaration"`
  56. }
  57. plugin_manifest_cache, err := cache.AutoGetWithGetter(plugin_unique_identifier.String(), func() (*ManifestCache, error) {
  58. manager := plugin_manager.Manager()
  59. pkg, err := manager.GetPackage(plugin_unique_identifier)
  60. if err != nil {
  61. return nil, err
  62. }
  63. decoder, err := decoder.NewZipPluginDecoder(pkg)
  64. if err != nil {
  65. return nil, err
  66. }
  67. manifest, err := decoder.Manifest()
  68. if err != nil {
  69. return nil, err
  70. }
  71. return &ManifestCache{Declaration: manifest}, nil
  72. })
  73. if err != nil {
  74. return entities.NewErrorResponse(-500, err.Error())
  75. }
  76. return entities.NewSuccessResponse(plugin_manifest_cache)
  77. }