Skip to content

Commit b797112

Browse files
committed
fix: Fix typing in photo related classes
Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent ed685f7 commit b797112

6 files changed

Lines changed: 94 additions & 58 deletions

File tree

lib/Controller/PhotosController.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,17 @@ public function getNonLocalizedPhotos(?int $myMapId = null, ?string $timezone =
8787

8888

8989
/**
90-
* @param $paths
91-
* @param $lats
92-
* @param $lngs
90+
* @param list<string> $paths
91+
* @param list<float> $lats
92+
* @param list<float> $lngs
93+
* @return DataResponse<Http::STATUS_*, list<array{path: (array | string | null), lat: float, lng: float, oldLat: ?float, oldLng: ?float}>|array<never, never>, array<string, mixed>>
9394
* @throws NoUserException
9495
* @throws NotFoundException
9596
* @throws NotPermittedException
9697
* @throws InvalidPathException
9798
*/
9899
#[NoAdminRequired]
99-
public function placePhotos($paths, $lats, $lngs, bool $directory = false, ?int $myMapId = null, bool $relative = false): DataResponse {
100+
public function placePhotos(array $paths, array $lats, array $lngs, bool $directory = false, ?int $myMapId = null, bool $relative = false): DataResponse {
100101
$userFolder = $this->root->getUserFolder($this->userId);
101102
if (!is_null($myMapId)) {
102103
if ($directory) {
@@ -105,15 +106,19 @@ public function placePhotos($paths, $lats, $lngs, bool $directory = false, ?int
105106
}
106107

107108
$folder = $userFolder->getFirstNodeById($myMapId);
108-
if (!($folder instanceof Folder)) {
109+
if (!$folder instanceof Folder) {
109110
return new DataResponse(statusCode: Http::STATUS_BAD_REQUEST);
110111
}
111112

112113
// photo's path is relative to this map's folder => get full path, don't copy
113114
if ($relative) {
114115
foreach ($paths as $key => $path) {
115116
$photoFile = $folder->get($path);
116-
$paths[$key] = $userFolder->getRelativePath($photoFile->getPath());
117+
$path = $userFolder->getRelativePath($photoFile->getPath());
118+
if ($path === null) {
119+
return new DataResponse(statusCode: Http::STATUS_BAD_REQUEST);
120+
}
121+
$paths[$key] = $path;
117122
}
118123
} else {
119124
// here the photo path is good, copy it in this map's folder if it's not already there
@@ -122,7 +127,11 @@ public function placePhotos($paths, $lats, $lngs, bool $directory = false, ?int
122127
// is the photo in this map's folder?
123128
if ($folder->getFirstNodeById($photoFile->getId()) !== null) {
124129
$copiedFile = $photoFile->copy($folder->getPath() . '/' . $photoFile->getName());
125-
$paths[$key] = $userFolder->getRelativePath($copiedFile->getPath());
130+
$path = $userFolder->getRelativePath($copiedFile->getPath());
131+
if ($path === null) {
132+
return new DataResponse(statusCode: Http::STATUS_BAD_REQUEST);
133+
}
134+
$paths[$key] = $path;
126135
}
127136
}
128137
}

lib/DB/Geophoto.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515

1616
use OCP\AppFramework\Db\Entity;
1717

18+
/**
19+
* @method int getFileId()
20+
* @method float getLat()
21+
* @method float getLng()
22+
* @method string getUserId()
23+
*/
1824
class Geophoto extends Entity {
1925

2026
protected $fileId;

lib/DB/GeophotoMapper.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,11 @@ public function find($id) {
4444
}
4545

4646
/**
47-
* @param $fileId
48-
* @param $userId
49-
* @return mixed|\OCP\AppFramework\Db\Entity
5047
* @throws \OCP\AppFramework\Db\DoesNotExistException
5148
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
5249
* @throws \OCP\DB\Exception
5350
*/
54-
public function findByFileIdUserId($fileId, $userId) {
51+
public function findByFileIdUserId(int $fileId, string $userId): Geophoto {
5552
$qb = $this->db->getQueryBuilder();
5653

5754
$qb->select('*')
@@ -66,13 +63,11 @@ public function findByFileIdUserId($fileId, $userId) {
6663
}
6764

6865
/**
69-
* @param $fileId
70-
* @return mixed|\OCP\AppFramework\Db\Entity
7166
* @throws \OCP\AppFramework\Db\DoesNotExistException
7267
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
7368
* @throws \OCP\DB\Exception
7469
*/
75-
public function findByFileId($fileId) {
70+
public function findByFileId(int $fileId): Geophoto {
7671
$qb = $this->db->getQueryBuilder();
7772

7873
$qb->select('*')

lib/Service/FavoritesService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ public function deleteFavoritesFromJSON($file, $ids): void {
581581
$file->putContent(json_encode($data, JSON_PRETTY_PRINT));
582582
}
583583

584-
public function exportFavorites($userId, $fileHandler, $categoryList, $begin, $end, string $appVersion): void {
584+
public function exportFavorites(?string $userId, $fileHandler, $categoryList, $begin, $end, string $appVersion): void {
585585
$qb = $this->dbconnection->getQueryBuilder();
586586
$nbFavorites = $this->countFavorites($userId, $categoryList, $begin, $end);
587587

@@ -632,7 +632,7 @@ public function exportFavorites($userId, $fileHandler, $categoryList, $begin, $e
632632
$req = $qb->executeQuery();
633633

634634
while ($row = $req->fetch()) {
635-
$name = str_replace('&', '&amp;', $row['name']);
635+
$name = str_replace('&', '&amp;', $row['name'] ?? '');
636636
$epoch = $row['date_created'];
637637
$date = '';
638638
if (is_numeric($epoch)) {

lib/Service/PhotofilesService.php

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OCP\Files\Folder;
2727
use OCP\Files\IRootFolder;
2828
use OCP\Files\Node;
29+
use OCP\Files\NotFoundException;
2930
use OCP\Files\StorageNotAvailableException;
3031
use OCP\ICache;
3132
use OCP\ICacheFactory;
@@ -248,39 +249,58 @@ public function getBackgroundJobStatus(string $userId): array {
248249
];
249250
}
250251

251-
public function setPhotosFilesCoords(string $userId, $paths, $lats, $lngs, $directory): array {
252+
/**
253+
* @param list<string> $paths
254+
* @param list<float> $lats
255+
* @param list<float> $lngs
256+
* @return list<array{path: (array | string | null), lat: float, lng: float, oldLat: ?float, oldLng: ?float}>
257+
*/
258+
public function setPhotosFilesCoords(string $userId, $paths, $lats, $lngs, bool $directory): array {
252259
if ($directory) {
253260
return $this->setDirectoriesCoords($userId, $paths, $lats, $lngs);
254261
}
255262

256263
return $this->setFilesCoords($userId, $paths, $lats, $lngs);
257264
}
258265

266+
/**
267+
* @param list<string> $paths
268+
* @param list<float> $lats
269+
* @param list<float> $lngs
270+
* @return list<array{path: (array | string | null), lat: float, lng: float, oldLat: ?float, oldLng: ?float}>
271+
*/
259272
private function setDirectoriesCoords(string $userId, $paths, $lats, $lngs): array {
260273
$lat = $lats[0] ?? 0;
261274
$lng = $lngs[0] ?? 0;
262275
$userFolder = $this->root->getUserFolder($userId);
263276
$done = [];
264277
foreach ($paths as $dirPath) {
265278
$cleanDirPath = str_replace(['../', '..\\'], '', $dirPath);
266-
if ($userFolder->nodeExists($cleanDirPath)) {
279+
try {
267280
$dir = $userFolder->get($cleanDirPath);
268-
if ($dir instanceof Folder) {
269-
$nodes = $dir->getDirectoryListing();
270-
foreach ($nodes as $node) {
271-
if ($this->isPhoto($node) && $node->isUpdateable()) {
272-
$photo = $this->photoMapper->findByFileIdUserId($node->getId(), $userId);
273-
$done[] = [
274-
'path' => preg_replace('/^files/', '', (string)$node->getInternalPath()),
275-
'lat' => $lat,
276-
'lng' => $lng,
277-
'oldLat' => $photo ? $photo->getLat() : null,
278-
'oldLng' => $photo ? $photo->getLng() : null,
279-
];
280-
$this->setExifCoords($node, $lat, $lng);
281-
$this->updateByFileNow($node);
282-
}
281+
} catch (NotFoundException) {
282+
continue;
283+
}
284+
if (!$dir instanceof Folder) {
285+
continue;
286+
}
287+
$nodes = $dir->getDirectoryListing();
288+
foreach ($nodes as $node) {
289+
if ($this->isPhoto($node) && $node->isUpdateable()) {
290+
try {
291+
$photo = $this->photoMapper->findByFileIdUserId($node->getId(), $userId);
292+
} catch (DoesNotExistException) {
293+
$photo = null;
283294
}
295+
$done[] = [
296+
'path' => preg_replace('/^files/', '', (string)$node->getInternalPath()),
297+
'lat' => $lat,
298+
'lng' => $lng,
299+
'oldLat' => $photo?->getLat(),
300+
'oldLng' => $photo?->getLng(),
301+
];
302+
$this->setExifCoords($node, $lat, $lng);
303+
$this->updateByFileNow($node);
284304
}
285305
}
286306
}
@@ -289,43 +309,49 @@ private function setDirectoriesCoords(string $userId, $paths, $lats, $lngs): arr
289309
}
290310

291311
/**
292-
* @return array{path: (array | string | null), lat: mixed, lng: mixed, oldLat: mixed, oldLng: mixed}[]
312+
* @param list<string> $paths
313+
* @param list<float> $lats
314+
* @param list<float> $lngs
315+
* @return list<array{path: (array | string | null), lat: float, lng: float, oldLat: ?float, oldLng: ?float}>
293316
*/
294-
private function setFilesCoords(string $userId, $paths, array $lats, array $lngs): array {
317+
private function setFilesCoords(string $userId, array $paths, array $lats, array $lngs): array {
295318
$userFolder = $this->root->getUserFolder($userId);
296319
$done = [];
297320

298321
foreach ($paths as $i => $path) {
299-
$cleanpath = str_replace(['../', '..\\'], '', $path);
300-
if ($userFolder->nodeExists($cleanpath)) {
301-
$file = $userFolder->get($cleanpath);
302-
if ($file instanceof File && $this->isPhoto($file) && $file->isUpdateable()) {
303-
$lat = (count($lats) > $i) ? $lats[$i] : $lats[0];
304-
$lng = (count($lngs) > $i) ? $lngs[$i] : $lngs[0];
305-
try {
306-
$photo = $this->photoMapper->findByFileIdUserId($file->getId(), $userId);
307-
} catch (DoesNotExistException) {
308-
$photo = null;
309-
}
322+
$cleanPath = str_replace(['../', '..\\'], '', $path);
323+
try {
324+
$file = $userFolder->get($cleanPath);
325+
} catch (NotFoundException) {
326+
continue;
327+
}
310328

311-
$done[] = [
312-
'path' => preg_replace('/^files/', '', $file->getInternalPath()),
313-
'lat' => $lat,
314-
'lng' => $lng,
315-
'oldLat' => $photo ? $photo->getLat() : null,
316-
'oldLng' => $photo ? $photo->getLng() : null,
317-
];
318-
$this->setExifCoords($file, $lat, $lng);
319-
$this->updateByFileNow($file);
329+
if ($file instanceof File && $this->isPhoto($file) && $file->isUpdateable()) {
330+
$lat = (count($lats) > $i) ? $lats[$i] : $lats[0];
331+
$lng = (count($lngs) > $i) ? $lngs[$i] : $lngs[0];
332+
try {
333+
$photo = $this->photoMapper->findByFileIdUserId($file->getId(), $userId);
334+
} catch (DoesNotExistException) {
335+
$photo = null;
320336
}
337+
338+
$done[] = [
339+
'path' => preg_replace('/^files/', '', $file->getInternalPath()),
340+
'lat' => $lat,
341+
'lng' => $lng,
342+
'oldLat' => $photo?->getLat(),
343+
'oldLng' => $photo?->getLng(),
344+
];
345+
$this->setExifCoords($file, $lat, $lng);
346+
$this->updateByFileNow($file);
321347
}
322348
}
323349

324350
return $done;
325351
}
326352

327353
/**
328-
* @return array{path: (array | string | null), lat: null, lng: null, oldLat: mixed, oldLng: mixed}[]
354+
* @return list<array{path: (array | string | null), lat: null, lng: null, oldLat: ?float, oldLng: ?float}>
329355
*/
330356
public function resetPhotosFilesCoords($userId, $paths): array {
331357
$userFolder = $this->root->getUserFolder($userId);

lib/Service/TracksService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ public function generateTrackMetadata($file): ?string {
949949
}
950950

951951
/**
952-
* @return mixed[]
952+
* @return object[]
953953
*/
954954
private function getDistanceFilteredPoints($points): array {
955955
$DISTANCE_THRESHOLD = 10;
@@ -959,7 +959,7 @@ private function getDistanceFilteredPoints($points): array {
959959
$distFilteredPoints[] = $points[0];
960960
$lastPoint = $points[0];
961961
foreach ($points as $point) {
962-
if (distance($lastPoint, $point) >= $DISTANCE_THRESHOLD) {
962+
if (distance((array)$lastPoint, (array)$point) >= $DISTANCE_THRESHOLD) {
963963
$distFilteredPoints[] = $point;
964964
$lastPoint = $point;
965965
}

0 commit comments

Comments
 (0)