1
0

BackgroundCleanupJob.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.parent', $qb->createNamedParameter($this->previewFolder->getId()))
  65. )->andWhere(
  66. $qb->expr()->like('a.name', $qb->createNamedParameter('__%'))
  67. );
  68. if (!$this->isCLI) {
  69. $qb->setMaxResults(10);
  70. }
  71. $cursor = $qb->execute();
  72. while ($row = $cursor->fetch()) {
  73. yield $row['name'];
  74. }
  75. $cursor->closeCursor();
  76. }
  77. private function getNewPreviewLocations(): \Iterator {
  78. $qb = $this->connection->getQueryBuilder();
  79. $qb->select('path', 'mimetype')
  80. ->from('filecache')
  81. ->where($qb->expr()->eq('fileid', $qb->createNamedParameter($this->previewFolder->getId())));
  82. $cursor = $qb->execute();
  83. $data = $cursor->fetch();
  84. $cursor->closeCursor();
  85. if ($data === null) {
  86. return [];
  87. }
  88. /*
  89. * This lovely like is the result of the way the new previews are stored
  90. * We take the md5 of the name (fileid) and split the first 7 chars. That way
  91. * there are not a gazillion files in the root of the preview appdata.
  92. */
  93. $like = $this->connection->escapeLikeParameter($data['path']) . '/_/_/_/_/_/_/_/%';
  94. /*
  95. * Deleting a file will not delete related previews right away.
  96. *
  97. * A delete request is usually an HTTP request.
  98. * The preview deleting is done by a background job to avoid timeouts.
  99. *
  100. * Previews for a file are stored within a folder in appdata_/preview using the fileid as folder name.
  101. * Preview folders in oc_filecache are identified by a.storage, a.path (cf. $like) and a.mimetype.
  102. *
  103. * To find preview folders to delete, we query oc_filecache for a preview folder in app data, matching the preview folder structure
  104. * 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),
  105. * even if there are no matches in the right table (b).
  106. *
  107. * If the related file is deleted, b.fileid will be null and the preview folder can be deleted.
  108. */
  109. $qb = $this->connection->getQueryBuilder();
  110. $qb->select('a.name')
  111. ->from('filecache', 'a')
  112. ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
  113. $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
  114. ))
  115. ->where(
  116. $qb->expr()->andX(
  117. $qb->expr()->eq('a.storage', $qb->createNamedParameter($this->previewFolder->getStorageId())),
  118. $qb->expr()->isNull('b.fileid'),
  119. $qb->expr()->like('a.path', $qb->createNamedParameter($like)),
  120. $qb->expr()->eq('a.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('httpd/unix-directory')))
  121. )
  122. );
  123. if (!$this->isCLI) {
  124. $qb->setMaxResults(10);
  125. }
  126. $cursor = $qb->execute();
  127. while ($row = $cursor->fetch()) {
  128. yield $row['name'];
  129. }
  130. $cursor->closeCursor();
  131. }
  132. }