From 8a7aaea3239ef581a3705b62038747d5a842ac58 Mon Sep 17 00:00:00 2001 From: Christian McHugh Date: Mon, 4 Dec 2023 17:11:04 +0000 Subject: [PATCH 1/9] Add OCC comand add-photo-to-album Signed-off-by: Christian McHugh --- appinfo/info.xml | 1 + lib/Command/AlbumAddCommand.php | 113 ++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 lib/Command/AlbumAddCommand.php diff --git a/appinfo/info.xml b/appinfo/info.xml index e7cbf3b82..2c4bc37cb 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -32,6 +32,7 @@ OCA\Photos\Command\UpdateReverseGeocodingFilesCommand + OCA\Photos\Command\AlbumAddCommand diff --git a/lib/Command/AlbumAddCommand.php b/lib/Command/AlbumAddCommand.php new file mode 100644 index 000000000..f11d2147a --- /dev/null +++ b/lib/Command/AlbumAddCommand.php @@ -0,0 +1,113 @@ + + * + * @author Christian McHugh + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\Photos\Command; + +use OCP\Files\IRootFolder; +use OCP\IUserManager; +use OCA\Photos\Album\AlbumMapper; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + + +class AlbumAddCommand extends Command { + private IRootFolder $rootFolder; + private IUserManager $userManager; + private AlbumMapper $albumMapper; + + public function __construct( + AlbumMapper $albumMapper, + IRootFolder $rootFolder, + IUserManager $userManager, + ) { + $this->rootFolder = $rootFolder; + $this->userManager = $userManager; + $this->albumMapper = $albumMapper; + parent::__construct(); + } + + /** + * Configure the command + */ + protected function configure(): void { + $this->setName('photos:add-photo-to-album') + ->setDescription('Add specified photo to album') + ->addArgument('user', InputArgument::REQUIRED, 'user owning album') + ->addArgument('album', InputArgument::REQUIRED, 'album name') + ->addArgument('file', InputArgument::REQUIRED, + 'Path to the file to add to the album. It must already be scanned and available in NextCloud. Example: Photos/picture1.jpg'); + } + + /** + * Execute the command + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + $userString = $input->getArgument('user'); + $albumString = $input->getArgument('album'); + $filePath = $input->getArgument('file'); + + $user = $this->userManager->get($userString); + if ($user === null) { + throw new \Exception("User $userString was not found"); + } + $userID = $user->getUID(); + + try { + $pictureFileID = $this->rootFolder->getUserFolder($userID)->get($filePath)->getId(); + } catch (\Exception $ex) { + $output->writeln('Invalid file path'); + $output->writeln($ex->getMessage()); + return 1; + } + + $retrievedAlbums = $this->albumMapper->getForUser($userString); + + $album = null; + foreach ($retrievedAlbums as $singleAlbum) { + if ($singleAlbum->getTitle() == $albumString) { + $album = $singleAlbum; + break; + } + } + + if (!$album) { + throw new \Exception("Album $albumString was not found"); + } + + try { + $this->albumMapper->addFile($album->getId(), $pictureFileID, $userID); + + } catch (\Exception $ex) { + $output->writeln("Problem adding $filePath to $albumString"); + $output->writeln($ex->getMessage()); + return 1; + } + + return 0; + } +} From e8ef520ba902993c510f3c270e0d24f97d5e6a75 Mon Sep 17 00:00:00 2001 From: Christian McHugh Date: Mon, 4 Dec 2023 17:18:23 +0000 Subject: [PATCH 2/9] uppercase Signed-off-by: Christian McHugh --- lib/Command/AlbumAddCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Command/AlbumAddCommand.php b/lib/Command/AlbumAddCommand.php index f11d2147a..646f2f08a 100644 --- a/lib/Command/AlbumAddCommand.php +++ b/lib/Command/AlbumAddCommand.php @@ -57,8 +57,8 @@ public function __construct( protected function configure(): void { $this->setName('photos:add-photo-to-album') ->setDescription('Add specified photo to album') - ->addArgument('user', InputArgument::REQUIRED, 'user owning album') - ->addArgument('album', InputArgument::REQUIRED, 'album name') + ->addArgument('user', InputArgument::REQUIRED, 'User owning album') + ->addArgument('album', InputArgument::REQUIRED, 'Album name') ->addArgument('file', InputArgument::REQUIRED, 'Path to the file to add to the album. It must already be scanned and available in NextCloud. Example: Photos/picture1.jpg'); } From c563f9e55c922dbdb5067e9a7b800d2b3f36062f Mon Sep 17 00:00:00 2001 From: Christian McHugh Date: Tue, 5 Dec 2023 07:40:00 +0000 Subject: [PATCH 3/9] Add album creation to occ Signed-off-by: Christian McHugh --- appinfo/info.xml | 1 + lib/Command/AlbumCreateCommand.php | 99 ++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 lib/Command/AlbumCreateCommand.php diff --git a/appinfo/info.xml b/appinfo/info.xml index 2c4bc37cb..4fc8698ed 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -33,6 +33,7 @@ OCA\Photos\Command\UpdateReverseGeocodingFilesCommand OCA\Photos\Command\AlbumAddCommand + OCA\Photos\Command\AlbumCreateCommand diff --git a/lib/Command/AlbumCreateCommand.php b/lib/Command/AlbumCreateCommand.php new file mode 100644 index 000000000..b4904fdda --- /dev/null +++ b/lib/Command/AlbumCreateCommand.php @@ -0,0 +1,99 @@ + + * + * @author Christian McHugh + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\Photos\Command; + +use OCP\IUserManager; +use OCA\Photos\Album\AlbumMapper; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + + +class AlbumCreateCommand extends Command { + private IUserManager $userManager; + private AlbumMapper $albumMapper; + + public function __construct( + AlbumMapper $albumMapper, + IUserManager $userManager, + ) { + $this->userManager = $userManager; + $this->albumMapper = $albumMapper; + parent::__construct(); + } + + /** + * Configure the command + */ + protected function configure(): void { + $this->setName('photos:create-album') + ->setDescription('Add file to album') + ->addArgument('user', InputArgument::REQUIRED, 'User to own album') + ->addArgument('album', InputArgument::REQUIRED, 'Album name') + ->addOption('location', 'l', InputOption::VALUE_REQUIRED, 'Set album location (optional)', ""); + } + + /** + * Execute the command + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + $userString = $input->getArgument('user'); + $albumString = $input->getArgument('album'); + $location = $input->getOption('location'); + + $user = $this->userManager->get($userString); + if ($user === null) { + throw new \Exception("User $userString was not found"); + } + $userID = $user->getUID(); + + $retrievedAlbums = $this->albumMapper->getForUser($userString); + + $album = null; + foreach ($retrievedAlbums as $singleAlbum) { + if ($singleAlbum->getTitle() == $albumString) { + $album = $singleAlbum; + break; + } + } + + if ($album) { + throw new \Exception("Album $albumString already exists and cannot be created."); + } + + try { + $this->albumMapper->create($userID, $albumString, $location); + } catch (\Exception $ex) { + $output->writeln('Problem creating album'); + $output->writeln($ex->getMessage()); + return 1; + } + + return 0; + } +} \ No newline at end of file From 0aef574b01ef4b595a398868cccac53bf69e10dc Mon Sep 17 00:00:00 2001 From: Christian McHugh Date: Tue, 5 Dec 2023 21:06:01 +0000 Subject: [PATCH 4/9] remove empty space Signed-off-by: Christian McHugh --- lib/Command/AlbumAddCommand.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/Command/AlbumAddCommand.php b/lib/Command/AlbumAddCommand.php index 646f2f08a..5efcfc085 100644 --- a/lib/Command/AlbumAddCommand.php +++ b/lib/Command/AlbumAddCommand.php @@ -101,7 +101,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int try { $this->albumMapper->addFile($album->getId(), $pictureFileID, $userID); - } catch (\Exception $ex) { $output->writeln("Problem adding $filePath to $albumString"); $output->writeln($ex->getMessage()); From a218a5f9b2c5648f3691165b356e16e9ea157709 Mon Sep 17 00:00:00 2001 From: Christian McHugh Date: Tue, 5 Dec 2023 21:05:26 +0000 Subject: [PATCH 5/9] Apply suggestions from code review Co-authored-by: Louis Signed-off-by: Christian McHugh --- lib/Command/AlbumAddCommand.php | 2 +- lib/Command/AlbumCreateCommand.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Command/AlbumAddCommand.php b/lib/Command/AlbumAddCommand.php index 5efcfc085..888cfcf66 100644 --- a/lib/Command/AlbumAddCommand.php +++ b/lib/Command/AlbumAddCommand.php @@ -55,7 +55,7 @@ public function __construct( * Configure the command */ protected function configure(): void { - $this->setName('photos:add-photo-to-album') + $this->setName('photos:albums:add') ->setDescription('Add specified photo to album') ->addArgument('user', InputArgument::REQUIRED, 'User owning album') ->addArgument('album', InputArgument::REQUIRED, 'Album name') diff --git a/lib/Command/AlbumCreateCommand.php b/lib/Command/AlbumCreateCommand.php index b4904fdda..b91bea7a9 100644 --- a/lib/Command/AlbumCreateCommand.php +++ b/lib/Command/AlbumCreateCommand.php @@ -51,8 +51,8 @@ public function __construct( * Configure the command */ protected function configure(): void { - $this->setName('photos:create-album') - ->setDescription('Add file to album') + $this->setName('photos:albums:create') + ->setDescription('Create a new album for a user') ->addArgument('user', InputArgument::REQUIRED, 'User to own album') ->addArgument('album', InputArgument::REQUIRED, 'Album name') ->addOption('location', 'l', InputOption::VALUE_REQUIRED, 'Set album location (optional)', ""); From 8b1acd6cb5cb3fa32cd757d162a3f594cb7a204c Mon Sep 17 00:00:00 2001 From: Christian McHugh Date: Tue, 5 Dec 2023 21:52:50 +0000 Subject: [PATCH 6/9] Add get album by name and simplify command logic Signed-off-by: Christian McHugh --- lib/Album/AlbumMapper.php | 17 +++++++++++++++++ lib/Command/AlbumAddCommand.php | 11 +---------- lib/Command/AlbumCreateCommand.php | 11 +---------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/Album/AlbumMapper.php b/lib/Album/AlbumMapper.php index 61e8188c5..5b053fe08 100644 --- a/lib/Album/AlbumMapper.php +++ b/lib/Album/AlbumMapper.php @@ -110,6 +110,23 @@ public function getForUser(string $userId): array { return new AlbumInfo((int)$row['album_id'], $userId, $row['name'], $row['location'], (int)$row['created'], (int)$row['last_added_photo']); }, $rows); } + + /** + * @param string $albumName + * @return AlbumInfo + */ + public function getByName(string $albumName): ?AlbumInfo { + $query = $this->connection->getQueryBuilder(); + $query->select("album_id", "user", "location", "created", "last_added_photo") + ->from("photos_albums") + ->where($query->expr()->eq('name', $query->createNamedParameter($albumName))); + $row = $query->executeQuery()->fetch(); + if ($row) { + return new AlbumInfo((int)$row['album_id'], $row['user'], $albumName, $row['location'], (int)$row['created'], (int)$row['last_added_photo']); + } else { + return null; + } + } /** * @param int $fileId diff --git a/lib/Command/AlbumAddCommand.php b/lib/Command/AlbumAddCommand.php index 888cfcf66..bb6bab5cd 100644 --- a/lib/Command/AlbumAddCommand.php +++ b/lib/Command/AlbumAddCommand.php @@ -85,16 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 1; } - $retrievedAlbums = $this->albumMapper->getForUser($userString); - - $album = null; - foreach ($retrievedAlbums as $singleAlbum) { - if ($singleAlbum->getTitle() == $albumString) { - $album = $singleAlbum; - break; - } - } - + $album = $this->albumMapper->getByName($albumString); if (!$album) { throw new \Exception("Album $albumString was not found"); } diff --git a/lib/Command/AlbumCreateCommand.php b/lib/Command/AlbumCreateCommand.php index b91bea7a9..954e3c3b2 100644 --- a/lib/Command/AlbumCreateCommand.php +++ b/lib/Command/AlbumCreateCommand.php @@ -72,16 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $userID = $user->getUID(); - $retrievedAlbums = $this->albumMapper->getForUser($userString); - - $album = null; - foreach ($retrievedAlbums as $singleAlbum) { - if ($singleAlbum->getTitle() == $albumString) { - $album = $singleAlbum; - break; - } - } - + $album = $this->albumMapper->getByName($albumString); if ($album) { throw new \Exception("Album $albumString already exists and cannot be created."); } From 852811cb2e136f01cad0dc5ebe5bc460dc107868 Mon Sep 17 00:00:00 2001 From: Christian McHugh Date: Tue, 5 Dec 2023 21:58:22 +0000 Subject: [PATCH 7/9] improve wording slightly Signed-off-by: Christian McHugh --- lib/Command/AlbumAddCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Command/AlbumAddCommand.php b/lib/Command/AlbumAddCommand.php index bb6bab5cd..a735d8ee1 100644 --- a/lib/Command/AlbumAddCommand.php +++ b/lib/Command/AlbumAddCommand.php @@ -57,10 +57,10 @@ public function __construct( protected function configure(): void { $this->setName('photos:albums:add') ->setDescription('Add specified photo to album') - ->addArgument('user', InputArgument::REQUIRED, 'User owning album') + ->addArgument('user', InputArgument::REQUIRED, 'User owning the album') ->addArgument('album', InputArgument::REQUIRED, 'Album name') ->addArgument('file', InputArgument::REQUIRED, - 'Path to the file to add to the album. It must already be scanned and available in NextCloud. Example: Photos/picture1.jpg'); + 'Path of file to add to the album. It must already be scanned and available in NextCloud. Example: Photos/picture1.jpg'); } /** From b892270d78013ee2e4e2a458d6a2f0f6efaf0389 Mon Sep 17 00:00:00 2001 From: Christian McHugh Date: Wed, 6 Dec 2023 09:19:29 +0000 Subject: [PATCH 8/9] lint fixes Signed-off-by: Christian McHugh --- lib/Album/AlbumMapper.php | 4 +- lib/Command/AlbumAddCommand.php | 118 ++++++++++++++--------------- lib/Command/AlbumCreateCommand.php | 97 ++++++++++++------------ 3 files changed, 108 insertions(+), 111 deletions(-) diff --git a/lib/Album/AlbumMapper.php b/lib/Album/AlbumMapper.php index 5b053fe08..39537bbfe 100644 --- a/lib/Album/AlbumMapper.php +++ b/lib/Album/AlbumMapper.php @@ -122,9 +122,9 @@ public function getByName(string $albumName): ?AlbumInfo { ->where($query->expr()->eq('name', $query->createNamedParameter($albumName))); $row = $query->executeQuery()->fetch(); if ($row) { - return new AlbumInfo((int)$row['album_id'], $row['user'], $albumName, $row['location'], (int)$row['created'], (int)$row['last_added_photo']); + return new AlbumInfo((int)$row['album_id'], $row['user'], $albumName, $row['location'], (int)$row['created'], (int)$row['last_added_photo']); } else { - return null; + return null; } } diff --git a/lib/Command/AlbumAddCommand.php b/lib/Command/AlbumAddCommand.php index a735d8ee1..9ffa14332 100644 --- a/lib/Command/AlbumAddCommand.php +++ b/lib/Command/AlbumAddCommand.php @@ -24,80 +24,78 @@ */ namespace OCA\Photos\Command; +use OCA\Photos\Album\AlbumMapper; use OCP\Files\IRootFolder; use OCP\IUserManager; -use OCA\Photos\Album\AlbumMapper; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; - class AlbumAddCommand extends Command { - private IRootFolder $rootFolder; - private IUserManager $userManager; - private AlbumMapper $albumMapper; + private IRootFolder $rootFolder; + private IUserManager $userManager; + private AlbumMapper $albumMapper; - public function __construct( - AlbumMapper $albumMapper, - IRootFolder $rootFolder, - IUserManager $userManager, - ) { - $this->rootFolder = $rootFolder; - $this->userManager = $userManager; - $this->albumMapper = $albumMapper; - parent::__construct(); - } + public function __construct( + AlbumMapper $albumMapper, + IRootFolder $rootFolder, + IUserManager $userManager, + ) { + $this->rootFolder = $rootFolder; + $this->userManager = $userManager; + $this->albumMapper = $albumMapper; + parent::__construct(); + } - /** - * Configure the command - */ - protected function configure(): void { - $this->setName('photos:albums:add') - ->setDescription('Add specified photo to album') - ->addArgument('user', InputArgument::REQUIRED, 'User owning the album') - ->addArgument('album', InputArgument::REQUIRED, 'Album name') - ->addArgument('file', InputArgument::REQUIRED, - 'Path of file to add to the album. It must already be scanned and available in NextCloud. Example: Photos/picture1.jpg'); - } + /** + * Configure the command + */ + protected function configure(): void { + $this->setName('photos:albums:add') + ->setDescription('Add specified photo to album') + ->addArgument('user', InputArgument::REQUIRED, 'User owning the album') + ->addArgument('album', InputArgument::REQUIRED, 'Album name') + ->addArgument('file', InputArgument::REQUIRED, + 'Path of file to add to the album. It must already be scanned and available in NextCloud. Example: Photos/picture1.jpg'); + } - /** - * Execute the command - */ - protected function execute(InputInterface $input, OutputInterface $output): int { - $userString = $input->getArgument('user'); - $albumString = $input->getArgument('album'); - $filePath = $input->getArgument('file'); + /** + * Execute the command + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + $userString = $input->getArgument('user'); + $albumString = $input->getArgument('album'); + $filePath = $input->getArgument('file'); - $user = $this->userManager->get($userString); - if ($user === null) { - throw new \Exception("User $userString was not found"); - } - $userID = $user->getUID(); + $user = $this->userManager->get($userString); + if ($user === null) { + throw new \Exception("User $userString was not found"); + } + $userID = $user->getUID(); - try { - $pictureFileID = $this->rootFolder->getUserFolder($userID)->get($filePath)->getId(); - } catch (\Exception $ex) { - $output->writeln('Invalid file path'); - $output->writeln($ex->getMessage()); - return 1; - } + try { + $pictureFileID = $this->rootFolder->getUserFolder($userID)->get($filePath)->getId(); + } catch (\Exception $ex) { + $output->writeln('Invalid file path'); + $output->writeln($ex->getMessage()); + return 1; + } - $album = $this->albumMapper->getByName($albumString); - if (!$album) { - throw new \Exception("Album $albumString was not found"); - } + $album = $this->albumMapper->getByName($albumString); + if (!$album) { + throw new \Exception("Album $albumString was not found"); + } - try { - $this->albumMapper->addFile($album->getId(), $pictureFileID, $userID); - } catch (\Exception $ex) { - $output->writeln("Problem adding $filePath to $albumString"); - $output->writeln($ex->getMessage()); - return 1; - } + try { + $this->albumMapper->addFile($album->getId(), $pictureFileID, $userID); + } catch (\Exception $ex) { + $output->writeln("Problem adding $filePath to $albumString"); + $output->writeln($ex->getMessage()); + return 1; + } - return 0; - } + return 0; + } } diff --git a/lib/Command/AlbumCreateCommand.php b/lib/Command/AlbumCreateCommand.php index 954e3c3b2..11a42b168 100644 --- a/lib/Command/AlbumCreateCommand.php +++ b/lib/Command/AlbumCreateCommand.php @@ -24,67 +24,66 @@ */ namespace OCA\Photos\Command; -use OCP\IUserManager; use OCA\Photos\Album\AlbumMapper; +use OCP\IUserManager; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; - class AlbumCreateCommand extends Command { - private IUserManager $userManager; - private AlbumMapper $albumMapper; + private IUserManager $userManager; + private AlbumMapper $albumMapper; - public function __construct( - AlbumMapper $albumMapper, - IUserManager $userManager, - ) { - $this->userManager = $userManager; - $this->albumMapper = $albumMapper; - parent::__construct(); - } + public function __construct( + AlbumMapper $albumMapper, + IUserManager $userManager, + ) { + $this->userManager = $userManager; + $this->albumMapper = $albumMapper; + parent::__construct(); + } - /** - * Configure the command - */ - protected function configure(): void { - $this->setName('photos:albums:create') - ->setDescription('Create a new album for a user') - ->addArgument('user', InputArgument::REQUIRED, 'User to own album') - ->addArgument('album', InputArgument::REQUIRED, 'Album name') - ->addOption('location', 'l', InputOption::VALUE_REQUIRED, 'Set album location (optional)', ""); - } + /** + * Configure the command + */ + protected function configure(): void { + $this->setName('photos:albums:create') + ->setDescription('Create a new album for a user') + ->addArgument('user', InputArgument::REQUIRED, 'User to own album') + ->addArgument('album', InputArgument::REQUIRED, 'Album name') + ->addOption('location', 'l', InputOption::VALUE_REQUIRED, 'Set album location (optional)', ""); + } - /** - * Execute the command - */ - protected function execute(InputInterface $input, OutputInterface $output): int { - $userString = $input->getArgument('user'); - $albumString = $input->getArgument('album'); - $location = $input->getOption('location'); + /** + * Execute the command + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + $userString = $input->getArgument('user'); + $albumString = $input->getArgument('album'); + $location = $input->getOption('location'); - $user = $this->userManager->get($userString); - if ($user === null) { - throw new \Exception("User $userString was not found"); - } - $userID = $user->getUID(); + $user = $this->userManager->get($userString); + if ($user === null) { + throw new \Exception("User $userString was not found"); + } + $userID = $user->getUID(); - $album = $this->albumMapper->getByName($albumString); - if ($album) { - throw new \Exception("Album $albumString already exists and cannot be created."); - } + $album = $this->albumMapper->getByName($albumString); + if ($album) { + throw new \Exception("Album $albumString already exists and cannot be created."); + } - try { - $this->albumMapper->create($userID, $albumString, $location); - } catch (\Exception $ex) { - $output->writeln('Problem creating album'); - $output->writeln($ex->getMessage()); - return 1; - } + try { + $this->albumMapper->create($userID, $albumString, $location); + } catch (\Exception $ex) { + $output->writeln('Problem creating album'); + $output->writeln($ex->getMessage()); + return 1; + } - return 0; - } -} \ No newline at end of file + return 0; + } +} From 3e2cb7a7344a1392a1695ddc502869399758cc72 Mon Sep 17 00:00:00 2001 From: Christian McHugh Date: Thu, 7 Dec 2023 18:31:29 +0000 Subject: [PATCH 9/9] Require user when querying album name from DB Signed-off-by: Christian McHugh --- lib/Album/AlbumMapper.php | 10 ++++++---- lib/Command/AlbumAddCommand.php | 2 +- lib/Command/AlbumCreateCommand.php | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/Album/AlbumMapper.php b/lib/Album/AlbumMapper.php index 39537bbfe..c70c41182 100644 --- a/lib/Album/AlbumMapper.php +++ b/lib/Album/AlbumMapper.php @@ -113,16 +113,18 @@ public function getForUser(string $userId): array { /** * @param string $albumName + * @param string $userName * @return AlbumInfo */ - public function getByName(string $albumName): ?AlbumInfo { + public function getByName(string $albumName, string $userName): ?AlbumInfo { $query = $this->connection->getQueryBuilder(); - $query->select("album_id", "user", "location", "created", "last_added_photo") + $query->select("album_id", "location", "created", "last_added_photo") ->from("photos_albums") - ->where($query->expr()->eq('name', $query->createNamedParameter($albumName))); + ->where($query->expr()->eq('name', $query->createNamedParameter($albumName))) + ->andWhere($query->expr()->eq('user', $query->createNamedParameter($userName))); $row = $query->executeQuery()->fetch(); if ($row) { - return new AlbumInfo((int)$row['album_id'], $row['user'], $albumName, $row['location'], (int)$row['created'], (int)$row['last_added_photo']); + return new AlbumInfo((int)$row['album_id'], $userName, $albumName, $row['location'], (int)$row['created'], (int)$row['last_added_photo']); } else { return null; } diff --git a/lib/Command/AlbumAddCommand.php b/lib/Command/AlbumAddCommand.php index 9ffa14332..3d62396c7 100644 --- a/lib/Command/AlbumAddCommand.php +++ b/lib/Command/AlbumAddCommand.php @@ -83,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 1; } - $album = $this->albumMapper->getByName($albumString); + $album = $this->albumMapper->getByName($albumString, $userString); if (!$album) { throw new \Exception("Album $albumString was not found"); } diff --git a/lib/Command/AlbumCreateCommand.php b/lib/Command/AlbumCreateCommand.php index 11a42b168..99053b3a2 100644 --- a/lib/Command/AlbumCreateCommand.php +++ b/lib/Command/AlbumCreateCommand.php @@ -71,7 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $userID = $user->getUID(); - $album = $this->albumMapper->getByName($albumString); + $album = $this->albumMapper->getByName($albumString, $userString); if ($album) { throw new \Exception("Album $albumString already exists and cannot be created."); }