plugin_service.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from collections.abc import Generator
  2. from mimetypes import guess_type
  3. from core.helper.download import download_with_size_limit
  4. from core.plugin.entities.plugin import PluginEntity, PluginInstallationSource
  5. from core.plugin.entities.plugin_daemon import InstallPluginMessage, PluginDaemonInnerError
  6. from core.plugin.manager.asset import PluginAssetManager
  7. from core.plugin.manager.debugging import PluginDebuggingManager
  8. from core.plugin.manager.plugin import PluginInstallationManager
  9. class PluginService:
  10. @staticmethod
  11. def get_debugging_key(tenant_id: str) -> str:
  12. manager = PluginDebuggingManager()
  13. return manager.get_debugging_key(tenant_id)
  14. @staticmethod
  15. def list(tenant_id: str) -> list[PluginEntity]:
  16. manager = PluginInstallationManager()
  17. return manager.list_plugins(tenant_id)
  18. @staticmethod
  19. def get_asset(tenant_id: str, asset_file: str) -> tuple[bytes, str]:
  20. manager = PluginAssetManager()
  21. # guess mime type
  22. mime_type, _ = guess_type(asset_file)
  23. return manager.fetch_asset(tenant_id, asset_file), mime_type or "application/octet-stream"
  24. @staticmethod
  25. def check_plugin_unique_identifier(tenant_id: str, plugin_unique_identifier: str) -> bool:
  26. manager = PluginInstallationManager()
  27. return manager.fetch_plugin_by_identifier(tenant_id, plugin_unique_identifier)
  28. @staticmethod
  29. def install_from_unique_identifier(tenant_id: str, plugin_unique_identifier: str) -> bool:
  30. manager = PluginInstallationManager()
  31. return manager.install_from_identifier(tenant_id, plugin_unique_identifier)
  32. @staticmethod
  33. def install_from_local_pkg(tenant_id: str, pkg: bytes) -> Generator[InstallPluginMessage, None, None]:
  34. """
  35. Install plugin from uploaded package files
  36. """
  37. manager = PluginInstallationManager()
  38. try:
  39. yield from manager.install_from_pkg(tenant_id, pkg, PluginInstallationSource.Package, {})
  40. except PluginDaemonInnerError as e:
  41. yield InstallPluginMessage(event=InstallPluginMessage.Event.Error, data=str(e.message))
  42. @staticmethod
  43. def install_from_github_pkg(
  44. tenant_id: str, repo: str, version: str, package: str
  45. ) -> Generator[InstallPluginMessage, None, None]:
  46. """
  47. Install plugin from github release package files
  48. """
  49. pkg = download_with_size_limit(
  50. f"https://github.com/{repo}/releases/download/{version}/{package}", 15 * 1024 * 1024
  51. )
  52. manager = PluginInstallationManager()
  53. try:
  54. yield from manager.install_from_pkg(
  55. tenant_id,
  56. pkg,
  57. PluginInstallationSource.Github,
  58. {
  59. "repo": repo,
  60. "version": version,
  61. "package": package,
  62. },
  63. )
  64. except PluginDaemonInnerError as e:
  65. yield InstallPluginMessage(event=InstallPluginMessage.Event.Error, data=str(e.message))
  66. @staticmethod
  67. def install_from_marketplace_pkg(
  68. tenant_id: str, plugin_unique_identifier: str
  69. ) -> Generator[InstallPluginMessage, None, None]:
  70. """
  71. TODO: wait for marketplace api
  72. """
  73. manager = PluginInstallationManager()
  74. pkg = b""
  75. try:
  76. yield from manager.install_from_pkg(
  77. tenant_id,
  78. pkg,
  79. PluginInstallationSource.Marketplace,
  80. {
  81. "plugin_unique_identifier": plugin_unique_identifier,
  82. },
  83. )
  84. except PluginDaemonInnerError as e:
  85. yield InstallPluginMessage(event=InstallPluginMessage.Event.Error, data=str(e.message))
  86. @staticmethod
  87. def uninstall(tenant_id: str, plugin_installation_id: str) -> bool:
  88. manager = PluginInstallationManager()
  89. return manager.uninstall(tenant_id, plugin_installation_id)