ChunkCleanup.php 2.7 KB

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