refactor: make cluster more generic

Signed-off-by: Varun Patil <varunpatil@ucla.edu>
pull/563/head
Varun Patil 2023-03-22 16:19:59 -07:00
parent fffc597797
commit af7000a037
4 changed files with 41 additions and 22 deletions

View File

@ -75,7 +75,7 @@ class AlbumsController extends GenericClusterController
return array_values($list); return array_values($list);
} }
protected function getFileIds(string $name, ?int $limit = null): array protected function getFiles(string $name, ?int $limit = null): array
{ {
// Get album // Get album
$album = $this->timelineQuery->getAlbumIfAllowed($this->getUID(), $name); $album = $this->timelineQuery->getAlbumIfAllowed($this->getUID(), $name);
@ -84,8 +84,7 @@ class AlbumsController extends GenericClusterController
} }
// Get files // Get files
$list = $this->timelineQuery->getAlbumFiles((int) $album['album_id'], $limit) ?? []; $id = (int) $album['album_id'];
return $this->timelineQuery->getAlbumFiles($id, $limit) ?? [];
return array_map(fn ($item) => (int) $item['fileid'], $list);
} }
} }

View File

@ -69,18 +69,18 @@ abstract class GenericClusterController extends GenericApiController
$this->init(); $this->init();
// Get list of some files in this cluster // 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 no files found then return 404
if (0 === \count($fileIds)) { if (0 === \count($files)) {
return new JSONResponse([], Http::STATUS_NOT_FOUND); return new JSONResponse([], Http::STATUS_NOT_FOUND);
} }
// Shuffle the list so we don't always get the same preview // Put the files in the correct order
shuffle($fileIds); $this->sortFilesForPreview($files);
// Get preview from image list // Get preview from image list
return $this->getPreviewFromImageList($fileIds); return $this->getPreviewFromImageList($this->getFileIds($files));
} catch (HttpResponseException $e) { } catch (HttpResponseException $e) {
return $e->response; return $e->response;
} catch (\Exception $e) { } catch (\Exception $e) {
@ -101,7 +101,7 @@ abstract class GenericClusterController extends GenericApiController
$this->init(); $this->init();
// Get list of all files in this cluster // Get list of all files in this cluster
$fileIds = $this->getFileIds($name); $fileIds = $this->getFileIds($this->getFiles($name));
// Get download handle // Get download handle
$filename = $this->clusterName($name); $filename = $this->clusterName($name);
@ -132,14 +132,13 @@ abstract class GenericClusterController extends GenericApiController
abstract protected function getClusters(): array; 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 string $name Identifier for the cluster
* @param int $limit Maximum number of fileids to return * @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. * Initialize and check if the app is enabled.
@ -180,4 +179,29 @@ abstract class GenericClusterController extends GenericApiController
{ {
return $name; 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);
}
} }

View File

@ -40,10 +40,8 @@ class PlacesController extends GenericClusterController
return $this->timelineQuery->getPlaces($this->root); 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 $this->timelineQuery->getPlaceFiles((int) $name, $this->root, $limit) ?? [];
return array_map(fn ($item) => (int) $item['fileid'], $list);
} }
} }

View File

@ -77,10 +77,8 @@ class TagsController extends GenericClusterController
return $this->timelineQuery->getTags($this->root); 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 $this->timelineQuery->getTagFiles($name, $this->root, $limit) ?? [];
return array_map(fn ($item) => (int) $item['fileid'], $list);
} }
} }