BackgroundCleanupJob.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. class BackgroundCleanupJob extends TimedJob {
  17. /** @var IDBConnection */
  18. private $connection;
  19. /** @var Root */
  20. private $previewFolder;
  21. /** @var bool */
  22. private $isCLI;
  23. /** @var IMimeTypeLoader */
  24. private $mimeTypeLoader;
  25. public function __construct(ITimeFactory $timeFactory,
  26. IDBConnection $connection,
  27. Root $previewFolder,
  28. IMimeTypeLoader $mimeTypeLoader,
  29. bool $isCLI) {
  30. parent::__construct($timeFactory);
  31. // Run at most once an hour
  32. $this->setInterval(3600);
  33. $this->connection = $connection;
  34. $this->previewFolder = $previewFolder;
  35. $this->isCLI = $isCLI;
  36. $this->mimeTypeLoader = $mimeTypeLoader;
  37. }
  38. public function run($argument) {
  39. foreach ($this->getDeletedFiles() as $fileId) {
  40. try {
  41. $preview = $this->previewFolder->getFolder((string)$fileId);
  42. $preview->delete();
  43. } catch (NotFoundException $e) {
  44. // continue
  45. } catch (NotPermittedException $e) {
  46. // continue
  47. }
  48. }
  49. }
  50. private function getDeletedFiles(): \Iterator {
  51. yield from $this->getOldPreviewLocations();
  52. yield from $this->getNewPreviewLocations();
  53. }
  54. private function getOldPreviewLocations(): \Iterator {
  55. $qb = $this->connection->getQueryBuilder();
  56. $qb->select('a.name')
  57. ->from('filecache', 'a')
  58. ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
  59. $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
  60. ))
  61. ->where(
  62. $qb->expr()->isNull('b.fileid')
  63. )->andWhere(
  64. $qb->expr()->eq('a.storage', $qb->createNamedParameter($this->previewFolder->getStorageId()))
  65. )->andWhere(
  66. $qb->expr()->eq('a.parent', $qb->createNamedParameter($this->previewFolder->getId()))
  67. )->andWhere(
  68. $qb->expr()->like('a.name', $qb->createNamedParameter('__%'))
  69. );
  70. if (!$this->isCLI) {
  71. $qb->setMaxResults(10);
  72. }
  73. $cursor = $qb->execute();
  74. while ($row = $cursor->fetch()) {
  75. yield $row['name'];
  76. }
  77. $cursor->closeCursor();
  78. }
  79. private function getNewPreviewLocations(): \Iterator {
  80. $qb = $this->connection->getQueryBuilder();
  81. $qb->select('path', 'mimetype')
  82. ->from('filecache')
  83. ->where($qb->expr()->eq('fileid', $qb->createNamedParameter($this->previewFolder->getId())));
  84. $cursor = $qb->execute();
  85. $data = $cursor->fetch();
  86. $cursor->closeCursor();
  87. if ($data === null) {
  88. return [];
  89. }
  90. /*
  91. * This lovely like is the result of the way the new previews are stored
  92. * We take the md5 of the name (fileid) and split the first 7 chars. That way
  93. * there are not a gazillion files in the root of the preview appdata.
  94. */
  95. $like = $this->connection->escapeLikeParameter($data['path']) . '/_/_/_/_/_/_/_/%';
  96. /*
  97. * Deleting a file will not delete related previews right away.
  98. *
  99. * A delete request is usually an HTTP request.
  100. * The preview deleting is done by a background job to avoid timeouts.
  101. *
  102. * Previews for a file are stored within a folder in appdata_/preview using the fileid as folder name.
  103. * Preview folders in oc_filecache are identified by a.storage, a.path (cf. $like) and a.mimetype.
  104. *
  105. * To find preview folders to delete, we query oc_filecache for a preview folder in app data, matching the preview folder structure
  106. * 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),
  107. * even if there are no matches in the right table (b).
  108. *
  109. * If the related file is deleted, b.fileid will be null and the preview folder can be deleted.
  110. */
  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. }