1
0

BackgroundCleanupJob.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Preview;
  24. use OC\BackgroundJob\TimedJob;
  25. use OC\Files\AppData\Factory;
  26. use OCP\DB\QueryBuilder\IQueryBuilder;
  27. use OCP\Files\NotFoundException;
  28. use OCP\Files\NotPermittedException;
  29. use OCP\IDBConnection;
  30. class BackgroundCleanupJob extends TimedJob {
  31. /** @var IDBConnection */
  32. private $connection;
  33. /** @var Factory */
  34. private $appDataFactory;
  35. /** @var bool */
  36. private $isCLI;
  37. public function __construct(IDBConnection $connection,
  38. Factory $appDataFactory,
  39. bool $isCLI) {
  40. // Run at most once an hour
  41. $this->setInterval(3600);
  42. $this->connection = $connection;
  43. $this->appDataFactory = $appDataFactory;
  44. $this->isCLI = $isCLI;
  45. }
  46. public function run($argument) {
  47. $previews = $this->appDataFactory->get('preview');
  48. $previewFodlerId = $previews->getId();
  49. $qb = $this->connection->getQueryBuilder();
  50. $qb->select('a.name')
  51. ->from('filecache', 'a')
  52. ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
  53. $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
  54. ))
  55. ->where(
  56. $qb->expr()->isNull('b.fileid')
  57. )->andWhere(
  58. $qb->expr()->eq('a.parent', $qb->createNamedParameter($previewFodlerId))
  59. );
  60. if (!$this->isCLI) {
  61. $qb->setMaxResults(10);
  62. }
  63. $cursor = $qb->execute();
  64. while ($row = $cursor->fetch()) {
  65. try {
  66. $preview = $previews->getFolder($row['name']);
  67. $preview->delete();
  68. } catch (NotFoundException $e) {
  69. // continue
  70. } catch (NotPermittedException $e) {
  71. // continue
  72. }
  73. }
  74. $cursor->closeCursor();
  75. }
  76. }