plugin_service.py 4.0 KB

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