ChunkCleanup.php 1.7 KB

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