CleanPreviewsBackgroundJob.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  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\Repair\Owncloud;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\BackgroundJob\IJobList;
  27. use OCP\BackgroundJob\QueuedJob;
  28. use OCP\Files\Folder;
  29. use OCP\Files\IRootFolder;
  30. use OCP\Files\NotFoundException;
  31. use OCP\Files\NotPermittedException;
  32. use OCP\IUserManager;
  33. use Psr\Log\LoggerInterface;
  34. class CleanPreviewsBackgroundJob extends QueuedJob {
  35. public function __construct(
  36. private IRootFolder $rootFolder,
  37. private LoggerInterface $logger,
  38. private IJobList $jobList,
  39. ITimeFactory $timeFactory,
  40. private IUserManager $userManager,
  41. ) {
  42. parent::__construct($timeFactory);
  43. }
  44. public function run($arguments) {
  45. $uid = $arguments['uid'];
  46. if (!$this->userManager->userExists($uid)) {
  47. $this->logger->info('User no longer exists, skip user ' . $uid);
  48. return;
  49. }
  50. $this->logger->info('Started preview cleanup for ' . $uid);
  51. $empty = $this->cleanupPreviews($uid);
  52. if (!$empty) {
  53. $this->jobList->add(self::class, ['uid' => $uid]);
  54. $this->logger->info('New preview cleanup scheduled for ' . $uid);
  55. } else {
  56. $this->logger->info('Preview cleanup done for ' . $uid);
  57. }
  58. }
  59. /**
  60. * @param string $uid
  61. */
  62. private function cleanupPreviews($uid): bool {
  63. try {
  64. $userFolder = $this->rootFolder->getUserFolder($uid);
  65. } catch (NotFoundException $e) {
  66. return true;
  67. }
  68. $userRoot = $userFolder->getParent();
  69. try {
  70. /** @var Folder $thumbnailFolder */
  71. $thumbnailFolder = $userRoot->get('thumbnails');
  72. } catch (NotFoundException $e) {
  73. return true;
  74. }
  75. $thumbnails = $thumbnailFolder->getDirectoryListing();
  76. $start = $this->time->getTime();
  77. foreach ($thumbnails as $thumbnail) {
  78. try {
  79. $thumbnail->delete();
  80. } catch (NotPermittedException $e) {
  81. // Ignore
  82. }
  83. if (($this->time->getTime() - $start) > 15) {
  84. return false;
  85. }
  86. }
  87. try {
  88. $thumbnailFolder->delete();
  89. } catch (NotPermittedException $e) {
  90. // Ignore
  91. }
  92. return true;
  93. }
  94. }