1
0

Put.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 OCA\Files\Command\Object;
  8. use OCP\Files\IMimeTypeDetector;
  9. use Symfony\Component\Console\Command\Command;
  10. use Symfony\Component\Console\Helper\QuestionHelper;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Question\ConfirmationQuestion;
  16. class Put extends Command {
  17. public function __construct(
  18. private ObjectUtil $objectUtils,
  19. private IMimeTypeDetector $mimeTypeDetector,
  20. ) {
  21. parent::__construct();
  22. }
  23. protected function configure(): void {
  24. $this
  25. ->setName('files:object:put')
  26. ->setDescription('Write a file to the object store')
  27. ->addArgument('input', InputArgument::REQUIRED, 'Source local path, use - to read from STDIN')
  28. ->addArgument('object', InputArgument::REQUIRED, 'Object to write')
  29. ->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket where to store the object, only required in cases where it can't be determined from the config");
  30. ;
  31. }
  32. public function execute(InputInterface $input, OutputInterface $output): int {
  33. $object = $input->getArgument('object');
  34. $inputName = (string)$input->getArgument('input');
  35. $objectStore = $this->objectUtils->getObjectStore($input->getOption('bucket'), $output);
  36. if (!$objectStore) {
  37. return -1;
  38. }
  39. if ($fileId = $this->objectUtils->objectExistsInDb($object)) {
  40. $output->writeln("<error>Warning, object $object belongs to an existing file, overwriting the object contents can lead to unexpected behavior.</error>");
  41. $output->writeln("You can use <info>occ files:put $inputName $fileId</info> to write to the file safely.");
  42. $output->writeln('');
  43. /** @var QuestionHelper $helper */
  44. $helper = $this->getHelper('question');
  45. $question = new ConfirmationQuestion('Write to the object anyway? [y/N] ', false);
  46. if (!$helper->ask($input, $output, $question)) {
  47. return -1;
  48. }
  49. }
  50. $source = $inputName === '-' ? STDIN : fopen($inputName, 'r');
  51. if (!$source) {
  52. $output->writeln("<error>Failed to open $inputName</error>");
  53. return self::FAILURE;
  54. }
  55. $objectStore->writeObject($object, $source, $this->mimeTypeDetector->detectPath($inputName));
  56. return self::SUCCESS;
  57. }
  58. }