UploadCleanup.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\IJobList;
  30. use OCP\BackgroundJob\TimedJob;
  31. use OCP\Files\File;
  32. use OCP\Files\Folder;
  33. use OCP\Files\IRootFolder;
  34. use OCP\Files\NotFoundException;
  35. use Psr\Log\LoggerInterface;
  36. class UploadCleanup extends TimedJob {
  37. private IRootFolder $rootFolder;
  38. private IJobList $jobList;
  39. private LoggerInterface $logger;
  40. public function __construct(ITimeFactory $time, IRootFolder $rootFolder, IJobList $jobList, LoggerInterface $logger) {
  41. parent::__construct($time);
  42. $this->rootFolder = $rootFolder;
  43. $this->jobList = $jobList;
  44. $this->logger = $logger;
  45. // Run once a day
  46. $this->setInterval(60 * 60 * 24);
  47. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  48. }
  49. protected function run($argument) {
  50. $uid = $argument['uid'];
  51. $folder = $argument['folder'];
  52. try {
  53. $userFolder = $this->rootFolder->getUserFolder($uid);
  54. $userRoot = $userFolder->getParent();
  55. /** @var Folder $uploads */
  56. $uploads = $userRoot->get('uploads');
  57. $uploadFolder = $uploads->get($folder);
  58. } catch (NotFoundException | NoUserException $e) {
  59. $this->jobList->remove(self::class, $argument);
  60. return;
  61. }
  62. // Remove if all files have an mtime of more than a day
  63. $time = $this->time->getTime() - 60 * 60 * 24;
  64. if (!($uploadFolder instanceof Folder)) {
  65. $this->logger->error("Found a file inside the uploads folder. Uid: " . $uid . ' folder: ' . $folder);
  66. if ($uploadFolder->getMTime() < $time) {
  67. $uploadFolder->delete();
  68. }
  69. $this->jobList->remove(self::class, $argument);
  70. return;
  71. }
  72. /** @var File[] $files */
  73. $files = $uploadFolder->getDirectoryListing();
  74. // The folder has to be more than a day old
  75. $initial = $uploadFolder->getMTime() < $time;
  76. $expire = array_reduce($files, function (bool $carry, File $file) use ($time) {
  77. return $carry && $file->getMTime() < $time;
  78. }, $initial);
  79. if ($expire) {
  80. $uploadFolder->delete();
  81. $this->jobList->remove(self::class, $argument);
  82. }
  83. }
  84. }