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