-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCacheableFlysystemAdapter.php
More file actions
240 lines (220 loc) · 6.12 KB
/
Copy pathCacheableFlysystemAdapter.php
File metadata and controls
240 lines (220 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* @author Hemant Mann <hemant.mann121@gmail.com>
*
* @copyright Copyright (c) 2017, ownCloud GmbH.
* @license GPL-2.0
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
namespace OCA\Files_external_dropbox\Storage;
use Icewind\Streams\IteratorDirectory;
use League\Flysystem\FileNotFoundException;
use OCP\Files\Storage\FlysystemStorageAdapter;
use League\Flysystem\AdapterInterface;
use League\Flysystem\Plugin\GetWithMetadata;
/**
* Generic Cacheable adapter between flysystem adapters and owncloud's storage system
*
* To use: subclass and call $this->buildFlysystem with the flysystem adapter of choice
*/
abstract class CacheableFlysystemAdapter extends FlysystemStorageAdapter {
/**
* This property is used to check whether the storage is case insensitive or not
* @var boolean
*/
protected $isCaseInsensitiveStorage = false;
/**
* Stores the results in cache for the current request to prevent multiple requests to the API
* @var array
*/
protected $cacheContents = [];
/**
* Initialize the storage backend with a flyssytem adapter
* Override parent method so the flysystem include information about storage case sensitivity
*
* @param \League\Flysystem\AdapterInterface $adapter
*/
protected function buildFlySystem(AdapterInterface $adapter) {
$this->flysystem = new Filesystem($adapter, [Filesystem::IS_CASE_INSENSITIVE_STORAGE => $this->isCaseInsensitiveStorage]);
$this->flysystem->addPlugin(new GetWithMetadata());
}
public function clearCache() {
$this->cacheContents = [];
return $this;
}
/**
* Get the location which will be used as a key in cache
* If Storage is not case sensitive then convert the key to lowercase
* @param string $path Path to file/folder
* @return string
*/
public function getCacheLocation($path) {
$location = $this->buildPath($path);
if ($location === '') {
$location = '/';
}
if ($this->isCaseInsensitiveStorage) {
$location = \strtolower($location);
}
return $location;
}
/**
* Check if Cache Contains the data for given path, if not then get the data
* from flysystem and store it in cache for this request lifetime
* @param string $path Path to file/folder
* @return array|boolean
*/
public function getFlysystemMetadata($path, $overRideCache = false) {
$location = $this->getCacheLocation($path);
if (!isset($this->cacheContents[$location]) || $overRideCache) {
try {
$this->cacheContents[$location] = $this->flysystem->getMetadata($location);
} catch (FileNotFoundException $e) {
// do not store this info in cache as it might interfere with Upload process
return false;
}
}
return $this->cacheContents[$location];
}
/**
* Store the list of files/folders in the cache so that subsequent requests in the
* same request life cycle does not call the flysystem API
* @param array $contents Return value of $this->flysystem->listContents
*/
public function updateCache($contents) {
foreach ($contents as $object) {
$path = $object['path'];
$this->cacheContents[$path] = $object;
}
}
/**
* {@inheritdoc}
*/
public function opendir($path) {
try {
$location = $this->buildPath($path);
$content = $this->flysystem->listContents($location);
$this->updateCache($content);
} catch (FileNotFoundException $e) {
return false;
}
$names = \array_map(function ($object) {
return $object['basename'];
}, $content);
return IteratorDirectory::wrap($names);
}
/**
* {@inheritdoc}
*/
public function fopen($path, $mode) {
switch ($mode) {
case 'r':
case 'rb':
return parent::fopen($path, $mode);
default:
unset($this->cacheContents[$this->getCacheLocation($path)]);
return parent::fopen($path, $mode);
}
return false;
}
/**
* {@inheritdoc}
*/
public function filesize($path) {
if ($this->is_dir($path)) {
return 0;
} else {
$info = $this->getFlysystemMetadata($path);
return (int) $info['size'];
}
}
/**
* {@inheritdoc}
*/
public function filemtime($path) {
$info = $this->getFlysystemMetadata($path);
if ($info) { // if $path exists
return $info['timestamp'];
}
return 0;
}
/**
* {@inheritdoc}
*/
public function stat($path) {
$info = $this->getFlysystemMetadata($path);
if ($info) {
return [
'mtime' => $info['timestamp'],
'size' => $info['size']
];
}
return false;
}
/**
* {@inheritdoc}
*/
public function filetype($path) {
if ($path === '' or $path === '/' or $path === '.') {
return 'dir';
}
$info = $this->getFlysystemMetadata($path);
if ($info) {
return $info['type'];
}
return false;
}
/**
* {@inheritdoc}
*/
public function file_exists($path) {
$info = $this->getFlysystemMetadata($path);
return (bool) $info;
}
/**
* Set the cacheContents for the given path to false instead of null
* to prevent request to external storage
*
* Should be used when we know that the querying this path in the
* adapter will return false (i.e path not exists in external storage)
* @param string $path Path to file/folder
*/
protected function removeStatCache($path) {
$location = $this->getCacheLocation($path);
$this->cacheContents[$location] = false;
}
/**
* {@inheritdoc}
*/
public function rmdir($path) {
$success = parent::rmdir($path);
if ($success) {
$this->removeStatCache($path);
}
return $success;
}
/**
* {@inheritdoc}
*/
public function unlink($path) {
$success = parent::unlink($path);
if ($success) {
$this->removeStatCache($path);
}
return $success;
}
}