You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BackgroundCleanupJob.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\BackgroundJob\TimedJob;
  26. use OC\Preview\Storage\Root;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. use OCP\Files\IMimeTypeLoader;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\NotPermittedException;
  31. use OCP\IDBConnection;
  32. class BackgroundCleanupJob extends TimedJob {
  33. /** @var IDBConnection */
  34. private $connection;
  35. /** @var Root */
  36. private $previewFolder;
  37. /** @var bool */
  38. private $isCLI;
  39. /** @var IMimeTypeLoader */
  40. private $mimeTypeLoader;
  41. public function __construct(IDBConnection $connection,
  42. Root $previewFolder,
  43. IMimeTypeLoader $mimeTypeLoader,
  44. bool $isCLI) {
  45. // Run at most once an hour
  46. $this->setInterval(3600);
  47. $this->connection = $connection;
  48. $this->previewFolder = $previewFolder;
  49. $this->isCLI = $isCLI;
  50. $this->mimeTypeLoader = $mimeTypeLoader;
  51. }
  52. public function run($argument) {
  53. foreach ($this->getDeletedFiles() as $fileId) {
  54. try {
  55. $preview = $this->previewFolder->getFolder((string)$fileId);
  56. $preview->delete();
  57. } catch (NotFoundException $e) {
  58. // continue
  59. } catch (NotPermittedException $e) {
  60. // continue
  61. }
  62. }
  63. }
  64. private function getDeletedFiles(): \Iterator {
  65. yield from $this->getOldPreviewLocations();
  66. yield from $this->getNewPreviewLocations();
  67. }
  68. private function getOldPreviewLocations(): \Iterator {
  69. $qb = $this->connection->getQueryBuilder();
  70. $qb->select('a.name')
  71. ->from('filecache', 'a')
  72. ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
  73. $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
  74. ))
  75. ->where(
  76. $qb->expr()->isNull('b.fileid')
  77. )->andWhere(
  78. $qb->expr()->eq('a.parent', $qb->createNamedParameter($this->previewFolder->getId()))
  79. )->andWhere(
  80. $qb->expr()->like('a.name', $qb->createNamedParameter('__%'))
  81. );
  82. if (!$this->isCLI) {
  83. $qb->setMaxResults(10);
  84. }
  85. $cursor = $qb->execute();
  86. while ($row = $cursor->fetch()) {
  87. yield $row['name'];
  88. }
  89. $cursor->closeCursor();
  90. }
  91. private function getNewPreviewLocations(): \Iterator {
  92. $qb = $this->connection->getQueryBuilder();
  93. $qb->select('path', 'mimetype')
  94. ->from('filecache')
  95. ->where($qb->expr()->eq('fileid', $qb->createNamedParameter($this->previewFolder->getId())));
  96. $cursor = $qb->execute();
  97. $data = $cursor->fetch();
  98. $cursor->closeCursor();
  99. if ($data === null) {
  100. return [];
  101. }
  102. /*
  103. * This lovely like is the result of the way the new previews are stored
  104. * We take the md5 of the name (fileid) and split the first 7 chars. That way
  105. * there are not a gazillion files in the root of the preview appdata.
  106. */
  107. $like = $this->connection->escapeLikeParameter($data['path']) . '/_/_/_/_/_/_/_/%';
  108. $qb = $this->connection->getQueryBuilder();
  109. $qb->select('a.name')
  110. ->from('filecache', 'a')
  111. ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
  112. $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
  113. ))
  114. ->where(
  115. $qb->expr()->andX(
  116. $qb->expr()->isNull('b.fileid'),
  117. $qb->expr()->like('a.path', $qb->createNamedParameter($like)),
  118. $qb->expr()->eq('a.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('httpd/unix-directory')))
  119. )
  120. );
  121. if (!$this->isCLI) {
  122. $qb->setMaxResults(10);
  123. }
  124. $cursor = $qb->execute();
  125. while ($row = $cursor->fetch()) {
  126. yield $row['name'];
  127. }
  128. $cursor->closeCursor();
  129. }
  130. }