|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * @copyright Copyright (c) 2023 Louis Chemineau <louis@chmn.me> |
| 6 | + * |
| 7 | + * @author Louis Chemineau <louis@chmn.me> |
| 8 | + * |
| 9 | + * @license AGPL-3.0-or-later |
| 10 | + * |
| 11 | + * This program is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU Affero General Public License as |
| 13 | + * published by the Free Software Foundation, either version 3 of the |
| 14 | + * License, or (at your option) any later version. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Affero General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Affero General Public License |
| 22 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +namespace OC\Core\BackgroundJobs; |
| 27 | + |
| 28 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 29 | +use OCP\BackgroundJob\IJobList; |
| 30 | +use OCP\BackgroundJob\TimedJob; |
| 31 | +use OCP\Files\Folder; |
| 32 | +use OCP\Files\IRootFolder; |
| 33 | +use OCP\FilesMetadata\IFilesMetadataManager; |
| 34 | +use OCP\IConfig; |
| 35 | +use OCP\IUserManager; |
| 36 | + |
| 37 | +class GenerateMetadataJob extends TimedJob { |
| 38 | + public function __construct( |
| 39 | + ITimeFactory $time, |
| 40 | + private IConfig $config, |
| 41 | + private IRootFolder $rootFolder, |
| 42 | + private IUserManager $userManager, |
| 43 | + private IFilesMetadataManager $filesMetadataManager, |
| 44 | + private IJobList $jobList, |
| 45 | + ) { |
| 46 | + parent::__construct($time); |
| 47 | + |
| 48 | + $this->setTimeSensitivity(\OCP\BackgroundJob\IJob::TIME_INSENSITIVE); |
| 49 | + $this->setInterval(24 * 3600); |
| 50 | + } |
| 51 | + |
| 52 | + protected function run(mixed $argument): void { |
| 53 | + $users = $this->userManager->search(''); |
| 54 | + $lastMappedUser = $this->config->getAppValue('core', 'metadataGenerationLastHandledUser', ''); |
| 55 | + |
| 56 | + if ($lastMappedUser === '') { |
| 57 | + $lastMappedUser = $users[array_key_first($users)]->getUID(); |
| 58 | + } |
| 59 | + |
| 60 | + $startTime = null; |
| 61 | + foreach ($users as $user) { |
| 62 | + if ($startTime === null) { |
| 63 | + // Skip all user before lastMappedUser. |
| 64 | + if ($lastMappedUser !== $user->getUID()) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + $startTime = time(); |
| 69 | + } |
| 70 | + |
| 71 | + // Stop if execution time is more than one hour. |
| 72 | + if (time() - $startTime > 60 * 60) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + $this->scanFilesForUser($user->getUID()); |
| 77 | + $this->config->setAppValue('core', 'metadataGenerationLastHandledUser', $user->getUID()); |
| 78 | + } |
| 79 | + |
| 80 | + $this->jobList->remove(GenerateMetadataJob::class); |
| 81 | + } |
| 82 | + |
| 83 | + private function scanFilesForUser(string $userId): void { |
| 84 | + $userFolder = $this->rootFolder->getUserFolder($userId); |
| 85 | + $this->scanFolder($userFolder); |
| 86 | + } |
| 87 | + |
| 88 | + private function scanFolder(Folder $folder): void { |
| 89 | + // Do not scan share and other moveable mounts. |
| 90 | + if ($folder->getMountPoint() instanceof \OC\Files\Mount\MoveableMount) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + foreach ($folder->getDirectoryListing() as $node) { |
| 95 | + if ($node instanceof Folder) { |
| 96 | + $this->scanFolder($node); |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + $this->filesMetadataManager->refreshMetadata( |
| 101 | + $node, |
| 102 | + IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments