plugins.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. BindRequestWithPluginUniqueIdentifier(c, func(request struct {
  48. TenantID string `json:"tenant_id" binding:"required"`
  49. }, identifier plugin_entities.PluginUniqueIdentifier) {
  50. // TODO
  51. })
  52. }
  53. }
  54. func UninstallPlugin(c *gin.Context) {
  55. }
  56. func ListPlugins(c *gin.Context) {
  57. }