2525
2626use OC \Security \CSP \ContentSecurityPolicy ;
2727use OCA \Files \Event \LoadAdditionalScriptsEvent ;
28- use OCA \Files_External_Ethswarm \Exception \BaseException ;
2928use OCA \Files_External_Ethswarm \Utils \Env ;
30- use OCP \App \AppPathNotFoundException ;
31- use OCP \App \IAppManager ;
32- use OCP \AppFramework \App ;
3329use OCP \AppFramework \Bootstrap \IBootContext ;
34- use OCP \AppFramework \Bootstrap \IBootstrap ;
3530use OCP \AppFramework \Bootstrap \IRegistrationContext ;
3631use OCP \EventDispatcher \IEventDispatcher ;
3732use OCP \Exceptions \AppConfigException ;
38- use OCP \IConfig ;
39- use OCP \IURLGenerator ;
4033use OCP \Security \CSP \AddContentSecurityPolicyEvent ;
4134use OCP \Util ;
42- use Psr \Container \ContainerInterface ;
43- use Psr \Log \LoggerInterface ;
44- use Sentry ;
4535
46- class Application extends App implements IBootstrap {
36+ class Application extends BaseApp {
4737 public const NAME = 'files_external_ethswarm ' ;
4838 public const API_URL = 'app.hejbit.com ' ;
49- public const TELEMETRY_URL =
'https://[email protected] /2 ' ;
50- public const TELEMETRY_MINIMUM_SUPPORTED_NEXTCLOUD_VERSION = '30.0.0 ' ;
51- public ContainerInterface $ container ;
52-
53- private readonly IEventDispatcher $ dispatcher ;
54- private LoggerInterface $ logger ;
55- private IConfig $ config ;
5639
5740 /**
5841 * @throws AppConfigException
5942 */
6043 public function __construct () {
6144 parent ::__construct (Application::NAME );
62- $ this ->logger = $ this ->getContainer ()->get (LoggerInterface::class);
63- $ this ->config = $ this ->getContainer ()->get (IConfig::class);
64- $ this ->dispatcher = $ this ->getContainer ()->getServer ()->get (IEventDispatcher::class);
65-
66- $ this ->enableFilesExternalApp ();
6745 }
6846
6947 public function boot (IBootContext $ context ): void {
70- new ExternalStorage ($ this ->getContainer (), $ context );
48+ $ this ->installApp ('files_external ' , 'external_mounts ' );
49+
50+ new ExternalStorage ($ this ->container , $ context );
7151
7252 $ this ->loadAssets ($ context );
7353 $ this ->configureTelemetry ();
@@ -76,7 +56,9 @@ public function boot(IBootContext $context): void {
7656 public function register (IRegistrationContext $ context ): void {
7757 $ this ->loadTelemetry ();
7858
79- $ this ->dispatcher ->addListener (
59+ /** @var IEventDispatcher $dispatcher */
60+ $ dispatcher = $ this ->container ->get (IEventDispatcher::class);
61+ $ dispatcher ->addListener (
8062 AddContentSecurityPolicyEvent::class,
8163 function (AddContentSecurityPolicyEvent $ event ): void {
8264 $ policy = new ContentSecurityPolicy ();
@@ -89,36 +71,6 @@ function (AddContentSecurityPolicyEvent $event): void {
8971 );
9072 }
9173
92- /**
93- * @throws AppConfigException
94- */
95- private function enableFilesExternalApp (): void {
96- /** @var IAppManager $appManager */
97- $ appManager = $ this ->getContainer ()->get (IAppManager::class);
98- if (!$ appManager ->isInstalled ('files_external ' )) {
99- try {
100- $ this ->logger ->info ('External Storage Support app is not enabled, enabling it now ' );
101- $ appManager ->enableApp ('files_external ' );
102- if ($ appManager ->isInstalled ('files_external ' )) {
103- $ this ->logger ->info ('External Storage support enabled ' );
104-
105- return ;
106- }
107- $ this ->logger ->warning ('Try enabling it now by force ' );
108- $ appManager ->enableApp ('files_external ' , true );
109- if ($ appManager ->isInstalled ('files_external ' )) {
110- $ this ->logger ->info ('External Storage support enabled ' );
111-
112- return ;
113- }
114- $ this ->logger ->error ('Failed to enable External Storage Support app ' );
115- } catch (AppPathNotFoundException $ e ) {
116- }
117-
118- throw new AppConfigException ('External Storage Support app is required be installed and enabled. Please enable it to use this app. ' );
119- }
120- }
121-
12274 private function loadAssets ($ context ): void {
12375 Util::addStyle (Application::NAME , 'app ' );
12476 Util::addScript (Application::NAME , 'nextcloud-swarm-plugin-settings ' );
@@ -130,61 +82,4 @@ private function loadAssets($context): void {
13082 Util::addInitScript (Application::NAME , 'nextcloud-swarm-plugin-app ' );
13183 });
13284 }
133-
134- private function loadTelemetry (): void {
135- // Register autoloader of sentry
136- $ autoloadPath = __DIR__ .'/../../vendor-bin/sentry/vendor/autoload.php ' ;
137- if (!file_exists ($ autoloadPath )) {
138- throw new BaseException ('Vendor autoload.php not found at: ' .$ autoloadPath );
139- }
140-
141- require_once $ autoloadPath ;
142- }
143-
144- private function configureTelemetry (): void {
145- // Initialize Sentry if telemetry is enabled and the nextcloud version is supported
146- $ environment = Env::get ('ENV ' ) ?? 'production ' ;
147-
148- // Get telemetry enabled status and current nextcloud version
149- $ currentNextcloudVersion = $ this ->config ->getSystemValue ('version ' );
150- $ isSupported = version_compare ($ currentNextcloudVersion , Application::TELEMETRY_MINIMUM_SUPPORTED_NEXTCLOUD_VERSION , '>= ' );
151-
152- // if telemetry is not set, set it to true
153- // but if it is set to false, don't override it
154- if ('' === $ this ->config ->getSystemValue ('telemetry.enabled ' ) && $ isSupported ) {
155- $ this ->config ->setSystemValue ('telemetry.enabled ' , false );
156- $ this ->logger ->info ('Telemetry option has not been set, setting it to true ' );
157- }
158-
159- if ($ this ->config ->getSystemValue ('telemetry.enabled ' ) && $ isSupported ) {
160- /** @var IAppManager $appManager */
161- $ appManager = $ this ->getContainer ()->get (IAppManager::class);
162- $ appInfo = $ appManager ->getAppInfo (self ::NAME );
163- $ pluginVersion = $ appInfo ['version ' ] ?? 'unknown ' ;
164-
165- /** @var IURLGenerator $urlGenerator */
166- $ urlGenerator = $ this ->getContainer ()->get (IURLGenerator::class);
167- $ instanceUrl = $ urlGenerator ->getAbsoluteURL ('/ ' );
168-
169- Sentry \init ([
170- 'dsn ' => Application::TELEMETRY_URL ,
171- 'traces_sample_rate ' => 1.0 ,
172- 'environment ' => $ environment ,
173- 'default_integrations ' => false , // Disable default integrations to avoid sending unnecessary data
174- 'release ' => $ pluginVersion ,
175- 'server_name ' => $ instanceUrl ,
176- ]);
177-
178- // Set nextcloud version as a Sentry tag
179- Sentry \configureScope (function (Sentry \State \Scope $ scope ) use ($ currentNextcloudVersion ): void {
180- $ scope ->setTag ('nextcloud_version ' , $ currentNextcloudVersion );
181- });
182-
183- $ this ->logger ->info ('Telemetry is enabled and the nextcloud version is supported ' );
184- } elseif ($ this ->config ->getSystemValue ('telemetry.enabled ' ) && !$ isSupported ) {
185- $ this ->logger ->info ('Telemetry is enabled but the nextcloud version ' .$ currentNextcloudVersion .' is not supported ' );
186- } elseif (false === $ this ->config ->getSystemValue ('telemetry.enabled ' )) {
187- $ this ->logger ->info ('Telemetry is disabled ' );
188- }
189- }
19085}
0 commit comments