1
0

Put.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. }
  47. public function execute(InputInterface $input, OutputInterface $output): int {
  48. $object = $input->getArgument('object');
  49. $inputName = (string)$input->getArgument('input');
  50. $objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output);
  51. if (!$objectStore) {
  52. return -1;
  53. }
  54. if ($fileId = $this->objectUtils->objectExistsInDb($object)) {
  55. $output->writeln("<error>Warning, object $object belongs to an existing file, overwriting the object contents can lead to unexpected behavior.</error>");
  56. $output->writeln("You can use <info>occ files:put $inputName $fileId</info> to write to the file safely.");
  57. $output->writeln("");
  58. /** @var QuestionHelper $helper */
  59. $helper = $this->getHelper('question');
  60. $question = new ConfirmationQuestion("Write to the object anyway? [y/N] ", false);
  61. if (!$helper->ask($input, $output, $question)) {
  62. return -1;
  63. }
  64. }
  65. $source = $inputName === '-' ? STDIN : fopen($inputName, 'r');
  66. if (!$source) {
  67. $output->writeln("<error>Failed to open $inputName</error>");
  68. return self::FAILURE;
  69. }
  70. $objectStore->writeObject($object, $source, $this->mimeTypeDetector->detectPath($inputName));
  71. return self::SUCCESS;
  72. }
  73. }