1
0

UploadCleanup.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. class UploadCleanup extends TimedJob {
  36. /** @var IRootFolder */
  37. private $rootFolder;
  38. /** @var IJobList */
  39. private $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. }
  47. protected function run($argument) {
  48. $uid = $argument['uid'];
  49. $folder = $argument['folder'];
  50. try {
  51. $userFolder = $this->rootFolder->getUserFolder($uid);
  52. $userRoot = $userFolder->getParent();
  53. /** @var Folder $uploads */
  54. $uploads = $userRoot->get('uploads');
  55. /** @var Folder $uploadFolder */
  56. $uploadFolder = $uploads->get($folder);
  57. } catch (NotFoundException|NoUserException $e) {
  58. $this->jobList->remove(self::class, $argument);
  59. return;
  60. }
  61. $files = $uploadFolder->getDirectoryListing();
  62. // Remove if all files have an mtime of more than a day
  63. $time = $this->time->getTime() - 60 * 60 * 24;
  64. // The folder has to be more than a day old
  65. $initial = $uploadFolder->getMTime() < $time;
  66. $expire = array_reduce($files, function (bool $carry, File $file) use ($time) {
  67. return $carry && $file->getMTime() < $time;
  68. }, $initial);
  69. if ($expire) {
  70. $uploadFolder->delete();
  71. $this->jobList->remove(self::class, $argument);
  72. }
  73. }
  74. }