You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UploadCleanup.php 2.5KB

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