|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, Roeland Jago Douma <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
namespace OC\Preview; |
|
24
|
|
|
|
|
25
|
|
|
use OCP\Files\File; |
|
26
|
|
|
use OCP\Files\Node; |
|
27
|
|
|
use OCP\Files\Folder; |
|
28
|
|
|
use OCP\Files\IAppData; |
|
29
|
|
|
use OCP\Files\NotFoundException; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Class Watcher |
|
33
|
|
|
* |
|
34
|
|
|
* @package OC\Preview |
|
35
|
|
|
* |
|
36
|
|
|
* Class that will watch filesystem activity and remove previews as needed. |
|
37
|
|
|
*/ |
|
38
|
|
|
class Watcher { |
|
39
|
|
|
/** @var IAppData */ |
|
40
|
|
|
private $appData; |
|
41
|
|
|
|
|
42
|
|
|
/** @var int[] */ |
|
43
|
|
|
private $toDelete = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Watcher constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param IAppData $appData |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(IAppData $appData) { |
|
51
|
|
|
$this->appData = $appData; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
View Code Duplication |
public function postWrite(Node $node) { |
|
|
|
|
|
|
55
|
|
|
// We only handle files |
|
56
|
|
|
if ($node instanceof Folder) { |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
try { |
|
61
|
|
|
$folder = $this->appData->getFolder($node->getId()); |
|
62
|
|
|
$folder->delete(); |
|
63
|
|
|
} catch (NotFoundException $e) { |
|
64
|
|
|
//Nothing to do |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function preDelete(Node $node) { |
|
69
|
|
|
// To avoid cycles |
|
70
|
|
|
if ($this->toDelete !== []) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($node instanceof File) { |
|
75
|
|
|
$this->toDelete[] = $node->getId(); |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** @var Folder $node */ |
|
80
|
|
|
$nodes = $node->search(''); |
|
81
|
|
|
foreach ($nodes as $node) { |
|
82
|
|
|
if ($node instanceof File) { |
|
83
|
|
|
$this->toDelete[] = $node->getId(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
View Code Duplication |
public function postDelete(Node $node) { |
|
|
|
|
|
|
89
|
|
|
foreach ($this->toDelete as $fid) { |
|
90
|
|
|
try { |
|
91
|
|
|
$folder = $this->appData->getFolder($fid); |
|
92
|
|
|
$folder->delete(); |
|
93
|
|
|
} catch (NotFoundException $e) { |
|
94
|
|
|
// continue |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.