Cleanup.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Command\Preview;
  8. use OC\Core\Command\Base;
  9. use OCP\Files\Folder;
  10. use OCP\Files\IRootFolder;
  11. use OCP\Files\NotFoundException;
  12. use OCP\Files\NotPermittedException;
  13. use Psr\Log\LoggerInterface;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class Cleanup extends Base {
  17. public function __construct(
  18. private IRootFolder $rootFolder,
  19. private LoggerInterface $logger,
  20. ) {
  21. parent::__construct();
  22. }
  23. protected function configure(): void {
  24. $this
  25. ->setName('preview:cleanup')
  26. ->setDescription('Removes existing preview files');
  27. }
  28. protected function execute(InputInterface $input, OutputInterface $output): int {
  29. try {
  30. $appDataFolder = $this->rootFolder->get($this->rootFolder->getAppDataDirectoryName());
  31. if (!$appDataFolder instanceof Folder) {
  32. $this->logger->error("Previews can't be removed: appdata is not a folder");
  33. $output->writeln("Previews can't be removed: appdata is not a folder");
  34. return 1;
  35. }
  36. /** @var Folder $previewFolder */
  37. $previewFolder = $appDataFolder->get('preview');
  38. } catch (NotFoundException $e) {
  39. $this->logger->error("Previews can't be removed: appdata folder can't be found", ['exception' => $e]);
  40. $output->writeln("Previews can't be removed: preview folder isn't deletable");
  41. return 1;
  42. }
  43. if (!$previewFolder->isDeletable()) {
  44. $this->logger->error("Previews can't be removed: preview folder isn't deletable");
  45. $output->writeln("Previews can't be removed: preview folder isn't deletable");
  46. return 1;
  47. }
  48. try {
  49. $previewFolder->delete();
  50. $this->logger->debug('Preview folder deleted');
  51. $output->writeln('Preview folder deleted', OutputInterface::VERBOSITY_VERBOSE);
  52. } catch (NotFoundException $e) {
  53. $output->writeln("Previews weren't deleted: preview folder was not found while deleting it");
  54. $this->logger->error("Previews weren't deleted: preview folder was not found while deleting it", ['exception' => $e]);
  55. return 1;
  56. } catch (NotPermittedException $e) {
  57. $output->writeln("Previews weren't deleted: you don't have the permission to delete preview folder");
  58. $this->logger->error("Previews weren't deleted: you don't have the permission to delete preview folder", ['exception' => $e]);
  59. return 1;
  60. }
  61. try {
  62. $appDataFolder->newFolder('preview');
  63. $this->logger->debug('Preview folder recreated');
  64. $output->writeln('Preview folder recreated', OutputInterface::VERBOSITY_VERBOSE);
  65. } catch (NotPermittedException $e) {
  66. $output->writeln("Preview folder was deleted, but you don't have the permission to create preview folder");
  67. $this->logger->error("Preview folder was deleted, but you don't have the permission to create preview folder", ['exception' => $e]);
  68. return 1;
  69. }
  70. $output->writeln('Previews removed');
  71. return 0;
  72. }
  73. }