BackgroundCleanupJob.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Preview;
  8. use OC\Preview\Storage\Root;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\BackgroundJob\TimedJob;
  11. use OCP\DB\QueryBuilder\IQueryBuilder;
  12. use OCP\Files\IMimeTypeLoader;
  13. use OCP\Files\NotFoundException;
  14. use OCP\Files\NotPermittedException;
  15. use OCP\IDBConnection;
  16. use function Symfony\Component\Translation\t;
  17. class BackgroundCleanupJob extends TimedJob {
  18. /** @var IDBConnection */
  19. private $connection;
  20. /** @var Root */
  21. private $previewFolder;
  22. /** @var bool */
  23. private $isCLI;
  24. /** @var IMimeTypeLoader */
  25. private $mimeTypeLoader;
  26. public function __construct(ITimeFactory $timeFactory,
  27. IDBConnection $connection,
  28. Root $previewFolder,
  29. IMimeTypeLoader $mimeTypeLoader,
  30. bool $isCLI) {
  31. parent::__construct($timeFactory);
  32. // Run at most once an hour
  33. $this->setInterval(3600);
  34. $this->connection = $connection;
  35. $this->previewFolder = $previewFolder;
  36. $this->isCLI = $isCLI;
  37. $this->mimeTypeLoader = $mimeTypeLoader;
  38. }
  39. public function run($argument) {
  40. foreach ($this->getDeletedFiles() as $fileId) {
  41. try {
  42. $preview = $this->previewFolder->getFolder((string)$fileId);
  43. $preview->delete();
  44. } catch (NotFoundException $e) {
  45. // continue
  46. } catch (NotPermittedException $e) {
  47. // continue
  48. }
  49. }
  50. }
  51. private function getDeletedFiles(): \Iterator {
  52. yield from $this->getOldPreviewLocations();
  53. yield from $this->getNewPreviewLocations();
  54. }
  55. private function getOldPreviewLocations(): \Iterator {
  56. if ($this->connection->getShardDefinition('filecache')) {
  57. // sharding is new enough that we don't need to support this
  58. return;
  59. }
  60. $qb = $this->connection->getQueryBuilder();
  61. $qb->select('a.name')
  62. ->from('filecache', 'a')
  63. ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
  64. $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
  65. ))
  66. ->where(
  67. $qb->expr()->isNull('b.fileid')
  68. )->andWhere(
  69. $qb->expr()->eq('a.storage', $qb->createNamedParameter($this->previewFolder->getStorageId()))
  70. )->andWhere(
  71. $qb->expr()->eq('a.parent', $qb->createNamedParameter($this->previewFolder->getId()))
  72. )->andWhere(
  73. $qb->expr()->like('a.name', $qb->createNamedParameter('__%'))
  74. );
  75. if (!$this->isCLI) {
  76. $qb->setMaxResults(10);
  77. }
  78. $cursor = $qb->execute();
  79. while ($row = $cursor->fetch()) {
  80. yield $row['name'];
  81. }
  82. $cursor->closeCursor();
  83. }
  84. private function getNewPreviewLocations(): \Iterator {
  85. $qb = $this->connection->getQueryBuilder();
  86. $qb->select('path', 'mimetype')
  87. ->from('filecache')
  88. ->where($qb->expr()->eq('fileid', $qb->createNamedParameter($this->previewFolder->getId())));
  89. $cursor = $qb->execute();
  90. $data = $cursor->fetch();
  91. $cursor->closeCursor();
  92. if ($data === null) {
  93. return [];
  94. }
  95. if ($this->connection->getShardDefinition('filecache')) {
  96. $chunks = $this->getAllPreviewIds($data['path'], 1000);
  97. foreach ($chunks as $chunk) {
  98. yield from $this->findMissingSources($chunk);
  99. }
  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. /*
  109. * Deleting a file will not delete related previews right away.
  110. *
  111. * A delete request is usually an HTTP request.
  112. * The preview deleting is done by a background job to avoid timeouts.
  113. *
  114. * Previews for a file are stored within a folder in appdata_/preview using the fileid as folder name.
  115. * Preview folders in oc_filecache are identified by a.storage, a.path (cf. $like) and a.mimetype.
  116. *
  117. * To find preview folders to delete, we query oc_filecache for a preview folder in app data, matching the preview folder structure
  118. * 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),
  119. * even if there are no matches in the right table (b).
  120. *
  121. * If the related file is deleted, b.fileid will be null and the preview folder can be deleted.
  122. */
  123. $qb = $this->connection->getQueryBuilder();
  124. $qb->select('a.name')
  125. ->from('filecache', 'a')
  126. ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
  127. $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
  128. ))
  129. ->where(
  130. $qb->expr()->andX(
  131. $qb->expr()->eq('a.storage', $qb->createNamedParameter($this->previewFolder->getStorageId())),
  132. $qb->expr()->isNull('b.fileid'),
  133. $qb->expr()->like('a.path', $qb->createNamedParameter($like)),
  134. $qb->expr()->eq('a.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('httpd/unix-directory')))
  135. )
  136. );
  137. if (!$this->isCLI) {
  138. $qb->setMaxResults(10);
  139. }
  140. $cursor = $qb->execute();
  141. while ($row = $cursor->fetch()) {
  142. yield $row['name'];
  143. }
  144. $cursor->closeCursor();
  145. }
  146. private function getAllPreviewIds(string $previewRoot, int $chunkSize): \Iterator {
  147. // See `getNewPreviewLocations` for some more info about the logic here
  148. $like = $this->connection->escapeLikeParameter($previewRoot). '/_/_/_/_/_/_/_/%';
  149. $qb = $this->connection->getQueryBuilder();
  150. $qb->select('name', 'fileid')
  151. ->from('filecache')
  152. ->where(
  153. $qb->expr()->andX(
  154. $qb->expr()->eq('storage', $qb->createNamedParameter($this->previewFolder->getStorageId())),
  155. $qb->expr()->like('path', $qb->createNamedParameter($like)),
  156. $qb->expr()->eq('mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('httpd/unix-directory'))),
  157. $qb->expr()->gt('fileid', $qb->createParameter('min_id')),
  158. )
  159. )
  160. ->orderBy('fileid', 'ASC')
  161. ->setMaxResults($chunkSize);
  162. $minId = 0;
  163. while (true) {
  164. $qb->setParameter('min_id', $minId);
  165. $rows = $qb->executeQuery()->fetchAll();
  166. if (count($rows) > 0) {
  167. $minId = $rows[count($rows) - 1]['fileid'];
  168. yield array_map(function ($row) {
  169. return (int)$row['name'];
  170. }, $rows);
  171. } else {
  172. break;
  173. }
  174. }
  175. }
  176. private function findMissingSources(array $ids): array {
  177. $qb = $this->connection->getQueryBuilder();
  178. $qb->select('fileid')
  179. ->from('filecache')
  180. ->where($qb->expr()->in('fileid', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
  181. $found = $qb->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
  182. return array_diff($ids, $found);
  183. }
  184. }