ResetRenderedTexts.php 5.9 KB

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