Skip to content

Commit

Permalink
Unified logic in PhotofilesService+rescan().
Browse files Browse the repository at this point in the history
Signed-off-by: tetebueno <[email protected]>
  • Loading branch information
tetebueno authored and tetebueno committed Nov 22, 2024
1 parent 74b937f commit dcd7832
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 44 deletions.
35 changes: 7 additions & 28 deletions lib/Command/RescanPhotos.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
}
}
}
24 changes: 8 additions & 16 deletions lib/Service/PhotofilesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit dcd7832

Please sign in to comment.