plugins.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package controllers
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager"
  6. "github.com/langgenius/dify-plugin-daemon/internal/service"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  10. )
  11. func GetAsset(c *gin.Context) {
  12. plugin_manager := plugin_manager.Manager()
  13. asset, err := plugin_manager.GetAsset(c.Param("id"))
  14. if err != nil {
  15. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  16. return
  17. }
  18. c.Data(http.StatusOK, "application/octet-stream", asset)
  19. }
  20. func InstallPluginFromPkg(app *app.Config) gin.HandlerFunc {
  21. return func(c *gin.Context) {
  22. dify_pkg_file_header, err := c.FormFile("dify_pkg")
  23. if err != nil {
  24. c.JSON(http.StatusOK, entities.NewErrorResponse(-400, err.Error()))
  25. return
  26. }
  27. tenant_id := c.PostForm("tenant_id")
  28. if tenant_id == "" {
  29. c.JSON(http.StatusOK, entities.NewErrorResponse(-400, "Tenant ID is required"))
  30. return
  31. }
  32. if dify_pkg_file_header.Size > app.MaxPluginPackageSize {
  33. c.JSON(http.StatusOK, entities.NewErrorResponse(-413, "File size exceeds the maximum limit"))
  34. return
  35. }
  36. dify_pkg_file, err := dify_pkg_file_header.Open()
  37. if err != nil {
  38. c.JSON(http.StatusOK, entities.NewErrorResponse(-500, err.Error()))
  39. return
  40. }
  41. defer dify_pkg_file.Close()
  42. service.InstallPluginFromPkg(c, tenant_id, dify_pkg_file)
  43. }
  44. }
  45. func InstallPluginFromIdentifier(app *app.Config) gin.HandlerFunc {
  46. return func(c *gin.Context) {
  47. BindRequest(c, func(request struct {
  48. TenantID string `json:"tenant_id" binding:"required"`
  49. PluginUniqueIdentifier plugin_entities.PluginUniqueIdentifier `json:"plugin_unique_identifier" binding:"required" validate:"plugin_unique_identifier"`
  50. }) {
  51. c.JSON(http.StatusOK, service.InstallPluginFromIdentifier(c, request.TenantID, request.PluginUniqueIdentifier))
  52. })
  53. }
  54. }
  55. func UninstallPlugin(c *gin.Context) {
  56. }
  57. func ListPlugins(c *gin.Context) {
  58. }