2525
2626use OC \Security \CSP \ContentSecurityPolicy ;
2727use OCA \Files \Event \LoadAdditionalScriptsEvent ;
28- use OCA \Files_External_Ethswarm \Exception \ BaseException ;
28+ use OCA \Files_External_Ethswarm \Listener \ PreventExternalStorageDisableListener ;
2929use OCA \Files_External_Ethswarm \Utils \Env ;
30- use OCP \App \AppPathNotFoundException ;
31- use OCP \App \IAppManager ;
32- use OCP \AppFramework \App ;
30+ use OCP \App \Events \AppDisableEvent ;
3331use OCP \AppFramework \Bootstrap \IBootContext ;
34- use OCP \AppFramework \Bootstrap \IBootstrap ;
3532use OCP \AppFramework \Bootstrap \IRegistrationContext ;
3633use OCP \EventDispatcher \IEventDispatcher ;
3734use OCP \Exceptions \AppConfigException ;
38- use OCP \IConfig ;
39- use OCP \IURLGenerator ;
4035use OCP \Security \CSP \AddContentSecurityPolicyEvent ;
4136use OCP \Util ;
42- use Psr \Container \ContainerInterface ;
43- use Psr \Log \LoggerInterface ;
44- use Sentry ;
4537
46- class Application extends App implements IBootstrap {
38+ class Application extends BaseApp {
4739 public const NAME = 'files_external_ethswarm ' ;
4840 public const API_URL = 'app.hejbit.com ' ;
49- public const TELEMETRY_URL = 'https://c46a60056f22db1db257c1d99fa99e5f@sentry.metaprovide.org/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 ;
5641
5742 /**
5843 * @throws AppConfigException
5944 */
6045 public function __construct () {
6146 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 ();
6747 }
6848
6949 public function boot (IBootContext $ context ): void {
70- new ExternalStorage ($ this ->getContainer (), $ context );
50+ $ this ->installApp ('files_external ' , 'external_mounts ' );
51+
52+ new ExternalStorage ($ this ->container , $ context );
7153
7254 $ this ->loadAssets ($ context );
7355 $ this ->configureTelemetry ();
@@ -76,7 +58,15 @@ public function boot(IBootContext $context): void {
7658 public function register (IRegistrationContext $ context ): void {
7759 $ this ->loadTelemetry ();
7860
79- $ this ->dispatcher ->addListener (
61+ // Register the listener to prevent files_external from being disabled
62+ $ context ->registerEventListener (
63+ AppDisableEvent::class,
64+ PreventExternalStorageDisableListener::class
65+ );
66+
67+ /** @var IEventDispatcher $dispatcher */
68+ $ dispatcher = $ this ->container ->get (IEventDispatcher::class);
69+ $ dispatcher ->addListener (
8070 AddContentSecurityPolicyEvent::class,
8171 function (AddContentSecurityPolicyEvent $ event ): void {
8272 $ policy = new ContentSecurityPolicy ();
@@ -89,36 +79,6 @@ function (AddContentSecurityPolicyEvent $event): void {
8979 );
9080 }
9181
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-
12282 private function loadAssets ($ context ): void {
12383 Util::addStyle (Application::NAME , 'app ' );
12484 Util::addScript (Application::NAME , 'nextcloud-swarm-plugin-settings ' );
@@ -130,61 +90,4 @@ private function loadAssets($context): void {
13090 Util::addInitScript (Application::NAME , 'nextcloud-swarm-plugin-app ' );
13191 });
13292 }
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- }
19093}
0 commit comments