1
0

GenerateMetadataJob.php 3.7 KB

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