plugin_decoder.go 2.5 KB

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