Put.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\Files\Command\Object;
  23. use OCP\Files\IMimeTypeDetector;
  24. use Symfony\Component\Console\Command\Command;
  25. use Symfony\Component\Console\Helper\QuestionHelper;
  26. use Symfony\Component\Console\Input\InputArgument;
  27. use Symfony\Component\Console\Input\InputInterface;
  28. use Symfony\Component\Console\Input\InputOption;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. use Symfony\Component\Console\Question\ConfirmationQuestion;
  31. class Put extends Command {
  32. public function __construct(
  33. private ObjectUtil $objectUtils,
  34. private IMimeTypeDetector $mimeTypeDetector,
  35. ) {
  36. parent::__construct();
  37. }
  38. protected function configure(): void {
  39. $this
  40. ->setName('files:object:put')
  41. ->setDescription('Write a file to the object store')
  42. ->addArgument('input', InputArgument::REQUIRED, "Source local path, use - to read from STDIN")
  43. ->addArgument('object', InputArgument::REQUIRED, "Object to write")
  44. ->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");;
  45. }
  46. public function execute(InputInterface $input, OutputInterface $output): int {
  47. $object = $input->getArgument('object');
  48. $inputName = (string)$input->getArgument('input');
  49. $objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output);
  50. if (!$objectStore) {
  51. return -1;
  52. }
  53. if ($fileId = $this->objectUtils->objectExistsInDb($object)) {
  54. $output->writeln("<error>Warning, object $object belongs to an existing file, overwriting the object contents can lead to unexpected behavior.</error>");
  55. $output->writeln("You can use <info>occ files:put $inputName $fileId</info> to write to the file safely.");
  56. $output->writeln("");
  57. /** @var QuestionHelper $helper */
  58. $helper = $this->getHelper('question');
  59. $question = new ConfirmationQuestion("Write to the object anyway? [y/N] ", false);
  60. if (!$helper->ask($input, $output, $question)) {
  61. return -1;
  62. }
  63. }
  64. $source = $inputName === '-' ? STDIN : fopen($inputName, 'r');
  65. if (!$source) {
  66. $output->writeln("<error>Failed to open $inputName</error>");
  67. return self::FAILURE;
  68. }
  69. $objectStore->writeObject($object, $source, $this->mimeTypeDetector->detectPath($inputName));
  70. return self::SUCCESS;
  71. }
  72. }