|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
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
|
|
|
|
|
27
|
|
|
namespace OC\Preview; |
|
28
|
|
|
|
|
29
|
|
|
use OC\BackgroundJob\TimedJob; |
|
30
|
|
|
use OC\Preview\Storage\Root; |
|
31
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
32
|
|
|
use OCP\Files\IMimeTypeLoader; |
|
33
|
|
|
use OCP\Files\NotFoundException; |
|
34
|
|
|
use OCP\Files\NotPermittedException; |
|
35
|
|
|
use OCP\IDBConnection; |
|
36
|
|
|
|
|
37
|
|
|
class BackgroundCleanupJob extends TimedJob { |
|
38
|
|
|
|
|
39
|
|
|
/** @var IDBConnection */ |
|
40
|
|
|
private $connection; |
|
41
|
|
|
|
|
42
|
|
|
/** @var Root */ |
|
43
|
|
|
private $previewFolder; |
|
44
|
|
|
|
|
45
|
|
|
/** @var bool */ |
|
46
|
|
|
private $isCLI; |
|
47
|
|
|
|
|
48
|
|
|
/** @var IMimeTypeLoader */ |
|
49
|
|
|
private $mimeTypeLoader; |
|
50
|
|
|
|
|
51
|
|
|
public function __construct(IDBConnection $connection, |
|
52
|
|
|
Root $previewFolder, |
|
53
|
|
|
IMimeTypeLoader $mimeTypeLoader, |
|
54
|
|
|
bool $isCLI) { |
|
55
|
|
|
// Run at most once an hour |
|
56
|
|
|
$this->setInterval(3600); |
|
57
|
|
|
|
|
58
|
|
|
$this->connection = $connection; |
|
59
|
|
|
$this->previewFolder = $previewFolder; |
|
60
|
|
|
$this->isCLI = $isCLI; |
|
61
|
|
|
$this->mimeTypeLoader = $mimeTypeLoader; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function run($argument) { |
|
65
|
|
|
foreach ($this->getDeletedFiles() as $fileId) { |
|
66
|
|
|
try { |
|
67
|
|
|
$preview = $this->previewFolder->getFolder((string)$fileId); |
|
68
|
|
|
$preview->delete(); |
|
69
|
|
|
} catch (NotFoundException $e) { |
|
70
|
|
|
// continue |
|
71
|
|
|
} catch (NotPermittedException $e) { |
|
72
|
|
|
// continue |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function getDeletedFiles(): \Iterator { |
|
78
|
|
|
yield from $this->getOldPreviewLocations(); |
|
79
|
|
|
yield from $this->getNewPreviewLocations(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function getOldPreviewLocations(): \Iterator { |
|
83
|
|
|
$qb = $this->connection->getQueryBuilder(); |
|
84
|
|
|
$qb->select('a.name') |
|
85
|
|
|
->from('filecache', 'a') |
|
86
|
|
|
->leftJoin('a', 'filecache', 'b', $qb->expr()->eq( |
|
87
|
|
|
$qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid' |
|
88
|
|
|
)) |
|
89
|
|
|
->where( |
|
90
|
|
|
$qb->expr()->isNull('b.fileid') |
|
91
|
|
|
)->andWhere( |
|
92
|
|
|
$qb->expr()->eq('a.parent', $qb->createNamedParameter($this->previewFolder->getId())) |
|
93
|
|
|
)->andWhere( |
|
94
|
|
|
$qb->expr()->like('a.name', $qb->createNamedParameter('__%')) |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
if (!$this->isCLI) { |
|
98
|
|
|
$qb->setMaxResults(10); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$cursor = $qb->execute(); |
|
102
|
|
|
|
|
103
|
|
|
while ($row = $cursor->fetch()) { |
|
104
|
|
|
yield $row['name']; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$cursor->closeCursor(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function getNewPreviewLocations(): \Iterator { |
|
111
|
|
|
$qb = $this->connection->getQueryBuilder(); |
|
112
|
|
|
$qb->select('path', 'mimetype') |
|
113
|
|
|
->from('filecache') |
|
114
|
|
|
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($this->previewFolder->getId()))); |
|
115
|
|
|
$cursor = $qb->execute(); |
|
116
|
|
|
$data = $cursor->fetch(); |
|
117
|
|
|
$cursor->closeCursor(); |
|
118
|
|
|
|
|
119
|
|
|
if ($data === null) { |
|
120
|
|
|
return []; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/* |
|
124
|
|
|
* This lovely like is the result of the way the new previews are stored |
|
125
|
|
|
* We take the md5 of the name (fileid) and split the first 7 chars. That way |
|
126
|
|
|
* there are not a gazillion files in the root of the preview appdata. |
|
127
|
|
|
*/ |
|
128
|
|
|
$like = $this->connection->escapeLikeParameter($data['path']) . '/_/_/_/_/_/_/_/%'; |
|
129
|
|
|
|
|
130
|
|
|
$qb = $this->connection->getQueryBuilder(); |
|
131
|
|
|
$qb->select('a.name') |
|
132
|
|
|
->from('filecache', 'a') |
|
133
|
|
|
->leftJoin('a', 'filecache', 'b', $qb->expr()->eq( |
|
134
|
|
|
$qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid' |
|
135
|
|
|
)) |
|
136
|
|
|
->where( |
|
137
|
|
|
$qb->expr()->andX( |
|
138
|
|
|
$qb->expr()->isNull('b.fileid'), |
|
139
|
|
|
$qb->expr()->like('a.path', $qb->createNamedParameter($like)), |
|
140
|
|
|
$qb->expr()->eq('a.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('httpd/unix-directory'))) |
|
141
|
|
|
) |
|
142
|
|
|
); |
|
143
|
|
|
|
|
144
|
|
|
if (!$this->isCLI) { |
|
145
|
|
|
$qb->setMaxResults(10); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$cursor = $qb->execute(); |
|
149
|
|
|
|
|
150
|
|
|
while ($row = $cursor->fetch()) { |
|
151
|
|
|
yield $row['name']; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$cursor->closeCursor(); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|