Get.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Core\Command\FilesMetadata;
  8. use OC\User\NoUserException;
  9. use OCP\Files\IRootFolder;
  10. use OCP\Files\NotFoundException;
  11. use OCP\Files\NotPermittedException;
  12. use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException;
  13. use OCP\FilesMetadata\IFilesMetadataManager;
  14. use Symfony\Component\Console\Command\Command;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. class Get extends Command {
  20. public function __construct(
  21. private IRootFolder $rootFolder,
  22. private IFilesMetadataManager $filesMetadataManager,
  23. ) {
  24. parent::__construct();
  25. }
  26. protected function configure(): void {
  27. $this->setName('metadata:get')
  28. ->setDescription('get stored metadata about a file, by its id')
  29. ->addArgument(
  30. 'fileId',
  31. InputArgument::REQUIRED,
  32. 'id of the file document'
  33. )
  34. ->addArgument(
  35. 'userId',
  36. InputArgument::OPTIONAL,
  37. 'file owner'
  38. )
  39. ->addOption(
  40. 'as-array',
  41. '',
  42. InputOption::VALUE_NONE,
  43. 'display metadata as a simple key=>value array'
  44. )
  45. ->addOption(
  46. 'refresh',
  47. '',
  48. InputOption::VALUE_NONE,
  49. 'refresh metadata'
  50. )
  51. ->addOption(
  52. 'reset',
  53. '',
  54. InputOption::VALUE_NONE,
  55. 'refresh metadata from scratch'
  56. );
  57. }
  58. /**
  59. * @throws NotPermittedException
  60. * @throws FilesMetadataNotFoundException
  61. * @throws NoUserException
  62. * @throws NotFoundException
  63. */
  64. protected function execute(InputInterface $input, OutputInterface $output): int {
  65. $fileId = (int)$input->getArgument('fileId');
  66. if ($input->getOption('reset')) {
  67. $this->filesMetadataManager->deleteMetadata($fileId);
  68. if (!$input->getOption('refresh')) {
  69. return self::SUCCESS;
  70. }
  71. }
  72. if ($input->getOption('refresh')) {
  73. $node = $this->rootFolder->getUserFolder($input->getArgument('userId'))->getFirstNodeById($fileId);
  74. if (!$node) {
  75. throw new NotFoundException();
  76. }
  77. $metadata = $this->filesMetadataManager->refreshMetadata(
  78. $node,
  79. IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
  80. );
  81. } else {
  82. $metadata = $this->filesMetadataManager->getMetadata($fileId);
  83. }
  84. if ($input->getOption('as-array')) {
  85. $output->writeln(json_encode($metadata->asArray(), JSON_PRETTY_PRINT));
  86. } else {
  87. $output->writeln(json_encode($metadata, JSON_PRETTY_PRINT));
  88. }
  89. return self::SUCCESS;
  90. }
  91. }