1
0

UpdateSingleMetadata.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\FilesMetadata\Job;
  8. use OC\FilesMetadata\FilesMetadataManager;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\BackgroundJob\QueuedJob;
  11. use OCP\Files\IRootFolder;
  12. use OCP\FilesMetadata\Event\MetadataLiveEvent;
  13. use OCP\FilesMetadata\IFilesMetadataManager;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16. * Simple background job, created when requested by an app during the
  17. * dispatch of MetadataLiveEvent.
  18. * This background job will re-run the event to refresh metadata on a non-live thread.
  19. *
  20. * @see MetadataLiveEvent::requestBackgroundJob()
  21. * @since 28.0.0
  22. */
  23. class UpdateSingleMetadata extends QueuedJob {
  24. public function __construct(
  25. ITimeFactory $time,
  26. private IRootFolder $rootFolder,
  27. private FilesMetadataManager $filesMetadataManager,
  28. private LoggerInterface $logger
  29. ) {
  30. parent::__construct($time);
  31. }
  32. protected function run($argument) {
  33. [$userId, $fileId] = $argument;
  34. try {
  35. $node = $this->rootFolder->getUserFolder($userId)->getFirstNodeById($fileId);
  36. if ($node) {
  37. $this->filesMetadataManager->refreshMetadata($node, IFilesMetadataManager::PROCESS_BACKGROUND);
  38. }
  39. } catch (\Exception $e) {
  40. $this->logger->warning('issue while running UpdateSingleMetadata', ['exception' => $e, 'userId' => $userId, 'fileId' => $fileId]);
  41. }
  42. }
  43. }