Generate.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Preview;
  8. use OCP\Files\Config\IUserMountCache;
  9. use OCP\Files\File;
  10. use OCP\Files\IRootFolder;
  11. use OCP\Files\Node;
  12. use OCP\Files\NotFoundException;
  13. use OCP\IPreview;
  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 Generate extends Command {
  20. public function __construct(
  21. private IRootFolder $rootFolder,
  22. private IUserMountCache $userMountCache,
  23. private IPreview $previewManager,
  24. ) {
  25. parent::__construct();
  26. }
  27. protected function configure() {
  28. $this
  29. ->setName('preview:generate')
  30. ->setDescription('generate a preview for a file')
  31. ->addArgument('file', InputArgument::REQUIRED, 'path or fileid of the file to generate the preview for')
  32. ->addOption('size', 's', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'size to generate the preview for in pixels, defaults to 64x64', ['64x64'])
  33. ->addOption('crop', 'c', InputOption::VALUE_NONE, 'crop the previews instead of maintaining aspect ratio')
  34. ->addOption('mode', 'm', InputOption::VALUE_REQUIRED, "mode for generating uncropped previews, 'cover' or 'fill'", IPreview::MODE_FILL);
  35. }
  36. protected function execute(InputInterface $input, OutputInterface $output): int {
  37. $fileInput = $input->getArgument('file');
  38. $sizes = $input->getOption('size');
  39. $sizes = array_map(function (string $size) use ($output) {
  40. if (str_contains($size, 'x')) {
  41. $sizeParts = explode('x', $size, 2);
  42. } else {
  43. $sizeParts = [$size, $size];
  44. }
  45. if (!is_numeric($sizeParts[0]) || !is_numeric($sizeParts[1] ?? null)) {
  46. $output->writeln("<error>Invalid size $size</error>");
  47. return null;
  48. }
  49. return array_map('intval', $sizeParts);
  50. }, $sizes);
  51. if (in_array(null, $sizes)) {
  52. return 1;
  53. }
  54. $mode = $input->getOption('mode');
  55. if ($mode !== IPreview::MODE_FILL && $mode !== IPreview::MODE_COVER) {
  56. $output->writeln("<error>Invalid mode $mode</error>");
  57. return 1;
  58. }
  59. $crop = $input->getOption('crop');
  60. $file = $this->getFile($fileInput);
  61. if (!$file) {
  62. $output->writeln("<error>File $fileInput not found</error>");
  63. return 1;
  64. }
  65. if (!$file instanceof File) {
  66. $output->writeln("<error>Can't generate previews for folders</error>");
  67. return 1;
  68. }
  69. if (!$this->previewManager->isAvailable($file)) {
  70. $output->writeln('<error>No preview generator available for file of type' . $file->getMimetype() . '</error>');
  71. return 1;
  72. }
  73. $specifications = array_map(function (array $sizes) use ($crop, $mode) {
  74. return [
  75. 'width' => $sizes[0],
  76. 'height' => $sizes[1],
  77. 'crop' => $crop,
  78. 'mode' => $mode,
  79. ];
  80. }, $sizes);
  81. $this->previewManager->generatePreviews($file, $specifications);
  82. if (count($specifications) > 1) {
  83. $output->writeln('generated <info>' . count($specifications) . '</info> previews');
  84. } else {
  85. $output->writeln('preview generated');
  86. }
  87. return 0;
  88. }
  89. private function getFile(string $fileInput): ?Node {
  90. if (is_numeric($fileInput)) {
  91. $mounts = $this->userMountCache->getMountsForFileId((int)$fileInput);
  92. if (!$mounts) {
  93. return null;
  94. }
  95. $mount = $mounts[0];
  96. $userFolder = $this->rootFolder->getUserFolder($mount->getUser()->getUID());
  97. return $userFolder->getFirstNodeById((int)$fileInput);
  98. } else {
  99. try {
  100. return $this->rootFolder->get($fileInput);
  101. } catch (NotFoundException $e) {
  102. return null;
  103. }
  104. }
  105. }
  106. }