BackgroundCleanupJob.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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\Preview;
  25. use OC\Preview\Storage\Root;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\BackgroundJob\TimedJob;
  28. use OCP\DB\QueryBuilder\IQueryBuilder;
  29. use OCP\Files\IMimeTypeLoader;
  30. use OCP\Files\NotFoundException;
  31. use OCP\Files\NotPermittedException;
  32. use OCP\IDBConnection;
  33. class BackgroundCleanupJob extends TimedJob {
  34. /** @var IDBConnection */
  35. private $connection;
  36. /** @var Root */
  37. private $previewFolder;
  38. /** @var bool */
  39. private $isCLI;
  40. /** @var IMimeTypeLoader */
  41. private $mimeTypeLoader;
  42. public function __construct(ITimeFactory $timeFactory,
  43. IDBConnection $connection,
  44. Root $previewFolder,
  45. IMimeTypeLoader $mimeTypeLoader,
  46. bool $isCLI) {
  47. parent::__construct($timeFactory);
  48. // Run at most once an hour
  49. $this->setInterval(3600);
  50. $this->connection = $connection;
  51. $this->previewFolder = $previewFolder;
  52. $this->isCLI = $isCLI;
  53. $this->mimeTypeLoader = $mimeTypeLoader;
  54. }
  55. public function run($argument) {
  56. foreach ($this->getDeletedFiles() as $fileId) {
  57. try {
  58. $preview = $this->previewFolder->getFolder((string)$fileId);
  59. $preview->delete();
  60. } catch (NotFoundException $e) {
  61. // continue
  62. } catch (NotPermittedException $e) {
  63. // continue
  64. }
  65. }
  66. }
  67. private function getDeletedFiles(): \Iterator {
  68. yield from $this->getOldPreviewLocations();
  69. yield from $this->getNewPreviewLocations();
  70. }
  71. private function getOldPreviewLocations(): \Iterator {
  72. $qb = $this->connection->getQueryBuilder();
  73. $qb->select('a.name')
  74. ->from('filecache', 'a')
  75. ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
  76. $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
  77. ))
  78. ->where(
  79. $qb->expr()->isNull('b.fileid')
  80. )->andWhere(
  81. $qb->expr()->eq('a.parent', $qb->createNamedParameter($this->previewFolder->getId()))
  82. )->andWhere(
  83. $qb->expr()->like('a.name', $qb->createNamedParameter('__%'))
  84. );
  85. if (!$this->isCLI) {
  86. $qb->setMaxResults(10);
  87. }
  88. $cursor = $qb->execute();
  89. while ($row = $cursor->fetch()) {
  90. yield $row['name'];
  91. }
  92. $cursor->closeCursor();
  93. }
  94. private function getNewPreviewLocations(): \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->execute();
  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')
  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()->eq('a.storage', $qb->createNamedParameter($this->previewFolder->getStorageId())),
  120. $qb->expr()->isNull('b.fileid'),
  121. $qb->expr()->like('a.path', $qb->createNamedParameter($like)),
  122. $qb->expr()->eq('a.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('httpd/unix-directory')))
  123. )
  124. );
  125. if (!$this->isCLI) {
  126. $qb->setMaxResults(10);
  127. }
  128. $cursor = $qb->execute();
  129. while ($row = $cursor->fetch()) {
  130. yield $row['name'];
  131. }
  132. $cursor->closeCursor();
  133. }
  134. }