1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Swis\Flysystem\Encrypted; |
6
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Encryption\Encrypter; |
8
|
|
|
use Illuminate\Support\Traits\ForwardsCalls; |
9
|
|
|
use League\Flysystem\AdapterInterface; |
10
|
|
|
use League\Flysystem\Config; |
11
|
|
|
use League\Flysystem\Util\MimeType; |
12
|
|
|
|
13
|
|
|
class EncryptedAdapter implements AdapterInterface |
14
|
|
|
{ |
15
|
|
|
use ForwardsCalls; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \League\Flysystem\AdapterInterface |
19
|
|
|
*/ |
20
|
|
|
private $adapter; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter |
24
|
|
|
*/ |
25
|
|
|
private $encrypter; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \League\Flysystem\AdapterInterface $adapter |
29
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter |
30
|
|
|
*/ |
31
|
218 |
|
public function __construct(AdapterInterface $adapter, Encrypter $encrypter) |
32
|
|
|
{ |
33
|
218 |
|
$this->adapter = $adapter; |
34
|
218 |
|
$this->encrypter = $encrypter; |
35
|
218 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
60 |
|
public function write($path, $contents, Config $config) |
41
|
|
|
{ |
42
|
60 |
|
return $this->adapter->write($path, $this->encrypter->encrypt($contents), $config); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
48
|
30 |
|
public function writeStream($path, $resource, Config $config) |
49
|
|
|
{ |
50
|
30 |
|
return $this->write($path, stream_get_contents($resource), $config); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
60 |
|
public function update($path, $contents, Config $config) |
57
|
|
|
{ |
58
|
60 |
|
return $this->adapter->update($path, $this->encrypter->encrypt($contents), $config); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
30 |
|
public function updateStream($path, $resource, Config $config) |
65
|
|
|
{ |
66
|
30 |
|
return $this->update($path, stream_get_contents($resource), $config); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
72
|
|
|
public function rename($path, $newpath) |
73
|
|
|
{ |
74
|
|
|
return $this->adapter->rename($path, $newpath); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
*/ |
80
|
|
|
public function copy($path, $newpath) |
81
|
|
|
{ |
82
|
|
|
return $this->adapter->copy($path, $newpath); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritDoc |
87
|
|
|
*/ |
88
|
|
|
public function delete($path) |
89
|
|
|
{ |
90
|
|
|
return $this->adapter->delete($path); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritDoc |
95
|
|
|
*/ |
96
|
|
|
public function deleteDir($dirname) |
97
|
|
|
{ |
98
|
|
|
return $this->adapter->deleteDir($dirname); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritDoc |
103
|
|
|
*/ |
104
|
|
|
public function createDir($dirname, Config $config) |
105
|
|
|
{ |
106
|
|
|
return $this->adapter->createDir($dirname, $config); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritDoc |
111
|
|
|
*/ |
112
|
|
|
public function setVisibility($path, $visibility) |
113
|
|
|
{ |
114
|
|
|
return $this->adapter->setVisibility($path, $visibility); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @inheritDoc |
119
|
|
|
*/ |
120
|
|
|
public function has($path) |
121
|
|
|
{ |
122
|
|
|
return $this->adapter->has($path); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @inheritDoc |
127
|
|
|
*/ |
128
|
90 |
|
public function read($path) |
129
|
|
|
{ |
130
|
90 |
|
$result = $this->adapter->read($path); |
131
|
|
|
|
132
|
90 |
|
if ($result === false) { |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
|
136
|
90 |
|
return array_merge($result, ['contents' => $this->encrypter->decrypt($result['contents'])]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @inheritDoc |
141
|
|
|
*/ |
142
|
30 |
|
public function readStream($path) |
143
|
|
|
{ |
144
|
30 |
|
$result = $this->read($path); |
145
|
|
|
|
146
|
30 |
|
if ($result === false) { |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
30 |
|
$stream = tmpfile(); |
151
|
30 |
|
fwrite($stream, $result['contents']); |
|
|
|
|
152
|
30 |
|
fseek($stream, 0); |
|
|
|
|
153
|
30 |
|
unset($result['contents']); |
154
|
|
|
|
155
|
30 |
|
return array_merge($result, compact('stream')); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @inheritDoc |
160
|
|
|
*/ |
161
|
|
|
public function listContents($directory = '', $recursive = false) |
162
|
|
|
{ |
163
|
|
|
return $this->adapter->listContents($directory, $recursive); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @inheritDoc |
168
|
|
|
*/ |
169
|
|
|
public function getMetadata($path) |
170
|
|
|
{ |
171
|
|
|
return $this->adapter->getMetadata($path); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @inheritDoc |
176
|
|
|
*/ |
177
|
30 |
|
public function getSize($path) |
178
|
|
|
{ |
179
|
30 |
|
$result = $this->adapter->getSize($path); |
180
|
30 |
|
$contents = $this->read($path); |
181
|
|
|
|
182
|
30 |
|
if ($result === false || $contents === false) { |
183
|
|
|
return false; |
184
|
|
|
} |
185
|
|
|
|
186
|
30 |
|
$result['size'] = \strlen($contents['contents']); |
187
|
|
|
|
188
|
30 |
|
return $result; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @inheritDoc |
193
|
|
|
*/ |
194
|
8 |
|
public function getMimetype($path) |
195
|
|
|
{ |
196
|
8 |
|
$result = $this->adapter->getMimetype($path); |
197
|
|
|
|
198
|
8 |
|
if ($result === false) { |
199
|
|
|
return false; |
200
|
|
|
} |
201
|
|
|
|
202
|
8 |
|
$result['mimetype'] = MimeType::detectByFilename($path); |
203
|
|
|
|
204
|
8 |
|
return $result; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @inheritDoc |
209
|
|
|
*/ |
210
|
|
|
public function getTimestamp($path) |
211
|
|
|
{ |
212
|
|
|
return $this->adapter->getTimestamp($path); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @inheritDoc |
217
|
|
|
*/ |
218
|
|
|
public function getVisibility($path) |
219
|
|
|
{ |
220
|
|
|
return $this->adapter->getVisibility($path); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Dynamically pass missing methods to the decorated adapter. |
225
|
|
|
* |
226
|
|
|
* @param string $method |
227
|
|
|
* @param array $parameters |
228
|
|
|
* |
229
|
|
|
* @return mixed |
230
|
|
|
*/ |
231
|
120 |
|
public function __call($method, $parameters) |
232
|
|
|
{ |
233
|
120 |
|
return $this->forwardCallTo($this->adapter, $method, $parameters); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|