Skip to content

Commit 9ef1f18

Browse files
committed
Change relevant Controllers to use GetStorageUrlTrait and remove duplicate declarations of getStorageUrl().
1 parent cc47041 commit 9ef1f18

6 files changed

Lines changed: 26 additions & 60 deletions

File tree

solid/lib/Controller/AppController.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,23 @@
1515
use OCP\IUserManager;
1616

1717
class AppController extends Controller {
18+
use GetStorageUrlTrait;
19+
20+
protected ServerConfig $config;
21+
protected IURLGenerator $urlGenerator;
22+
1823
private $userId;
1924
private $userManager;
20-
private $urlGenerator;
21-
private $config;
2225

2326
public function __construct($AppName, IRequest $request, IConfig $config, IUserManager $userManager, IManager $contactsManager, IURLGenerator $urlGenerator, $userId
24-
// , bool $userDomains
2527
){
2628
parent::__construct($AppName, $request);
2729
$this->userId = $userId;
2830
$this->userManager = $userManager;
2931
$this->contactsManager = $contactsManager;
3032
$this->request = $request;
3133
$this->urlGenerator = $urlGenerator;
32-
$this->config = new \OCA\Solid\ServerConfig($config, $urlGenerator, $userManager);
33-
// $this->userDomains = $userDomains;
34+
$this->config = new ServerConfig($config, $urlGenerator, $userManager);
3435
}
3536

3637
private function getUserApps($userId) {
@@ -67,14 +68,7 @@ private function getAppsList() {
6768
private function getProfilePage() {
6869
return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("solid.profile.handleGet", array("userId" => $this->userId, "path" => "/card"))) . "#me";
6970
}
70-
private function getStorageUrl($userId) {
71-
$storageUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("solid.storage.handleHead", array("userId" => $userId, "path" => "foo")));
72-
$storageUrl = preg_replace('/foo$/', '', $storageUrl);
73-
// if ($this->userDomains) {
74-
$storageUrl = $userId.'.'.$storageUrl;
75-
// }
76-
return $storageUrl;
77-
}
71+
7872
/**
7973
* @NoAdminRequired
8074
* @NoCSRFRequired

solid/lib/Controller/GetStorageUrlTrait.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace OCA\Solid\Controller;
44

5-
use OCA\Solid\BaseServerConfig;
5+
use OCA\Solid\ServerConfig;
66
use OCP\IURLGenerator;
77

88
trait GetStorageUrlTrait
99
{
1010
//////////////////////////// GETTERS AND SETTERS \\\\\\\\\\\\\\\\\\\\\\\\\\\
1111

12-
final public function setConfig(BaseServerConfig $config): void
12+
final public function setConfig(ServerConfig $config): void
1313
{
1414
$this->config = $config;
1515
}
@@ -21,13 +21,12 @@ final public function setUrlGenerator(IURLGenerator $urlGenerator): void
2121

2222
////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\
2323

24-
protected BaseServerConfig $config;
24+
protected ServerConfig $config;
2525
protected IURLGenerator $urlGenerator;
2626

2727
/////////////////////////////// PROTECTED API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
2828

2929
/**
30-
* @FIXME: Deduplicate multiple declarations of getStorageUrl()
3130
* @FIXME: Add check for bob.nextcloud.local/solid/alice to throw 404
3231
* @TODO: Use route without `@alice` in /apps/solid/@alice/profile/card#me when user-domains are enabled
3332
*/

solid/lib/Controller/ProfileController.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use OCA\Solid\DpopFactoryTrait;
55
use OCA\Solid\PlainResponse;
66
use OCA\Solid\Notifications\SolidNotifications;
7+
use OCA\Solid\ServerConfig;
78

89
use OCP\AppFramework\Controller;
910
use OCP\AppFramework\Http;
@@ -21,9 +22,10 @@
2122

2223
class ProfileController extends Controller {
2324
use DpopFactoryTrait;
25+
use GetStorageUrlTrait;
2426

25-
/* @var IURLGenerator */
26-
private $urlGenerator;
27+
protected ServerConfig $config;
28+
protected IURLGenerator $urlGenerator;
2729

2830
/* @var ISession */
2931
private $session;
@@ -131,26 +133,6 @@ private function getProfileUrl($userId) {
131133
$profileUrl = preg_replace('/foo$/', '', $profileUrl);
132134
return $profileUrl;
133135
}
134-
135-
private function build_url(array $parts) {
136-
return (isset($parts['scheme']) ? "{$parts['scheme']}:" : '') .
137-
(isset($parts['host']) ? "//{$parts['host']}" : '') .
138-
(isset($parts['port']) ? ":{$parts['port']}" : '') .
139-
(isset($parts['path']) ? "{$parts['path']}" : '') .
140-
(isset($parts['query']) ? "?{$parts['query']}" : '') .
141-
(isset($parts['fragment']) ? "#{$parts['fragment']}" : '');
142-
}
143-
144-
private function getStorageUrl($userId) {
145-
$storageUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("solid.storage.handleHead", array("userId" => $userId, "path" => "foo")));
146-
$storageUrl = preg_replace('/foo$/', '/', $storageUrl);
147-
// if ($this->userDomains) {
148-
$url = parse_url($storageUrl);
149-
$url['host'] = $userId.'.'.$url['host'];
150-
$storageUrl = $this->build_url($url);
151-
// }
152-
return $storageUrl;
153-
}
154136

155137
/**
156138
* @PublicPage

solid/lib/Controller/SolidWebhookController.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use OCA\Solid\DpopFactoryTrait;
88
use OCA\Solid\PlainResponse;
9+
use OCA\Solid\ServerConfig;
910
use OCA\Solid\Service\SolidWebhookService;
1011

1112
use OCP\AppFramework\Controller;
@@ -23,9 +24,10 @@
2324

2425
class SolidWebhookController extends Controller {
2526
use DpopFactoryTrait;
27+
use GetStorageUrlTrait;
2628

27-
/* @var IURLGenerator */
28-
private $urlGenerator;
29+
protected ServerConfig $config;
30+
protected IURLGenerator $urlGenerator;
2931

3032
/* @var ISession */
3133
private $session;
@@ -137,11 +139,6 @@ private function getFileSystem() {
137139
return $filesystem;
138140
}
139141

140-
private function getStorageUrl($userId) {
141-
$storageUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("solid.storage.handleHead", array("userId" => $userId, "path" => "foo")));
142-
$storageUrl = preg_replace('/foo$/', '', $storageUrl);
143-
return $storageUrl;
144-
}
145142
private function getAppBaseUrl() {
146143
$appBaseUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("solid.app.appLauncher"));
147144
return $appBaseUrl;

solid/lib/Controller/StorageController.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use OCA\Solid\DpopFactoryTrait;
66
use OCA\Solid\Notifications\SolidNotifications;
77
use OCA\Solid\PlainResponse;
8+
use OCA\Solid\ServerConfig;
89

910
use OCP\AppFramework\Controller;
1011
use OCP\AppFramework\Http;
@@ -22,11 +23,12 @@
2223

2324
class StorageController extends Controller
2425
{
25-
use DpopFactoryTrait;
2626
use BearerFactoryTrait;
27+
use DpopFactoryTrait;
28+
use GetStorageUrlTrait;
2729

28-
/* @var IURLGenerator */
29-
private $urlGenerator;
30+
protected ServerConfig $config;
31+
protected IURLGenerator $urlGenerator;
3032

3133
/* @var ISession */
3234
private $session;
@@ -89,14 +91,6 @@ private function getFileSystem() {
8991
private function getUserProfile($userId) {
9092
return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("solid.profile.handleGet", array("userId" => $userId, "path" => "/card"))) . "#me";
9193
}
92-
private function getStorageUrl($userId) {
93-
$storageUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("solid.storage.handleHead", array("userId" => $userId, "path" => "foo")));
94-
$storageUrl = preg_replace('/foo$/', '', $storageUrl);
95-
// if ($this->userDomains) {
96-
$storageUrl = $userId.'.'.$storageUrl;
97-
// }
98-
return $storageUrl;
99-
}
10094

10195
private function generateDefaultAcl($userId) {
10296
$defaultAcl = <<< EOF

solid/tests/Unit/Controller/GetStorageUrlTraitTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace OCA\Solid\Controller;
44

55
use Error;
6-
use OCA\Solid\BaseServerConfig;
6+
use OCA\Solid\ServerConfig;
77
use OCP\IURLGenerator;
88
use PHPUnit\Framework\MockObject\MockObject;
99
use PHPUnit\Framework\TestCase;
@@ -110,9 +110,9 @@ public function testGetStorageUrlWithUserSubDomainsEnabled($url, $userId, $expec
110110

111111
////////////////////////////// MOCKS AND STUBS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
112112

113-
public function getMockConfig($enabled = false): MockObject|BaseServerConfig
113+
public function getMockConfig($enabled = false): MockObject|ServerConfig
114114
{
115-
$mockConfig = $this->getMockBuilder(BaseServerConfig::class)
115+
$mockConfig = $this->getMockBuilder(ServerConfig::class)
116116
->disableOriginalConstructor()
117117
->getMock();
118118

0 commit comments

Comments
 (0)