ResetRenderedTexts.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021, Daniel Calviño Sánchez <danxuliu@gmail.com>
  5. *
  6. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Command\Preview;
  25. use OC\Preview\Storage\Root;
  26. use OCP\DB\QueryBuilder\IQueryBuilder;
  27. use OCP\Files\IMimeTypeLoader;
  28. use OCP\Files\NotFoundException;
  29. use OCP\Files\NotPermittedException;
  30. use OCP\IAvatarManager;
  31. use OCP\IDBConnection;
  32. use OCP\IUserManager;
  33. use Symfony\Component\Console\Command\Command;
  34. use Symfony\Component\Console\Input\InputInterface;
  35. use Symfony\Component\Console\Input\InputOption;
  36. use Symfony\Component\Console\Output\OutputInterface;
  37. class ResetRenderedTexts extends Command {
  38. public function __construct(
  39. protected IDBConnection $connection,
  40. protected IUserManager $userManager,
  41. protected IAvatarManager $avatarManager,
  42. private Root $previewFolder,
  43. private IMimeTypeLoader $mimeTypeLoader,
  44. ) {
  45. parent::__construct();
  46. }
  47. protected function configure() {
  48. $this
  49. ->setName('preview:reset-rendered-texts')
  50. ->setDescription('Deletes all generated avatars and previews of text and md files')
  51. ->addOption('dry', 'd', InputOption::VALUE_NONE, 'Dry mode - will not delete any files - in combination with the verbose mode one could check the operations.');
  52. }
  53. protected function execute(InputInterface $input, OutputInterface $output): int {
  54. $dryMode = $input->getOption('dry');
  55. if ($dryMode) {
  56. $output->writeln('INFO: The command is run in dry mode and will not modify anything.');
  57. $output->writeln('');
  58. }
  59. $this->deleteAvatars($output, $dryMode);
  60. $this->deletePreviews($output, $dryMode);
  61. return 0;
  62. }
  63. private function deleteAvatars(OutputInterface $output, bool $dryMode): void {
  64. $avatarsToDeleteCount = 0;
  65. foreach ($this->getAvatarsToDelete() as [$userId, $avatar]) {
  66. $output->writeln('Deleting avatar for ' . $userId, OutputInterface::VERBOSITY_VERBOSE);
  67. $avatarsToDeleteCount++;
  68. if ($dryMode) {
  69. continue;
  70. }
  71. try {
  72. $avatar->remove();
  73. } catch (NotFoundException $e) {
  74. // continue
  75. } catch (NotPermittedException $e) {
  76. // continue
  77. }
  78. }
  79. $output->writeln('Deleted ' . $avatarsToDeleteCount . ' avatars');
  80. $output->writeln('');
  81. }
  82. private function getAvatarsToDelete(): \Iterator {
  83. foreach ($this->userManager->search('') as $user) {
  84. $avatar = $this->avatarManager->getAvatar($user->getUID());
  85. if (!$avatar->isCustomAvatar()) {
  86. yield [$user->getUID(), $avatar];
  87. }
  88. }
  89. }
  90. private function deletePreviews(OutputInterface $output, bool $dryMode): void {
  91. $previewsToDeleteCount = 0;
  92. foreach ($this->getPreviewsToDelete() as ['name' => $previewFileId, 'path' => $filePath]) {
  93. $output->writeln('Deleting previews for ' . $filePath, OutputInterface::VERBOSITY_VERBOSE);
  94. $previewsToDeleteCount++;
  95. if ($dryMode) {
  96. continue;
  97. }
  98. try {
  99. $preview = $this->previewFolder->getFolder((string)$previewFileId);
  100. $preview->delete();
  101. } catch (NotFoundException $e) {
  102. // continue
  103. } catch (NotPermittedException $e) {
  104. // continue
  105. }
  106. }
  107. $output->writeln('Deleted ' . $previewsToDeleteCount . ' previews');
  108. }
  109. // Copy pasted and adjusted from
  110. // "lib/private/Preview/BackgroundCleanupJob.php".
  111. private function getPreviewsToDelete(): \Iterator {
  112. $qb = $this->connection->getQueryBuilder();
  113. $qb->select('path', 'mimetype')
  114. ->from('filecache')
  115. ->where($qb->expr()->eq('fileid', $qb->createNamedParameter($this->previewFolder->getId())));
  116. $cursor = $qb->execute();
  117. $data = $cursor->fetch();
  118. $cursor->closeCursor();
  119. if ($data === null) {
  120. return [];
  121. }
  122. /*
  123. * This lovely like is the result of the way the new previews are stored
  124. * We take the md5 of the name (fileid) and split the first 7 chars. That way
  125. * there are not a gazillion files in the root of the preview appdata.
  126. */
  127. $like = $this->connection->escapeLikeParameter($data['path']) . '/_/_/_/_/_/_/_/%';
  128. $qb = $this->connection->getQueryBuilder();
  129. $qb->select('a.name', 'b.path')
  130. ->from('filecache', 'a')
  131. ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
  132. $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
  133. ))
  134. ->where(
  135. $qb->expr()->andX(
  136. $qb->expr()->like('a.path', $qb->createNamedParameter($like)),
  137. $qb->expr()->eq('a.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('httpd/unix-directory'))),
  138. $qb->expr()->orX(
  139. $qb->expr()->eq('b.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('text/plain'))),
  140. $qb->expr()->eq('b.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('text/markdown'))),
  141. $qb->expr()->eq('b.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('text/x-markdown')))
  142. )
  143. )
  144. );
  145. $cursor = $qb->execute();
  146. while ($row = $cursor->fetch()) {
  147. yield $row;
  148. }
  149. $cursor->closeCursor();
  150. }
  151. }