diff --git a/lib/Controller/AlbumsController.php b/lib/Controller/AlbumsController.php index 0a8b5c2b..6d0855b7 100644 --- a/lib/Controller/AlbumsController.php +++ b/lib/Controller/AlbumsController.php @@ -75,7 +75,7 @@ class AlbumsController extends GenericClusterController return array_values($list); } - protected function getFileIds(string $name, ?int $limit = null): array + protected function getFiles(string $name, ?int $limit = null): array { // Get album $album = $this->timelineQuery->getAlbumIfAllowed($this->getUID(), $name); @@ -84,8 +84,7 @@ class AlbumsController extends GenericClusterController } // Get files - $list = $this->timelineQuery->getAlbumFiles((int) $album['album_id'], $limit) ?? []; - - return array_map(fn ($item) => (int) $item['fileid'], $list); + $id = (int) $album['album_id']; + return $this->timelineQuery->getAlbumFiles($id, $limit) ?? []; } } diff --git a/lib/Controller/GenericClusterController.php b/lib/Controller/GenericClusterController.php index 78976908..a0644310 100644 --- a/lib/Controller/GenericClusterController.php +++ b/lib/Controller/GenericClusterController.php @@ -69,18 +69,18 @@ abstract class GenericClusterController extends GenericApiController $this->init(); // Get list of some files in this cluster - $fileIds = $this->getFileIds($name, 8); + $files = $this->getFiles($name, 8); // If no files found then return 404 - if (0 === \count($fileIds)) { + if (0 === \count($files)) { return new JSONResponse([], Http::STATUS_NOT_FOUND); } - // Shuffle the list so we don't always get the same preview - shuffle($fileIds); + // Put the files in the correct order + $this->sortFilesForPreview($files); // Get preview from image list - return $this->getPreviewFromImageList($fileIds); + return $this->getPreviewFromImageList($this->getFileIds($files)); } catch (HttpResponseException $e) { return $e->response; } catch (\Exception $e) { @@ -101,7 +101,7 @@ abstract class GenericClusterController extends GenericApiController $this->init(); // Get list of all files in this cluster - $fileIds = $this->getFileIds($name); + $fileIds = $this->getFileIds($this->getFiles($name)); // Get download handle $filename = $this->clusterName($name); @@ -132,14 +132,13 @@ abstract class GenericClusterController extends GenericApiController abstract protected function getClusters(): array; /** - * Get a list of fileids for the given cluster for preview generation. + * Get a list of files with extra parameters for the given cluster + * Used for preview generation and download. * * @param string $name Identifier for the cluster * @param int $limit Maximum number of fileids to return - * - * @return int[] */ - abstract protected function getFileIds(string $name, ?int $limit = null): array; + abstract protected function getFiles(string $name, ?int $limit = null): array; /** * Initialize and check if the app is enabled. @@ -180,4 +179,29 @@ abstract class GenericClusterController extends GenericApiController { return $name; } + + /** + * Put the file objects in priority list. + * Works on the array in place. + */ + protected function sortFilesForPreview(array &$files) + { + shuffle($files); + } + + /** + * Get the file ID for a file object. + */ + protected function getFileId(array $file): int + { + return (int) $file['fileid']; + } + + /** + * Get array of fileIds from array of file objects. + */ + private function getFileIds(array $files): array + { + return array_map([$this, 'getFileId'], $files); + } } diff --git a/lib/Controller/PlacesController.php b/lib/Controller/PlacesController.php index 6856649f..f48393a6 100644 --- a/lib/Controller/PlacesController.php +++ b/lib/Controller/PlacesController.php @@ -40,10 +40,8 @@ class PlacesController extends GenericClusterController return $this->timelineQuery->getPlaces($this->root); } - protected function getFileIds(string $name, ?int $limit = null): array + protected function getFiles(string $name, ?int $limit = null): array { - $list = $this->timelineQuery->getPlaceFiles((int) $name, $this->root, $limit) ?? []; - - return array_map(fn ($item) => (int) $item['fileid'], $list); + return $this->timelineQuery->getPlaceFiles((int) $name, $this->root, $limit) ?? []; } } diff --git a/lib/Controller/TagsController.php b/lib/Controller/TagsController.php index 1a8e9aac..e553bebf 100644 --- a/lib/Controller/TagsController.php +++ b/lib/Controller/TagsController.php @@ -77,10 +77,8 @@ class TagsController extends GenericClusterController return $this->timelineQuery->getTags($this->root); } - protected function getFileIds(string $name, ?int $limit = null): array + protected function getFiles(string $name, ?int $limit = null): array { - $list = $this->timelineQuery->getTagFiles($name, $this->root, $limit) ?? []; - - return array_map(fn ($item) => (int) $item['fileid'], $list); + return $this->timelineQuery->getTagFiles($name, $this->root, $limit) ?? []; } }