GenerateMetadataJob.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\IAppConfig;
  33. use OCP\IConfig;
  34. use OCP\IUserManager;
  35. use Psr\Log\LoggerInterface;
  36. class GenerateMetadataJob extends TimedJob {
  37. // Default file size limit for metadata generation (MBytes).
  38. protected const DEFAULT_MAX_FILESIZE = 256;
  39. public function __construct(
  40. ITimeFactory $time,
  41. private IConfig $config,
  42. private IAppConfig $appConfig,
  43. private IRootFolder $rootFolder,
  44. private IUserManager $userManager,
  45. private IFilesMetadataManager $filesMetadataManager,
  46. private IJobList $jobList,
  47. private LoggerInterface $logger,
  48. ) {
  49. parent::__construct($time);
  50. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  51. $this->setInterval(24 * 60 * 60);
  52. }
  53. protected function run(mixed $argument): void {
  54. if ($this->appConfig->getValueBool('core', 'metadataGenerationDone', false)) {
  55. return;
  56. }
  57. $lastHandledUser = $this->appConfig->getValueString('core', 'metadataGenerationLastHandledUser', '');
  58. $users = $this->userManager->search('');
  59. // we'll only start timer once we have found a valid user to handle
  60. // meaning NOW if we have not handled any user from a previous run
  61. $startTime = ($lastHandledUser === '') ? time() : null;
  62. foreach ($users as $user) {
  63. $userId = $user->getUID();
  64. // if we already handled a previous run, we start timer only when we face the last handled user
  65. if ($startTime === null) {
  66. if ($userId === $lastHandledUser) {
  67. $startTime = time();
  68. }
  69. continue;
  70. }
  71. $this->appConfig->setValueString('core', 'metadataGenerationLastHandledUser', $userId);
  72. $this->scanFilesForUser($user->getUID());
  73. // Stop if execution time is more than one hour.
  74. if (time() - $startTime > 3600) {
  75. return;
  76. }
  77. }
  78. $this->appConfig->deleteKey('core', 'metadataGenerationLastHandledUser');
  79. $this->appConfig->setValueBool('core', 'metadataGenerationDone', true);
  80. }
  81. private function scanFilesForUser(string $userId): void {
  82. $userFolder = $this->rootFolder->getUserFolder($userId);
  83. $this->scanFolder($userFolder);
  84. }
  85. private function scanFolder(Folder $folder): void {
  86. // Do not scan share and other moveable mounts.
  87. if ($folder->getMountPoint() instanceof \OC\Files\Mount\MoveableMount) {
  88. return;
  89. }
  90. foreach ($folder->getDirectoryListing() as $node) {
  91. if ($node instanceof Folder) {
  92. $this->scanFolder($node);
  93. continue;
  94. }
  95. // Don't generate metadata for files bigger than configured metadata_max_filesize
  96. // Files are loaded in memory so very big files can lead to an OOM on the server
  97. $nodeSize = $node->getSize();
  98. $nodeLimit = $this->config->getSystemValueInt('metadata_max_filesize', self::DEFAULT_MAX_FILESIZE);
  99. if ($nodeSize > $nodeLimit * 1000000) {
  100. $this->logger->debug("Skipping generating metadata for fileid " . $node->getId() . " as its size exceeds configured 'metadata_max_filesize'.");
  101. continue;
  102. }
  103. try {
  104. $this->filesMetadataManager->getMetadata($node->getId(), false);
  105. } catch (FilesMetadataNotFoundException) {
  106. try {
  107. $this->filesMetadataManager->refreshMetadata(
  108. $node,
  109. IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
  110. );
  111. } catch (\Throwable $ex) {
  112. $this->logger->warning("Error while generating metadata for fileid " . $node->getId(), ['exception' => $ex]);
  113. }
  114. }
  115. }
  116. }
  117. }