|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright 2016 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 OC\Files\AppData; |
|
26
|
|
|
|
|
27
|
|
|
use OC\Cache\CappedMemoryCache; |
|
28
|
|
|
use OC\Files\SimpleFS\SimpleFolder; |
|
29
|
|
|
use OCP\Files\IAppData; |
|
30
|
|
|
use OCP\Files\IRootFolder; |
|
31
|
|
|
use OCP\Files\Folder; |
|
32
|
|
|
use OC\SystemConfig; |
|
33
|
|
|
use OCP\Files\Node; |
|
34
|
|
|
use OCP\Files\NotFoundException; |
|
35
|
|
|
use OCP\Files\NotPermittedException; |
|
36
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder; |
|
37
|
|
|
|
|
38
|
|
|
class AppData implements IAppData { |
|
39
|
|
|
|
|
40
|
|
|
/** @var IRootFolder */ |
|
41
|
|
|
private $rootFolder; |
|
42
|
|
|
|
|
43
|
|
|
/** @var SystemConfig */ |
|
44
|
|
|
private $config; |
|
45
|
|
|
|
|
46
|
|
|
/** @var string */ |
|
47
|
|
|
private $appId; |
|
48
|
|
|
|
|
49
|
|
|
/** @var Folder */ |
|
50
|
|
|
private $folder; |
|
51
|
|
|
|
|
52
|
|
|
/** @var (ISimpleFolder|NotFoundException)[]|CappedMemoryCache */ |
|
53
|
|
|
private $folders; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* AppData constructor. |
|
57
|
|
|
* |
|
58
|
|
|
* @param IRootFolder $rootFolder |
|
59
|
|
|
* @param SystemConfig $systemConfig |
|
60
|
|
|
* @param string $appId |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct(IRootFolder $rootFolder, |
|
63
|
|
|
SystemConfig $systemConfig, |
|
64
|
|
|
string $appId) { |
|
65
|
|
|
|
|
66
|
|
|
$this->rootFolder = $rootFolder; |
|
67
|
|
|
$this->config = $systemConfig; |
|
68
|
|
|
$this->appId = $appId; |
|
69
|
|
|
$this->folders = new CappedMemoryCache(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function getAppDataFolderName() { |
|
73
|
|
|
$instanceId = $this->config->getValue('instanceid', null); |
|
74
|
|
|
if ($instanceId === null) { |
|
75
|
|
|
throw new \RuntimeException('no instance id!'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return 'appdata_' . $instanceId; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function getAppDataRootFolder(): Folder { |
|
82
|
|
|
$name = $this->getAppDataFolderName(); |
|
83
|
|
|
|
|
84
|
|
|
try { |
|
85
|
|
|
/** @var Folder $node */ |
|
86
|
|
|
$node = $this->rootFolder->get($name); |
|
87
|
|
|
return $node; |
|
88
|
|
|
} catch (NotFoundException $e) { |
|
89
|
|
|
try { |
|
90
|
|
|
return $this->rootFolder->newFolder($name); |
|
91
|
|
|
} catch (NotPermittedException $e) { |
|
92
|
|
|
throw new \RuntimeException('Could not get appdata folder'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return Folder |
|
99
|
|
|
* @throws \RuntimeException |
|
100
|
|
|
*/ |
|
101
|
|
|
private function getAppDataFolder(): Folder { |
|
102
|
|
|
if ($this->folder === null) { |
|
103
|
|
|
$name = $this->getAppDataFolderName(); |
|
104
|
|
|
|
|
105
|
|
|
try { |
|
106
|
|
|
$this->folder = $this->rootFolder->get($name . '/' . $this->appId); |
|
107
|
|
|
} catch (NotFoundException $e) { |
|
108
|
|
|
$appDataRootFolder = $this->getAppDataRootFolder(); |
|
109
|
|
|
|
|
110
|
|
|
try { |
|
111
|
|
|
$this->folder = $appDataRootFolder->get($this->appId); |
|
112
|
|
|
} catch (NotFoundException $e) { |
|
113
|
|
|
try { |
|
114
|
|
|
$this->folder = $appDataRootFolder->newFolder($this->appId); |
|
115
|
|
|
} catch (NotPermittedException $e) { |
|
116
|
|
|
throw new \RuntimeException('Could not get appdata folder for ' . $this->appId); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $this->folder; |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getFolder(string $name): ISimpleFolder { |
|
126
|
|
|
$key = $this->appId . '/' . $name; |
|
127
|
|
|
if ($cachedFolder = $this->folders->get($key)) { |
|
128
|
|
|
if ($cachedFolder instanceof \Exception) { |
|
129
|
|
|
throw $cachedFolder; |
|
130
|
|
|
} else { |
|
131
|
|
|
return $cachedFolder; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
try { |
|
135
|
|
|
$path = $this->getAppDataFolderName() . '/' . $this->appId . '/' . $name; |
|
136
|
|
|
$node = $this->rootFolder->get($path); |
|
137
|
|
|
} catch (NotFoundException $e) { |
|
138
|
|
|
$this->folders->set($key, $e); |
|
139
|
|
|
throw $e; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** @var Folder $node */ |
|
143
|
|
|
$folder = new SimpleFolder($node); |
|
144
|
|
|
$this->folders->set($key, $folder); |
|
145
|
|
|
return $folder; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function newFolder(string $name): ISimpleFolder { |
|
149
|
|
|
$key = $this->appId . '/' . $name; |
|
150
|
|
|
$folder = $this->getAppDataFolder()->newFolder($name); |
|
151
|
|
|
|
|
152
|
|
|
$simpleFolder = new SimpleFolder($folder); |
|
153
|
|
|
$this->folders->set($key, $simpleFolder); |
|
154
|
|
|
return $simpleFolder; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function getDirectoryListing(): array { |
|
158
|
|
|
$listing = $this->getAppDataFolder()->getDirectoryListing(); |
|
159
|
|
|
|
|
160
|
|
|
$fileListing = array_map(function (Node $folder) { |
|
161
|
|
|
if ($folder instanceof Folder) { |
|
162
|
|
|
return new SimpleFolder($folder); |
|
163
|
|
|
} |
|
164
|
|
|
return null; |
|
165
|
|
|
}, $listing); |
|
166
|
|
|
|
|
167
|
|
|
$fileListing = array_filter($fileListing); |
|
168
|
|
|
|
|
169
|
|
|
return array_values($fileListing); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function getId(): int { |
|
173
|
|
|
return $this->getAppDataFolder()->getId(); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|