1
0

UploadCleanup.php 3.0 KB

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