ResetRenderedTexts.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. protected IDBConnection $connection;
  39. protected IUserManager $userManager;
  40. protected IAvatarManager $avatarManager;
  41. private Root $previewFolder;
  42. private IMimeTypeLoader $mimeTypeLoader;
  43. public function __construct(IDBConnection $connection,
  44. IUserManager $userManager,
  45. IAvatarManager $avatarManager,
  46. Root $previewFolder,
  47. IMimeTypeLoader $mimeTypeLoader) {
  48. parent::__construct();
  49. $this->connection = $connection;
  50. $this->userManager = $userManager;
  51. $this->avatarManager = $avatarManager;
  52. $this->previewFolder = $previewFolder;
  53. $this->mimeTypeLoader = $mimeTypeLoader;
  54. }
  55. protected function configure() {
  56. $this
  57. ->setName('preview:reset-rendered-texts')
  58. ->setDescription('Deletes all generated avatars and previews of text and md files')
  59. ->addOption('dry', 'd', InputOption::VALUE_NONE, 'Dry mode - will not delete any files - in combination with the verbose mode one could check the operations.');
  60. }
  61. protected function execute(InputInterface $input, OutputInterface $output): int {
  62. $dryMode = $input->getOption('dry');
  63. if ($dryMode) {
  64. $output->writeln('INFO: The command is run in dry mode and will not modify anything.');
  65. $output->writeln('');
  66. }
  67. $this->deleteAvatars($output, $dryMode);
  68. $this->deletePreviews($output, $dryMode);
  69. return 0;
  70. }
  71. private function deleteAvatars(OutputInterface $output, bool $dryMode): void {
  72. $avatarsToDeleteCount = 0;
  73. foreach ($this->getAvatarsToDelete() as [$userId, $avatar]) {
  74. $output->writeln('Deleting avatar for ' . $userId, OutputInterface::VERBOSITY_VERBOSE);
  75. $avatarsToDeleteCount++;
  76. if ($dryMode) {
  77. continue;
  78. }
  79. try {
  80. $avatar->remove();
  81. } catch (NotFoundException $e) {
  82. // continue
  83. } catch (NotPermittedException $e) {
  84. // continue
  85. }
  86. }
  87. $output->writeln('Deleted ' . $avatarsToDeleteCount . ' avatars');
  88. $output->writeln('');
  89. }
  90. private function getAvatarsToDelete(): \Iterator {
  91. foreach ($this->userManager->search('') as $user) {
  92. $avatar = $this->avatarManager->getAvatar($user->getUID());
  93. if (!$avatar->isCustomAvatar()) {
  94. yield [$user->getUID(), $avatar];
  95. }
  96. }
  97. }
  98. private function deletePreviews(OutputInterface $output, bool $dryMode): void {
  99. $previewsToDeleteCount = 0;
  100. foreach ($this->getPreviewsToDelete() as ['name' => $previewFileId, 'path' => $filePath]) {
  101. $output->writeln('Deleting previews for ' . $filePath, OutputInterface::VERBOSITY_VERBOSE);
  102. $previewsToDeleteCount++;
  103. if ($dryMode) {
  104. continue;
  105. }
  106. try {
  107. $preview = $this->previewFolder->getFolder((string)$previewFileId);
  108. $preview->delete();
  109. } catch (NotFoundException $e) {
  110. // continue
  111. } catch (NotPermittedException $e) {
  112. // continue
  113. }
  114. }
  115. $output->writeln('Deleted ' . $previewsToDeleteCount . ' previews');
  116. }
  117. // Copy pasted and adjusted from
  118. // "lib/private/Preview/BackgroundCleanupJob.php".
  119. private function getPreviewsToDelete(): \Iterator {
  120. $qb = $this->connection->getQueryBuilder();
  121. $qb->select('path', 'mimetype')
  122. ->from('filecache')
  123. ->where($qb->expr()->eq('fileid', $qb->createNamedParameter($this->previewFolder->getId())));
  124. $cursor = $qb->execute();
  125. $data = $cursor->fetch();
  126. $cursor->closeCursor();
  127. if ($data === null) {
  128. return [];
  129. }
  130. /*
  131. * This lovely like is the result of the way the new previews are stored
  132. * We take the md5 of the name (fileid) and split the first 7 chars. That way
  133. * there are not a gazillion files in the root of the preview appdata.
  134. */
  135. $like = $this->connection->escapeLikeParameter($data['path']) . '/_/_/_/_/_/_/_/%';
  136. $qb = $this->connection->getQueryBuilder();
  137. $qb->select('a.name', 'b.path')
  138. ->from('filecache', 'a')
  139. ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
  140. $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
  141. ))
  142. ->where(
  143. $qb->expr()->andX(
  144. $qb->expr()->like('a.path', $qb->createNamedParameter($like)),
  145. $qb->expr()->eq('a.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('httpd/unix-directory'))),
  146. $qb->expr()->orX(
  147. $qb->expr()->eq('b.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('text/plain'))),
  148. $qb->expr()->eq('b.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('text/markdown'))),
  149. $qb->expr()->eq('b.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('text/x-markdown')))
  150. )
  151. )
  152. );
  153. $cursor = $qb->execute();
  154. while ($row = $cursor->fetch()) {
  155. yield $row;
  156. }
  157. $cursor->closeCursor();
  158. }
  159. }