From dcd7832a69c0af53252d72c349b3c998dd071594 Mon Sep 17 00:00:00 2001 From: tetebueno <9064236+tetebueno@users.noreply.github.com> Date: Fri, 22 Nov 2024 19:08:28 +0000 Subject: [PATCH] Unified logic in PhotofilesService+rescan(). Signed-off-by: tetebueno --- lib/Command/RescanPhotos.php | 35 +++++++------------------------ lib/Service/PhotofilesService.php | 24 +++++++-------------- 2 files changed, 15 insertions(+), 44 deletions(-) diff --git a/lib/Command/RescanPhotos.php b/lib/Command/RescanPhotos.php index 14bb92300..22439b5cf 100644 --- a/lib/Command/RescanPhotos.php +++ b/lib/Command/RescanPhotos.php @@ -89,20 +89,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int } if ($userId === null) { $this->userManager->callForSeenUsers(function (IUser $user) use ($inBackground) { - if ($pathToScan === null) { - $this->rescanUserPhotos($user->getUID(), $inBackground); - } else { - $this->scanUserPhotos($user->getUID(), $pathToScan, $inBackground); - } + $this->rescanUserPhotos($user->getUID(), $inBackground, $pathToScan); }); } else { $user = $this->userManager->get($userId); if ($user !== null) { - if ($pathToScan === null) { - $this->rescanUserPhotos($userId, $inBackground); - } else { - $this->scanUserPhotos($userId, $pathToScan, $inBackground); - } + $this->rescanUserPhotos($userId, $inBackground, $pathToScan); } } return 0; @@ -111,32 +103,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** * @param string $userId * @param bool $inBackground - * @return void - * @throws \OCP\PreConditionNotMetException - */ - private function rescanUserPhotos(string $userId, bool $inBackground=true) { - echo '======== User '.$userId.' ========'."\n"; - $c = 1; - foreach ($this->photofilesService->rescan($userId, $inBackground) as $path) { - echo '['.$c.'] Photo "'.$path.'" added'."\n"; - $c++; - } - $this->config->setUserValue($userId, 'maps', 'installScanDone', 'yes'); - } - - /** - * @param string $userId * @param string $pathToScan - * @param bool $inBackground * @return void * @throws \OCP\PreConditionNotMetException */ - private function scanUserPhotos($userId, $pathToScan, bool $inBackground=true) { + private function rescanUserPhotos(string $userId, bool $inBackground=true, string $pathToScan=null) { echo '======== User '.$userId.' ========'."\n"; $c = 1; - foreach ($this->photofilesService->rescanPath($userId, $pathToScan, $inBackground) as $path) { + foreach ($this->photofilesService->rescan($userId, $inBackground, $pathToScan) as $path) { echo '['.$c.'] Photo "'.$path.'" added'."\n"; $c++; } + if ($pathToScan === null) { + $this->config->setUserValue($userId, 'maps', 'installScanDone', 'yes'); + } } } diff --git a/lib/Service/PhotofilesService.php b/lib/Service/PhotofilesService.php index fd2fb8318..b9147e31b 100644 --- a/lib/Service/PhotofilesService.php +++ b/lib/Service/PhotofilesService.php @@ -78,11 +78,16 @@ public function __construct (ILogger $logger, $this->backgroundJobCache = $this->cacheFactory->createDistributed('maps:background-jobs'); } - public function rescan($userId, $inBackground=true) { + public function rescan($userId, $inBackground=true, $pathToScan=null) { $this->photosCache->clear($userId); $userFolder = $this->root->getUserFolder($userId); - $photos = $this->gatherPhotoFiles($userFolder, true); - $this->photoMapper->deleteAll($userId); + if ($pathToScan === null) { + $folder = $userFolder; + $this->photoMapper->deleteAll($userId); + } else { + $folder = $userFolder->get($pathToScan); + } + $photos = $this->gatherPhotoFiles($folder, true); foreach ($photos as $photo) { if ($inBackground) { $this->addPhoto($photo, $userId); @@ -93,19 +98,6 @@ public function rescan($userId, $inBackground=true) { } } - public function rescanPath($userId, $pathToScan, $inBackground=true) { - $userFolder = $this->root->getUserFolder($userId)->get($pathToScan); - $photos = $this->gatherPhotoFiles($userFolder, true); - foreach ($photos as $photo) { - if ($inBackground) { - $this->addPhoto($photo, $userId); - } else { - $this->addPhotoNow($photo, $userId); - } - yield $photo->getPath(); - } - } - // add the file for its owner and users that have access // check if it's already in DB before adding public function addByFile(Node $file) {