Skip to content

Commit 85d0c6f

Browse files
committed
refactor: FavoritesController
Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent 486bbff commit 85d0c6f

7 files changed

Lines changed: 145 additions & 120 deletions

File tree

lib/Controller/DevicesController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
2121
use OCP\AppFramework\Http\DataResponse;
2222
use OCP\AppFramework\Services\IAppConfig;
23+
use OCP\Files\File;
2324
use OCP\Files\Folder;
2425
use OCP\Files\IRootFolder;
2526
use OCP\Files\NotFoundException;
@@ -213,9 +214,8 @@ public function importDevices(string $path): DataResponse {
213214

214215
if ($userFolder->nodeExists($cleanpath)) {
215216
$file = $userFolder->get($cleanpath);
216-
if ($file->getType() === \OCP\Files\FileInfo::TYPE_FILE
217-
and $file->isReadable()) {
218-
$lowerFileName = strtolower((string)$file->getName());
217+
if ($file instanceof File && $file->isReadable()) {
218+
$lowerFileName = strtolower($file->getName());
219219
if (str_ends_with($lowerFileName, '.gpx') || str_ends_with($lowerFileName, '.kml') || str_ends_with($lowerFileName, '.kmz')) {
220220
$nbImported = $this->devicesService->importDevices($this->userId, $file);
221221
return new DataResponse($nbImported);

lib/Controller/FavoritesController.php

Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,46 @@
1414

1515
namespace OCA\Maps\Controller;
1616

17+
use OCA\Maps\DB\FavoriteShareMapper;
1718
use OCA\Maps\Service\FavoritesService;
18-
use OCP\App\IAppManager;
1919
use OCP\AppFramework\Controller;
2020
use OCP\AppFramework\Db\DoesNotExistException;
2121
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
2222
use OCP\AppFramework\Http;
23+
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
2324
use OCP\AppFramework\Http\DataResponse;
25+
use OCP\AppFramework\Services\IAppConfig;
2426
use OCP\Files\Folder;
27+
use OCP\Files\IRootFolder;
2528
use OCP\Files\NotFoundException;
26-
use OCP\IConfig;
2729
use OCP\IDateTimeZone;
28-
use OCP\IGroupManager;
2930
use OCP\IL10N;
3031
use OCP\IRequest;
31-
use OCP\IServerContainer;
32-
use OCP\IUserManager;
33-
use OCP\Share\IManager;
3432

3533
class FavoritesController extends Controller {
36-
37-
private \OCP\Files\Folder $userFolder;
34+
private Folder $userFolder;
3835
private readonly string $appVersion;
3936
private readonly IL10N $l;
40-
private readonly IDateTimeZone $dateTimeZone;
4137
private readonly ?string $defaultFavoritsJSON;
4238
protected $appName;
4339

4440
public function __construct(
45-
$AppName,
41+
string $appName,
4642
IRequest $request,
47-
IServerContainer $serverContainer,
48-
IConfig $config,
49-
IManager $shareManager,
50-
IAppManager $appManager,
51-
IUserManager $userManager,
52-
IGroupManager $groupManager,
43+
IAppConfig $appConfig,
5344
IL10N $l,
45+
IRootFolder $rootFolder,
5446
private readonly FavoritesService $favoritesService,
55-
IDateTimeZone $dateTimeZone,
56-
private readonly \OCA\Maps\DB\FavoriteShareMapper $favoriteShareMapper,
57-
private readonly string $userId,
47+
private readonly IDateTimeZone $dateTimeZone,
48+
private readonly FavoriteShareMapper $favoriteShareMapper,
49+
private readonly ?string $userId,
5850
) {
59-
parent::__construct($AppName, $request);
60-
$this->dateTimeZone = $dateTimeZone;
61-
$this->appName = $AppName;
62-
$this->appVersion = $config->getAppValue('maps', 'installed_version');
51+
parent::__construct($appName, $request);
52+
$this->appVersion = $appConfig->getAppValueString('installed_version');
6353
$this->l = $l;
64-
if ($this->userId !== '' and $this->userId !== null and $serverContainer !== null) {
54+
if ($this->userId !== '' and $this->userId !== null) {
6555
// path of user files folder relative to DATA folder
66-
$this->userFolder = $serverContainer->getUserFolder($this->userId);
56+
$this->userFolder = $rootFolder->getUserFolder($this->userId);
6757
}
6858
$this->defaultFavoritsJSON = json_encode([
6959
'type' => 'FeatureCollection',
@@ -74,7 +64,7 @@ public function __construct(
7464
/**
7565
* @throws \OCP\Files\NotPermittedException
7666
*/
77-
private function getJSONFavoritesFile(\OCP\Files\Folder $folder): \OCP\Files\Node {
67+
private function getJSONFavoritesFile(Folder $folder): \OCP\Files\Node {
7868
try {
7969
$file = $folder->get('.favorites.json');
8070
} catch (NotFoundException) {
@@ -84,9 +74,9 @@ private function getJSONFavoritesFile(\OCP\Files\Folder $folder): \OCP\Files\Nod
8474
}
8575

8676
/**
87-
* @NoAdminRequired
8877
* @throws \OCP\Files\NotPermittedException
8978
*/
79+
#[NoAdminRequired]
9080
public function getFavorites(?int $myMapId = null): DataResponse {
9181
if (is_null($myMapId) || $myMapId === 0) {
9282
$favorites = $this->favoritesService->getFavoritesFromDB($this->userId);
@@ -100,11 +90,11 @@ public function getFavorites(?int $myMapId = null): DataResponse {
10090
}
10191

10292
/**
103-
* @NoAdminRequired
10493
* @throws NotFoundException
10594
* @throws \OCP\Files\InvalidPathException
10695
* @throws \OCP\Files\NotPermittedException
10796
*/
97+
#[NoAdminRequired]
10898
public function addFavorite(?string $name, float $lat, float $lng, ?string $category, ?string $comment, ?string $extensions, ?int $myMapId = null): DataResponse {
10999
if (is_numeric($lng)) {
110100
if (is_null($myMapId) || $myMapId === 0) {
@@ -132,11 +122,11 @@ public function addFavorite(?string $name, float $lat, float $lng, ?string $cate
132122
}
133123

134124
/**
135-
* @NoAdminRequired
136125
* @throws NotFoundException
137126
* @throws \OCP\Files\InvalidPathException
138127
* @throws \OCP\Files\NotPermittedException
139128
*/
129+
#[NoAdminRequired]
140130
public function addFavorites(array $favorites, ?int $myMapId = null): DataResponse {
141131
if (is_null($myMapId) || $myMapId === 0) {
142132
$favoritesAfter = [];
@@ -171,9 +161,9 @@ public function addFavorites(array $favorites, ?int $myMapId = null): DataRespon
171161
}
172162

173163
/**
174-
* @NoAdminRequired
175164
* @throws \OCP\Files\NotPermittedException
176165
*/
166+
#[NoAdminRequired]
177167
public function editFavorite(int $id, ?string $name, float $lat, float $lng, ?string $category, ?string $comment, ?string $extensions, ?int $myMapId = null): DataResponse {
178168
if (is_null($myMapId) || $myMapId === 0) {
179169
$favorite = $this->favoritesService->getFavoriteFromDB($id, $this->userId);
@@ -210,10 +200,10 @@ public function editFavorite(int $id, ?string $name, float $lat, float $lng, ?st
210200
}
211201

212202
/**
213-
* @NoAdminRequired
214203
* @throws \OCP\DB\Exception
215204
* @throws \OCP\Files\NotPermittedException
216205
*/
206+
#[NoAdminRequired]
217207
public function renameCategories(array $categories, string $newName, ?int $myMapId = null): DataResponse {
218208
if (is_array($categories)) {
219209
foreach ($categories as $cat) {
@@ -239,9 +229,9 @@ public function renameCategories(array $categories, string $newName, ?int $myMap
239229
}
240230

241231
/**
242-
* @NoAdminRequired
243232
* @throws \OCP\Files\NotPermittedException
244233
*/
234+
#[NoAdminRequired]
245235
public function deleteFavorite(int $id, ?int $myMapId = null): DataResponse {
246236
if (is_null($myMapId) || $myMapId === 0) {
247237
$favorite = $this->favoritesService->getFavoriteFromDB($id, $this->userId);
@@ -261,9 +251,9 @@ public function deleteFavorite(int $id, ?int $myMapId = null): DataResponse {
261251
}
262252

263253
/**
264-
* @NoAdminRequired
265254
* @throws \OCP\Files\NotPermittedException
266255
*/
256+
#[NoAdminRequired]
267257
public function deleteFavorites(array $ids, ?int $myMapId = null): DataResponse {
268258
if (is_null($myMapId) || $myMapId === 0) {
269259
$this->favoritesService->deleteFavoritesFromDB($ids, $this->userId);
@@ -277,10 +267,10 @@ public function deleteFavorites(array $ids, ?int $myMapId = null): DataResponse
277267
}
278268

279269
/**
280-
* @NoAdminRequired
281270
* @throws \OCP\Files\NotPermittedException
282271
* @throws \OC\User\NoUserException
283272
*/
273+
#[NoAdminRequired]
284274
public function getSharedCategories(?int $myMapId = null): DataResponse {
285275
if (is_null($myMapId) || $myMapId === 0) {
286276
$categories = $this->favoriteShareMapper->findAllByOwner($this->userId);
@@ -291,9 +281,7 @@ public function getSharedCategories(?int $myMapId = null): DataResponse {
291281
return new DataResponse($categories);
292282
}
293283

294-
/**
295-
* @NoAdminRequired
296-
*/
284+
#[NoAdminRequired]
297285
public function shareCategory(string $category): DataResponse {
298286
if ($this->favoritesService->countFavorites($this->userId, [$category], null, null) === 0) {
299287
return new DataResponse($this->l->t('Unknown category'), Http::STATUS_BAD_REQUEST);
@@ -308,9 +296,7 @@ public function shareCategory(string $category): DataResponse {
308296
return new DataResponse($share);
309297
}
310298

311-
/**
312-
* @NoAdminRequired
313-
*/
299+
#[NoAdminRequired]
314300
public function unShareCategory(string $category): DataResponse {
315301
if ($this->favoritesService->countFavorites($this->userId, [$category], null, null) === 0) {
316302
return new DataResponse($this->l->t('Unknown category'), Http::STATUS_BAD_REQUEST);
@@ -324,12 +310,12 @@ public function unShareCategory(string $category): DataResponse {
324310
}
325311

326312
/**
327-
* @NoAdminRequired
328313
* @throws DoesNotExistException
329314
* @throws MultipleObjectsReturnedException
330315
* @throws \OCP\Files\NotPermittedException
331316
* @throws \OC\User\NoUserException
332317
*/
318+
#[NoAdminRequired]
333319
public function addShareCategoryToMap(string $category, int $targetMapId, ?int $myMapId = null): DataResponse {
334320
if (is_null($myMapId) || $myMapId === 0) {
335321
$share = $this->favoriteShareMapper->findByOwnerAndCategory($this->userId, $category);
@@ -358,9 +344,7 @@ public function addShareCategoryToMap(string $category, int $targetMapId, ?int $
358344
return new DataResponse('Done');
359345
}
360346

361-
/**
362-
* @NoAdminRequired
363-
*/
347+
#[NoAdminRequired]
364348
public function removeShareCategoryFromMap(string $category, int $myMapId): DataResponse {
365349
$d = $this->favoriteShareMapper->removeByMapIdAndCategory($this->userId, $myMapId, $category);
366350
if (is_null($d)) {
@@ -370,10 +354,10 @@ public function removeShareCategoryFromMap(string $category, int $myMapId): Data
370354
}
371355

372356
/**
373-
* @NoAdminRequired
374357
* @throws NotFoundException
375358
* @throws \OCP\Files\NotPermittedException
376359
*/
360+
#[NoAdminRequired]
377361
public function exportFavorites(?array $categoryList = null, ?int $begin = null, ?int $end = null, bool $all = false): DataResponse {
378362
// sorry about ugly categoryList management:
379363
// when an empty list is passed in http request, we get null here
@@ -423,10 +407,10 @@ public function exportFavorites(?array $categoryList = null, ?int $begin = null,
423407
}
424408

425409
/**
426-
* @NoAdminRequired
427410
* @throws NotFoundException
428411
* @throws \OCP\Files\InvalidPathException
429412
*/
413+
#[NoAdminRequired]
430414
public function importFavorites(string $path): DataResponse {
431415
$userFolder = $this->userFolder;
432416
$cleanpath = str_replace(['../', '..\\'], '', $path);
@@ -436,7 +420,9 @@ public function importFavorites(string $path): DataResponse {
436420
if ($file->getType() === \OCP\Files\FileInfo::TYPE_FILE
437421
and $file->isReadable()) {
438422
$lowerFileName = strtolower((string)$file->getName());
439-
if ($this->endswith($lowerFileName, '.gpx') or $this->endswith($lowerFileName, '.kml') or $this->endswith($lowerFileName, '.kmz') or $this->endswith($lowerFileName, '.json') or $this->endswith($lowerFileName, '.geojson')) {
423+
if (str_ends_with($lowerFileName, '.gpx') or str_ends_with($lowerFileName, '.kml')
424+
|| str_ends_with($lowerFileName, '.kmz') or str_ends_with($lowerFileName, '.json')
425+
|| str_ends_with($lowerFileName, '.geojson')) {
440426
$result = $this->favoritesService->importFavorites($this->userId, $file);
441427
return new DataResponse($result);
442428
} else {
@@ -452,14 +438,4 @@ public function importFavorites(string $path): DataResponse {
452438
return new DataResponse($this->l->t('File does not exist'), 400);
453439
}
454440
}
455-
456-
private function endswith(string $string, string $test): bool {
457-
$strlen = strlen($string);
458-
$testlen = strlen($test);
459-
if ($testlen > $strlen) {
460-
return false;
461-
}
462-
return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
463-
}
464-
465441
}

0 commit comments

Comments
 (0)