UpdateSingleMetadata.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2023 Maxence Lange <maxence@artificial-owl.com>
  5. *
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  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\FilesMetadata\Job;
  25. use OC\FilesMetadata\FilesMetadataManager;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\BackgroundJob\QueuedJob;
  28. use OCP\Files\IRootFolder;
  29. use OCP\FilesMetadata\Event\MetadataLiveEvent;
  30. use OCP\FilesMetadata\IFilesMetadataManager;
  31. use Psr\Log\LoggerInterface;
  32. /**
  33. * Simple background job, created when requested by an app during the
  34. * dispatch of MetadataLiveEvent.
  35. * This background job will re-run the event to refresh metadata on a non-live thread.
  36. *
  37. * @see MetadataLiveEvent::requestBackgroundJob()
  38. * @since 28.0.0
  39. */
  40. class UpdateSingleMetadata extends QueuedJob {
  41. public function __construct(
  42. ITimeFactory $time,
  43. private IRootFolder $rootFolder,
  44. private FilesMetadataManager $filesMetadataManager,
  45. private LoggerInterface $logger
  46. ) {
  47. parent::__construct($time);
  48. }
  49. protected function run($argument) {
  50. [$userId, $fileId] = $argument;
  51. try {
  52. $node = $this->rootFolder->getUserFolder($userId)->getFirstNodeById($fileId);
  53. if ($node) {
  54. $this->filesMetadataManager->refreshMetadata($node, IFilesMetadataManager::PROCESS_BACKGROUND);
  55. }
  56. } catch (\Exception $e) {
  57. $this->logger->warning('issue while running UpdateSingleMetadata', ['exception' => $e, 'userId' => $userId, 'fileId' => $fileId]);
  58. }
  59. }
  60. }