CleanPreviewsBackgroundJob.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  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
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Repair\NC11;
  24. use OC\BackgroundJob\QueuedJob;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\BackgroundJob\IJobList;
  27. use OCP\Files\Folder;
  28. use OCP\Files\IRootFolder;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\NotPermittedException;
  31. use OCP\ILogger;
  32. class CleanPreviewsBackgroundJob extends QueuedJob {
  33. /** @var IRootFolder */
  34. private $rootFolder;
  35. /** @var ILogger */
  36. private $logger;
  37. /** @var IJobList */
  38. private $jobList;
  39. /** @var ITimeFactory */
  40. private $timeFactory;
  41. /**
  42. * CleanPreviewsBackgroundJob constructor.
  43. *
  44. * @param IRootFolder $rootFolder
  45. * @param ILogger $logger
  46. * @param IJobList $jobList
  47. * @param ITimeFactory $timeFactory
  48. */
  49. public function __construct(IRootFolder $rootFolder,
  50. ILogger $logger,
  51. IJobList $jobList,
  52. ITimeFactory $timeFactory) {
  53. $this->rootFolder = $rootFolder;
  54. $this->logger = $logger;
  55. $this->jobList = $jobList;
  56. $this->timeFactory = $timeFactory;
  57. }
  58. public function run($arguments) {
  59. $uid = $arguments['uid'];
  60. $this->logger->info('Started preview cleanup for ' . $uid);
  61. $empty = $this->cleanupPreviews($uid);
  62. if (!$empty) {
  63. $this->jobList->add(self::class, ['uid' => $uid]);
  64. $this->logger->info('New preview cleanup scheduled for ' . $uid);
  65. } else {
  66. $this->logger->info('Preview cleanup done for ' . $uid);
  67. }
  68. }
  69. /**
  70. * @param $uid
  71. * @return bool
  72. */
  73. private function cleanupPreviews($uid) {
  74. try {
  75. $userFolder = $this->rootFolder->getUserFolder($uid);
  76. } catch (NotFoundException $e) {
  77. return true;
  78. }
  79. $userRoot = $userFolder->getParent();
  80. try {
  81. /** @var Folder $thumbnailFolder */
  82. $thumbnailFolder = $userRoot->get('thumbnails');
  83. } catch (NotFoundException $e) {
  84. return true;
  85. }
  86. $thumbnails = $thumbnailFolder->getDirectoryListing();
  87. $start = $this->timeFactory->getTime();
  88. foreach ($thumbnails as $thumbnail) {
  89. try {
  90. $thumbnail->delete();
  91. } catch (NotPermittedException $e) {
  92. // Ignore
  93. }
  94. if (($this->timeFactory->getTime() - $start) > 15) {
  95. return false;
  96. }
  97. }
  98. try {
  99. $thumbnailFolder->delete();
  100. } catch (NotPermittedException $e) {
  101. // Ignore
  102. }
  103. return true;
  104. }
  105. }