UploadCleanup.php 3.0 KB

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