ResetRenderedTexts.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Command\Preview;
  8. use OC\Preview\Storage\Root;
  9. use OCP\DB\QueryBuilder\IQueryBuilder;
  10. use OCP\Files\IMimeTypeLoader;
  11. use OCP\Files\NotFoundException;
  12. use OCP\Files\NotPermittedException;
  13. use OCP\IAvatarManager;
  14. use OCP\IDBConnection;
  15. use OCP\IUserManager;
  16. use Symfony\Component\Console\Command\Command;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Input\InputOption;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. class ResetRenderedTexts extends Command {
  21. public function __construct(
  22. protected IDBConnection $connection,
  23. protected IUserManager $userManager,
  24. protected IAvatarManager $avatarManager,
  25. private Root $previewFolder,
  26. private IMimeTypeLoader $mimeTypeLoader,
  27. ) {
  28. parent::__construct();
  29. }
  30. protected function configure() {
  31. $this
  32. ->setName('preview:reset-rendered-texts')
  33. ->setDescription('Deletes all generated avatars and previews of text and md files')
  34. ->addOption('dry', 'd', InputOption::VALUE_NONE, 'Dry mode - will not delete any files - in combination with the verbose mode one could check the operations.');
  35. }
  36. protected function execute(InputInterface $input, OutputInterface $output): int {
  37. $dryMode = $input->getOption('dry');
  38. if ($dryMode) {
  39. $output->writeln('INFO: The command is run in dry mode and will not modify anything.');
  40. $output->writeln('');
  41. }
  42. $this->deleteAvatars($output, $dryMode);
  43. $this->deletePreviews($output, $dryMode);
  44. return 0;
  45. }
  46. private function deleteAvatars(OutputInterface $output, bool $dryMode): void {
  47. $avatarsToDeleteCount = 0;
  48. foreach ($this->getAvatarsToDelete() as [$userId, $avatar]) {
  49. $output->writeln('Deleting avatar for ' . $userId, OutputInterface::VERBOSITY_VERBOSE);
  50. $avatarsToDeleteCount++;
  51. if ($dryMode) {
  52. continue;
  53. }
  54. try {
  55. $avatar->remove();
  56. } catch (NotFoundException $e) {
  57. // continue
  58. } catch (NotPermittedException $e) {
  59. // continue
  60. }
  61. }
  62. $output->writeln('Deleted ' . $avatarsToDeleteCount . ' avatars');
  63. $output->writeln('');
  64. }
  65. private function getAvatarsToDelete(): \Iterator {
  66. foreach ($this->userManager->search('') as $user) {
  67. $avatar = $this->avatarManager->getAvatar($user->getUID());
  68. if (!$avatar->isCustomAvatar()) {
  69. yield [$user->getUID(), $avatar];
  70. }
  71. }
  72. }
  73. private function deletePreviews(OutputInterface $output, bool $dryMode): void {
  74. $previewsToDeleteCount = 0;
  75. foreach ($this->getPreviewsToDelete() as ['name' => $previewFileId, 'path' => $filePath]) {
  76. $output->writeln('Deleting previews for ' . $filePath, OutputInterface::VERBOSITY_VERBOSE);
  77. $previewsToDeleteCount++;
  78. if ($dryMode) {
  79. continue;
  80. }
  81. try {
  82. $preview = $this->previewFolder->getFolder((string)$previewFileId);
  83. $preview->delete();
  84. } catch (NotFoundException $e) {
  85. // continue
  86. } catch (NotPermittedException $e) {
  87. // continue
  88. }
  89. }
  90. $output->writeln('Deleted ' . $previewsToDeleteCount . ' previews');
  91. }
  92. // Copy pasted and adjusted from
  93. // "lib/private/Preview/BackgroundCleanupJob.php".
  94. private function getPreviewsToDelete(): \Iterator {
  95. $qb = $this->connection->getQueryBuilder();
  96. $qb->select('path', 'mimetype')
  97. ->from('filecache')
  98. ->where($qb->expr()->eq('fileid', $qb->createNamedParameter($this->previewFolder->getId())));
  99. $cursor = $qb->executeQuery();
  100. $data = $cursor->fetch();
  101. $cursor->closeCursor();
  102. if ($data === null) {
  103. return [];
  104. }
  105. /*
  106. * This lovely like is the result of the way the new previews are stored
  107. * We take the md5 of the name (fileid) and split the first 7 chars. That way
  108. * there are not a gazillion files in the root of the preview appdata.
  109. */
  110. $like = $this->connection->escapeLikeParameter($data['path']) . '/_/_/_/_/_/_/_/%';
  111. $qb = $this->connection->getQueryBuilder();
  112. $qb->select('a.name', 'b.path')
  113. ->from('filecache', 'a')
  114. ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
  115. $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
  116. ))
  117. ->where(
  118. $qb->expr()->andX(
  119. $qb->expr()->like('a.path', $qb->createNamedParameter($like)),
  120. $qb->expr()->eq('a.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('httpd/unix-directory'))),
  121. $qb->expr()->orX(
  122. $qb->expr()->eq('b.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('text/plain'))),
  123. $qb->expr()->eq('b.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('text/markdown'))),
  124. $qb->expr()->eq('b.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('text/x-markdown')))
  125. )
  126. )
  127. );
  128. $cursor = $qb->executeQuery();
  129. while ($row = $cursor->fetch()) {
  130. yield $row;
  131. }
  132. $cursor->closeCursor();
  133. }
  134. }