Skip to content

Commit

Permalink
Do not get all the files list when fetching the albums list
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Sep 23, 2022
1 parent 090396c commit 482b3b1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
27 changes: 10 additions & 17 deletions lib/Album/AlbumMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,40 +177,33 @@ public function delete(int $id): void {
}

/**
* @param int $albumId
* @param string $userId
* @return AlbumWithFiles[]
* @return AlbumFile[]
*/
public function getForUserWithFiles(string $userId): array {
public function getForAlbumIdAndUserWithFiles(int $albumId, string $userId): array {
$query = $this->connection->getQueryBuilder();
$query->select("fileid", "mimetype", "a.album_id", "size", "mtime", "etag", "location", "created", "last_added_photo", "added", "owner")
$query->select("fileid", "mimetype", "a.album_id", "size", "mtime", "etag", "added", "owner")
->selectAlias("f.name", "file_name")
->selectAlias("a.name", "album_name")
->from("photos_albums", "a")
->leftJoin("a", "photos_albums_files", "p", $query->expr()->eq("a.album_id", "p.album_id"))
->leftJoin("p", "filecache", "f", $query->expr()->eq("p.file_id", "f.fileid"))
->where($query->expr()->eq('user', $query->createNamedParameter($userId)));
->where($query->expr()->eq('a.album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('user', $query->createNamedParameter($userId)));
$rows = $query->executeQuery()->fetchAll();

$filesByAlbum = [];
$albumsById = [];
$files = [];
foreach ($rows as $row) {
$albumId = (int)$row['album_id'];
if ($row['fileid']) {
$mimeId = $row['mimetype'];
$mimeType = $this->mimeTypeLoader->getMimetypeById($mimeId);
$filesByAlbum[$albumId][] = new AlbumFile((int)$row['fileid'], $row['file_name'], $mimeType, (int)$row['size'], (int)$row['mtime'], $row['etag'], (int)$row['added'], $row['owner']);
}

if (!isset($albumsById[$albumId])) {
$albumsById[$albumId] = new AlbumInfo($albumId, $userId, $row['album_name'], $row['location'], (int)$row['created'], (int)$row['last_added_photo']);
$files[] = new AlbumFile((int)$row['fileid'], $row['file_name'], $mimeType, (int)$row['size'], (int)$row['mtime'], $row['etag'], (int)$row['added'], $row['owner']);
}
}

$result = [];
foreach ($albumsById as $id => $album) {
$result[] = new AlbumWithFiles($album, $filesByAlbum[$id] ?? []);
}
return $result;
return $files ?? [];
}

/**
Expand Down Expand Up @@ -414,7 +407,7 @@ public function getSharedAlbumsForCollaboratorWithFiles(string $collaboratorId,

$result = [];
foreach ($albumsById as $id => $album) {
$result[] = new AlbumWithFiles($album, $filesByAlbum[$id] ?? []);
$result[] = new AlbumWithFiles($album, $this, $filesByAlbum[$id] ?? []);
}
return $result;
}
Expand Down
24 changes: 22 additions & 2 deletions lib/Album/AlbumWithFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@

class AlbumWithFiles {
private AlbumInfo $info;
private AlbumMapper $albumMapper;

/** @var AlbumFile[] */
private array $files;

public function __construct(AlbumInfo $info, array $files) {
public function __construct(
AlbumInfo $info,
AlbumMapper $albumMapper,
array $files = []) {
$this->info = $info;
$this->albumMapper = $albumMapper;
$this->files = $files;
}

Expand All @@ -41,13 +47,20 @@ public function getAlbum(): AlbumInfo {
* @return AlbumFile[]
*/
public function getFiles(): array {
if (empty($this->files)) {
$this->files = $this->fetchFiles();
}
return $this->files;
}

/**
* @return AlbumFile[]
*/
public function addFile(AlbumFile $file): array {
if (empty($this->files)) {
$this->files = $this->fetchFiles();
}

array_push($this->files, $file);
return $this->files;
}
Expand All @@ -58,6 +71,13 @@ public function addFile(AlbumFile $file): array {
public function getFileIds(): array {
return array_map(function (AlbumFile $file) {
return $file->getFileId();
}, $this->files);
}, $this->getFiles());
}

/**
* @return AlbumFile[]
*/
private function fetchFiles(): array {
return $this->albumMapper->getForAlbumIdAndUserWithFiles($this->info->getId(), $this->info->getUserId()) ?? [];
}
}
9 changes: 5 additions & 4 deletions lib/Sabre/Album/AlbumsHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\Photos\Sabre\Album;

use OCA\Photos\Album\AlbumInfo;
use OCA\Photos\Album\AlbumMapper;
use OCA\Photos\Album\AlbumWithFiles;
use OCA\Photos\Service\UserConfigService;
Expand Down Expand Up @@ -106,10 +107,10 @@ public function getChild($name) {
*/
public function getChildren(): array {
if ($this->children === null) {
$folders = $this->albumMapper->getForUserWithFiles($this->user->getUID());
$this->children = array_map(function (AlbumWithFiles $folder) {
return new AlbumRoot($this->albumMapper, $folder, $this->rootFolder, $this->userFolder, $this->user, $this->userConfigService);
}, $folders);
$albumInfos = $this->albumMapper->getForUser($this->user->getUID());
$this->children = array_map(function (AlbumInfo $albumInfo) {
return new AlbumRoot($this->albumMapper, new AlbumWithFiles($albumInfo, $this->albumMapper), $this->rootFolder, $this->userFolder, $this->user, $this->userConfigService);
}, $albumInfos);
}

return $this->children;
Expand Down

0 comments on commit 482b3b1

Please sign in to comment.