BackgroundCleanupJob.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /*
  112. * Deleting a file will not delete related previews right away.
  113. *
  114. * A delete request is usually an HTTP request.
  115. * The preview deleting is done by a background job to avoid timeouts.
  116. *
  117. * Previews for a file are stored within a folder in appdata_/preview using the fileid as folder name.
  118. * Preview folders in oc_filecache are identified by a.storage, a.path (cf. $like) and a.mimetype.
  119. *
  120. * To find preview folders to delete, we query oc_filecache for a preview folder in app data, matching the preview folder structure
  121. * and use the name to left join oc_filecache on a.name = b.fileid. A left join returns all rows from the left table (a),
  122. * even if there are no matches in the right table (b).
  123. *
  124. * If the related file is deleted, b.fileid will be null and the preview folder can be deleted.
  125. */
  126. $qb = $this->connection->getQueryBuilder();
  127. $qb->select('a.name')
  128. ->from('filecache', 'a')
  129. ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
  130. $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
  131. ))
  132. ->where(
  133. $qb->expr()->andX(
  134. $qb->expr()->eq('a.storage', $qb->createNamedParameter($this->previewFolder->getStorageId())),
  135. $qb->expr()->isNull('b.fileid'),
  136. $qb->expr()->like('a.path', $qb->createNamedParameter($like)),
  137. $qb->expr()->eq('a.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('httpd/unix-directory')))
  138. )
  139. );
  140. if (!$this->isCLI) {
  141. $qb->setMaxResults(10);
  142. }
  143. $cursor = $qb->execute();
  144. while ($row = $cursor->fetch()) {
  145. yield $row['name'];
  146. }
  147. $cursor->closeCursor();
  148. }
  149. }