Skip to content

Commit b6a7ee2

Browse files
authored
Release 0.6.3: PRs 142, 143, 144, 145 (#146)
1 parent 9042e4c commit b6a7ee2

File tree

13 files changed

+301
-154
lines changed

13 files changed

+301
-154
lines changed

appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Experience the future of data storage with 5GB of free, decentralized storage on
8989
This program is licensed under the AGPLv3 or later.
9090
9191
]]></description>
92-
<version>0.6.2</version>
92+
<version>0.6.3</version>
9393
<licence>agpl</licence>
9494
<author>MetaProvide</author>
9595
<namespace>Files_External_Ethswarm</namespace>

lib/AppInfo/Application.php

Lines changed: 15 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -25,49 +25,31 @@
2525

2626
use OC\Security\CSP\ContentSecurityPolicy;
2727
use OCA\Files\Event\LoadAdditionalScriptsEvent;
28-
use OCA\Files_External_Ethswarm\Exception\BaseException;
28+
use OCA\Files_External_Ethswarm\Listener\PreventExternalStorageDisableListener;
2929
use 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;
3331
use OCP\AppFramework\Bootstrap\IBootContext;
34-
use OCP\AppFramework\Bootstrap\IBootstrap;
3532
use OCP\AppFramework\Bootstrap\IRegistrationContext;
3633
use OCP\EventDispatcher\IEventDispatcher;
3734
use OCP\Exceptions\AppConfigException;
38-
use OCP\IConfig;
39-
use OCP\IURLGenerator;
4035
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
4136
use 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
}

lib/AppInfo/BaseApp.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace OCA\Files_External_Ethswarm\AppInfo;
4+
5+
use OCP\AppFramework\App;
6+
use OCP\AppFramework\Bootstrap\IBootstrap;
7+
use Psr\Container\ContainerInterface;
8+
use Psr\Log\LoggerInterface;
9+
10+
abstract class BaseApp extends App implements IBootstrap {
11+
use Dependency;
12+
use Telemetry;
13+
14+
public ContainerInterface $container;
15+
protected LoggerInterface $logger;
16+
17+
public function __construct() {
18+
parent::__construct(Application::NAME);
19+
$this->container = $this->getContainer();
20+
$this->logger = $this->container->get(LoggerInterface::class);
21+
}
22+
}

lib/AppInfo/Dependency.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace OCA\Files_External_Ethswarm\AppInfo;
4+
5+
use Exception;
6+
use OC\Installer;
7+
use OCA\Files_External_Ethswarm\Exception\AppDependencyException;
8+
use OCP\App\IAppManager;
9+
use OCP\IDBConnection;
10+
11+
trait Dependency {
12+
protected function installApp(string $app, ?string $table = null): void {
13+
/** @var IAppManager $appManager */
14+
$appManager = $this->container->get(IAppManager::class);
15+
16+
/** @var Installer $installer */
17+
$installer = $this->container->get(Installer::class);
18+
19+
if (!$appManager->isInstalled($app)) {
20+
$this->logger->info($app.' is not installed, installing it now');
21+
22+
try {
23+
// Installing
24+
if ($table) {
25+
if (!$this->checkTableExists($table)) {
26+
$installer->installApp($app);
27+
}
28+
if (!$this->checkTableExists($table)) {
29+
$this->logger->error($app.' table '.$table.' does not exist, '.$app.' installation failed');
30+
31+
throw new AppDependencyException();
32+
}
33+
} else {
34+
$installer->installApp($app);
35+
}
36+
37+
// Enabling
38+
if (!$appManager->isInstalled($app)) {
39+
$this->logger->info($app.' is not enabled, enabling it now');
40+
$appManager->enableApp($app);
41+
42+
if (!$appManager->isInstalled($app)) {
43+
$this->logger->warning('Try enabling '.$app.' forcefully');
44+
$appManager->enableApp($app, true);
45+
46+
if (!$appManager->isInstalled($app)) {
47+
$this->logger->error($app.' enabling failed');
48+
49+
throw new AppDependencyException();
50+
}
51+
}
52+
}
53+
54+
// Loading
55+
if (!$appManager->isAppLoaded($app)) {
56+
$this->logger->info($app.' is not loaded, loading it now');
57+
$appManager->loadApp($app);
58+
}
59+
if (!$appManager->isAppLoaded($app)) {
60+
$this->logger->error($app.' loading failed');
61+
62+
throw new AppDependencyException();
63+
}
64+
} catch (Exception $e) {
65+
$this->logger->error($e->getMessage());
66+
67+
throw new AppDependencyException($e->getMessage());
68+
}
69+
}
70+
}
71+
72+
private function checkTableExists(string $table): bool {
73+
/** @var IDBConnection $connection */
74+
$connection = $this->container->get(IDBConnection::class);
75+
76+
return $connection->tableExists($table);
77+
}
78+
}

lib/AppInfo/Telemetry.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace OCA\Files_External_Ethswarm\AppInfo;
4+
5+
use OCA\Files_External_Ethswarm\Exception\BaseException;
6+
use OCA\Files_External_Ethswarm\Utils\Env;
7+
use OCP\App\IAppManager;
8+
use OCP\IConfig;
9+
use OCP\IURLGenerator;
10+
use Sentry;
11+
use Sentry\State;
12+
13+
trait Telemetry {
14+
public const TELEMETRY_URL = 'https://c46a60056f22db1db257c1d99fa99e5f@sentry.metaprovide.org/2';
15+
public const TELEMETRY_MINIMUM_SUPPORTED_NEXTCLOUD_VERSION = '30.0.0';
16+
17+
protected function loadTelemetry(): void {
18+
// Register autoloader of sentry
19+
$autoloadPath = __DIR__.'/../../vendor-bin/sentry/vendor/autoload.php';
20+
if (!file_exists($autoloadPath)) {
21+
throw new BaseException('Vendor autoload.php not found at: '.$autoloadPath);
22+
}
23+
24+
require_once $autoloadPath;
25+
}
26+
27+
protected function configureTelemetry(): void {
28+
// Initialize Sentry if telemetry is enabled and the nextcloud version is supported
29+
$environment = Env::get('ENV') ?? 'production';
30+
31+
/** @var IConfig $config */
32+
$config = $this->container->get(IConfig::class);
33+
34+
// Get telemetry enabled status and current nextcloud version
35+
$currentNextcloudVersion = $config->getSystemValue('version');
36+
$isSupported = version_compare($currentNextcloudVersion, Application::TELEMETRY_MINIMUM_SUPPORTED_NEXTCLOUD_VERSION, '>=');
37+
38+
// if telemetry is not set, set it to true
39+
// but if it is set to false, don't override it
40+
if ('' === $config->getSystemValue('telemetry.enabled') && $isSupported) {
41+
$config->setSystemValue('telemetry.enabled', false);
42+
$this->logger->info('Telemetry option has not been set, setting it to true');
43+
}
44+
45+
if ($config->getSystemValue('telemetry.enabled') && $isSupported) {
46+
/** @var IAppManager $appManager */
47+
$appManager = $this->container->get(IAppManager::class);
48+
$appInfo = $appManager->getAppInfo(parent::NAME);
49+
$pluginVersion = $appInfo['version'] ?? 'unknown';
50+
51+
/** @var IURLGenerator $urlGenerator */
52+
$urlGenerator = $this->container->get(IURLGenerator::class);
53+
$instanceUrl = $urlGenerator->getAbsoluteURL('/');
54+
55+
Sentry\init([
56+
'dsn' => Application::TELEMETRY_URL,
57+
'traces_sample_rate' => 1.0,
58+
'environment' => $environment,
59+
'default_integrations' => false, // Disable default integrations to avoid sending unnecessary data
60+
'release' => $pluginVersion,
61+
'server_name' => $instanceUrl,
62+
]);
63+
64+
// Set nextcloud version as a Sentry tag
65+
Sentry\configureScope(function (State\Scope $scope) use ($currentNextcloudVersion): void {
66+
$scope->setTag('nextcloud_version', $currentNextcloudVersion);
67+
});
68+
69+
$this->logger->info('Telemetry is enabled and the nextcloud version is supported');
70+
} elseif ($config->getSystemValue('telemetry.enabled') && !$isSupported) {
71+
$this->logger->info('Telemetry is enabled but the nextcloud version '.$currentNextcloudVersion.' is not supported');
72+
} elseif (false === $config->getSystemValue('telemetry.enabled')) {
73+
$this->logger->info('Telemetry is disabled');
74+
}
75+
}
76+
}

lib/Db/SwarmFile.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
use OCP\AppFramework\Db\Entity;
2727

2828
/**
29-
* @method void setFileId(int $fileId)
30-
* @method int getFileId()
3129
* @method void setName(string $name)
3230
* @method string getName()
3331
* @method void setSwarmReference(string $swarmReference)
@@ -50,9 +48,6 @@
5048
* @method int getToken()
5149
*/
5250
class SwarmFile extends Entity {
53-
/** @var null|int */
54-
protected $fileId;
55-
5651
/** @var string */
5752
protected $name;
5853

@@ -84,7 +79,6 @@ class SwarmFile extends Entity {
8479
protected $token;
8580

8681
public function __construct() {
87-
$this->addType('fileId', 'int');
8882
$this->addType('name', 'string');
8983
$this->addType('swarmReference', 'string');
9084
$this->addType('swarmTag', 'string');

0 commit comments

Comments
 (0)