ChunkCleanup.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 OCA\DAV\Migration;
  8. use OCA\DAV\BackgroundJob\UploadCleanup;
  9. use OCP\BackgroundJob\IJobList;
  10. use OCP\Files\Folder;
  11. use OCP\Files\IRootFolder;
  12. use OCP\Files\NotFoundException;
  13. use OCP\IConfig;
  14. use OCP\IUser;
  15. use OCP\IUserManager;
  16. use OCP\Migration\IOutput;
  17. use OCP\Migration\IRepairStep;
  18. class ChunkCleanup implements IRepairStep {
  19. /** @var IConfig */
  20. private $config;
  21. /** @var IUserManager */
  22. private $userManager;
  23. /** @var IRootFolder */
  24. private $rootFolder;
  25. /** @var IJobList */
  26. private $jobList;
  27. public function __construct(IConfig $config,
  28. IUserManager $userManager,
  29. IRootFolder $rootFolder,
  30. IJobList $jobList) {
  31. $this->config = $config;
  32. $this->userManager = $userManager;
  33. $this->rootFolder = $rootFolder;
  34. $this->jobList = $jobList;
  35. }
  36. public function getName(): string {
  37. return 'Chunk cleanup scheduler';
  38. }
  39. public function run(IOutput $output) {
  40. // If we already ran this onec there is no need to run it again
  41. if ($this->config->getAppValue('dav', 'chunks_migrated', '0') === '1') {
  42. $output->info('Cleanup not required');
  43. }
  44. $output->startProgress();
  45. // Loop over all seen users
  46. $this->userManager->callForSeenUsers(function (IUser $user) use ($output): void {
  47. try {
  48. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  49. $userRoot = $userFolder->getParent();
  50. /** @var Folder $uploadFolder */
  51. $uploadFolder = $userRoot->get('uploads');
  52. } catch (NotFoundException $e) {
  53. // No folder so skipping
  54. return;
  55. }
  56. // Insert a cleanup job for each folder we find
  57. $uploads = $uploadFolder->getDirectoryListing();
  58. foreach ($uploads as $upload) {
  59. $this->jobList->add(UploadCleanup::class, ['uid' => $user->getUID(), 'folder' => $upload->getName()]);
  60. }
  61. $output->advance();
  62. });
  63. $output->finishProgress();
  64. $this->config->setAppValue('dav', 'chunks_migrated', '1');
  65. }
  66. }