1
0

BackgroundCleanupJob.php 5.2 KB

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