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