UploadCleanup.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\BackgroundJob;
  27. use OC\User\NoUserException;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\BackgroundJob\IJob;
  30. use OCP\BackgroundJob\IJobList;
  31. use OCP\BackgroundJob\TimedJob;
  32. use OCP\Files\Node;
  33. use OCP\Files\File;
  34. use OCP\Files\Folder;
  35. use OCP\Files\IRootFolder;
  36. use OCP\Files\NotFoundException;
  37. class UploadCleanup extends TimedJob {
  38. private IRootFolder $rootFolder;
  39. private IJobList $jobList;
  40. public function __construct(ITimeFactory $time, IRootFolder $rootFolder, IJobList $jobList) {
  41. parent::__construct($time);
  42. $this->rootFolder = $rootFolder;
  43. $this->jobList = $jobList;
  44. // Run once a day
  45. $this->setInterval(60 * 60 * 24);
  46. $this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
  47. }
  48. protected function run($argument) {
  49. $uid = $argument['uid'];
  50. $folder = $argument['folder'];
  51. try {
  52. $userFolder = $this->rootFolder->getUserFolder($uid);
  53. $userRoot = $userFolder->getParent();
  54. /** @var Folder $uploads */
  55. $uploads = $userRoot->get('uploads');
  56. /** @var Folder $uploadFolder */
  57. $uploadFolder = $uploads->get($folder);
  58. } catch (NotFoundException | NoUserException $e) {
  59. $this->jobList->remove(self::class, $argument);
  60. return;
  61. }
  62. /** @var File[] $files */
  63. $files = $uploadFolder->getDirectoryListing();
  64. // Remove if all files have an mtime of more than a day
  65. $time = $this->time->getTime() - 60 * 60 * 24;
  66. // The folder has to be more than a day old
  67. $initial = $uploadFolder->getMTime() < $time;
  68. $expire = array_reduce($files, function (bool $carry, File $file) use ($time) {
  69. return $carry && $file->getMTime() < $time;
  70. }, $initial);
  71. if ($expire) {
  72. $uploadFolder->delete();
  73. $this->jobList->remove(self::class, $argument);
  74. }
  75. }
  76. }