| 1663 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace League\Flysystem\Local;
|
|
|
6 |
|
|
|
7 |
use DirectoryIterator;
|
|
|
8 |
use FilesystemIterator;
|
|
|
9 |
use Generator;
|
|
|
10 |
use League\Flysystem\ChecksumProvider;
|
|
|
11 |
use League\Flysystem\Config;
|
|
|
12 |
use League\Flysystem\DirectoryAttributes;
|
|
|
13 |
use League\Flysystem\FileAttributes;
|
|
|
14 |
use League\Flysystem\FilesystemAdapter;
|
|
|
15 |
use League\Flysystem\PathPrefixer;
|
|
|
16 |
use League\Flysystem\SymbolicLinkEncountered;
|
|
|
17 |
use League\Flysystem\UnableToCopyFile;
|
|
|
18 |
use League\Flysystem\UnableToCreateDirectory;
|
|
|
19 |
use League\Flysystem\UnableToDeleteDirectory;
|
|
|
20 |
use League\Flysystem\UnableToDeleteFile;
|
|
|
21 |
use League\Flysystem\UnableToMoveFile;
|
|
|
22 |
use League\Flysystem\UnableToProvideChecksum;
|
|
|
23 |
use League\Flysystem\UnableToReadFile;
|
|
|
24 |
use League\Flysystem\UnableToRetrieveMetadata;
|
|
|
25 |
use League\Flysystem\UnableToSetVisibility;
|
|
|
26 |
use League\Flysystem\UnableToWriteFile;
|
|
|
27 |
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
|
|
|
28 |
use League\Flysystem\UnixVisibility\VisibilityConverter;
|
|
|
29 |
use League\MimeTypeDetection\FinfoMimeTypeDetector;
|
|
|
30 |
use League\MimeTypeDetection\MimeTypeDetector;
|
|
|
31 |
use RecursiveDirectoryIterator;
|
|
|
32 |
use RecursiveIteratorIterator;
|
|
|
33 |
use SplFileInfo;
|
|
|
34 |
use Throwable;
|
|
|
35 |
use function chmod;
|
|
|
36 |
use function clearstatcache;
|
|
|
37 |
use function dirname;
|
|
|
38 |
use function error_clear_last;
|
|
|
39 |
use function error_get_last;
|
|
|
40 |
use function file_exists;
|
|
|
41 |
use function file_put_contents;
|
|
|
42 |
use function hash_file;
|
|
|
43 |
use function is_dir;
|
|
|
44 |
use function is_file;
|
|
|
45 |
use function mkdir;
|
|
|
46 |
use function rename;
|
|
|
47 |
use const DIRECTORY_SEPARATOR;
|
|
|
48 |
use const LOCK_EX;
|
|
|
49 |
|
|
|
50 |
class LocalFilesystemAdapter implements FilesystemAdapter, ChecksumProvider
|
|
|
51 |
{
|
|
|
52 |
/**
|
|
|
53 |
* @var int
|
|
|
54 |
*/
|
|
|
55 |
public const SKIP_LINKS = 0001;
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* @var int
|
|
|
59 |
*/
|
|
|
60 |
public const DISALLOW_LINKS = 0002;
|
|
|
61 |
|
|
|
62 |
private PathPrefixer $prefixer;
|
|
|
63 |
private VisibilityConverter $visibility;
|
|
|
64 |
private MimeTypeDetector $mimeTypeDetector;
|
|
|
65 |
private string $rootLocation;
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* @var bool
|
|
|
69 |
*/
|
|
|
70 |
private $rootLocationIsSetup = false;
|
|
|
71 |
|
|
|
72 |
public function __construct(
|
|
|
73 |
string $location,
|
|
|
74 |
VisibilityConverter $visibility = null,
|
|
|
75 |
private int $writeFlags = LOCK_EX,
|
|
|
76 |
private int $linkHandling = self::DISALLOW_LINKS,
|
|
|
77 |
MimeTypeDetector $mimeTypeDetector = null,
|
|
|
78 |
bool $lazyRootCreation = false,
|
|
|
79 |
) {
|
|
|
80 |
$this->prefixer = new PathPrefixer($location, DIRECTORY_SEPARATOR);
|
|
|
81 |
$visibility ??= new PortableVisibilityConverter();
|
|
|
82 |
$this->visibility = $visibility;
|
|
|
83 |
$this->rootLocation = $location;
|
|
|
84 |
$this->mimeTypeDetector = $mimeTypeDetector ?: new FallbackMimeTypeDetector(new FinfoMimeTypeDetector());
|
|
|
85 |
|
|
|
86 |
if ( ! $lazyRootCreation) {
|
|
|
87 |
$this->ensureRootDirectoryExists();
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
private function ensureRootDirectoryExists(): void
|
|
|
92 |
{
|
|
|
93 |
if ($this->rootLocationIsSetup) {
|
|
|
94 |
return;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
$this->ensureDirectoryExists($this->rootLocation, $this->visibility->defaultForDirectories());
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
public function write(string $path, string $contents, Config $config): void
|
|
|
101 |
{
|
|
|
102 |
$this->writeToFile($path, $contents, $config);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
public function writeStream(string $path, $contents, Config $config): void
|
|
|
106 |
{
|
|
|
107 |
$this->writeToFile($path, $contents, $config);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* @param resource|string $contents
|
|
|
112 |
*/
|
|
|
113 |
private function writeToFile(string $path, $contents, Config $config): void
|
|
|
114 |
{
|
|
|
115 |
$prefixedLocation = $this->prefixer->prefixPath($path);
|
|
|
116 |
$this->ensureRootDirectoryExists();
|
|
|
117 |
$this->ensureDirectoryExists(
|
|
|
118 |
dirname($prefixedLocation),
|
|
|
119 |
$this->resolveDirectoryVisibility($config->get(Config::OPTION_DIRECTORY_VISIBILITY))
|
|
|
120 |
);
|
|
|
121 |
error_clear_last();
|
|
|
122 |
|
|
|
123 |
if (@file_put_contents($prefixedLocation, $contents, $this->writeFlags) === false) {
|
|
|
124 |
throw UnableToWriteFile::atLocation($path, error_get_last()['message'] ?? '');
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
if ($visibility = $config->get(Config::OPTION_VISIBILITY)) {
|
|
|
128 |
$this->setVisibility($path, (string) $visibility);
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
public function delete(string $path): void
|
|
|
133 |
{
|
|
|
134 |
$location = $this->prefixer->prefixPath($path);
|
|
|
135 |
|
|
|
136 |
if ( ! file_exists($location)) {
|
|
|
137 |
return;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
error_clear_last();
|
|
|
141 |
|
|
|
142 |
if ( ! @unlink($location)) {
|
|
|
143 |
throw UnableToDeleteFile::atLocation($location, error_get_last()['message'] ?? '');
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
public function deleteDirectory(string $prefix): void
|
|
|
148 |
{
|
|
|
149 |
$location = $this->prefixer->prefixPath($prefix);
|
|
|
150 |
|
|
|
151 |
if ( ! is_dir($location)) {
|
|
|
152 |
return;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
$contents = $this->listDirectoryRecursively($location, RecursiveIteratorIterator::CHILD_FIRST);
|
|
|
156 |
|
|
|
157 |
/** @var SplFileInfo $file */
|
|
|
158 |
foreach ($contents as $file) {
|
|
|
159 |
if ( ! $this->deleteFileInfoObject($file)) {
|
|
|
160 |
throw UnableToDeleteDirectory::atLocation($prefix, "Unable to delete file at " . $file->getPathname());
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
unset($contents);
|
|
|
165 |
|
|
|
166 |
if ( ! @rmdir($location)) {
|
|
|
167 |
throw UnableToDeleteDirectory::atLocation($prefix, error_get_last()['message'] ?? '');
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
private function listDirectoryRecursively(
|
|
|
172 |
string $path,
|
|
|
173 |
int $mode = RecursiveIteratorIterator::SELF_FIRST
|
|
|
174 |
): Generator {
|
|
|
175 |
if ( ! is_dir($path)) {
|
|
|
176 |
return;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
yield from new RecursiveIteratorIterator(
|
|
|
180 |
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
|
|
|
181 |
$mode
|
|
|
182 |
);
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
protected function deleteFileInfoObject(SplFileInfo $file): bool
|
|
|
186 |
{
|
|
|
187 |
switch ($file->getType()) {
|
|
|
188 |
case 'dir':
|
|
|
189 |
return @rmdir((string) $file->getRealPath());
|
|
|
190 |
case 'link':
|
|
|
191 |
return @unlink((string) $file->getPathname());
|
|
|
192 |
default:
|
|
|
193 |
return @unlink((string) $file->getRealPath());
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
public function listContents(string $path, bool $deep): iterable
|
|
|
198 |
{
|
|
|
199 |
$location = $this->prefixer->prefixPath($path);
|
|
|
200 |
|
|
|
201 |
if ( ! is_dir($location)) {
|
|
|
202 |
return;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
/** @var SplFileInfo[] $iterator */
|
|
|
206 |
$iterator = $deep ? $this->listDirectoryRecursively($location) : $this->listDirectory($location);
|
|
|
207 |
|
|
|
208 |
foreach ($iterator as $fileInfo) {
|
|
|
209 |
$pathName = $fileInfo->getPathname();
|
|
|
210 |
|
|
|
211 |
try {
|
|
|
212 |
if ($fileInfo->isLink()) {
|
|
|
213 |
if ($this->linkHandling & self::SKIP_LINKS) {
|
|
|
214 |
continue;
|
|
|
215 |
}
|
|
|
216 |
throw SymbolicLinkEncountered::atLocation($pathName);
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
$path = $this->prefixer->stripPrefix($pathName);
|
|
|
220 |
$lastModified = $fileInfo->getMTime();
|
|
|
221 |
$isDirectory = $fileInfo->isDir();
|
|
|
222 |
$permissions = octdec(substr(sprintf('%o', $fileInfo->getPerms()), -4));
|
|
|
223 |
$visibility = $isDirectory ? $this->visibility->inverseForDirectory($permissions) : $this->visibility->inverseForFile($permissions);
|
|
|
224 |
|
|
|
225 |
yield $isDirectory ? new DirectoryAttributes(str_replace('\\', '/', $path), $visibility, $lastModified) : new FileAttributes(
|
|
|
226 |
str_replace('\\', '/', $path),
|
|
|
227 |
$fileInfo->getSize(),
|
|
|
228 |
$visibility,
|
|
|
229 |
$lastModified
|
|
|
230 |
);
|
|
|
231 |
} catch (Throwable $exception) {
|
|
|
232 |
if (file_exists($pathName)) {
|
|
|
233 |
throw $exception;
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
}
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
public function move(string $source, string $destination, Config $config): void
|
|
|
240 |
{
|
|
|
241 |
$sourcePath = $this->prefixer->prefixPath($source);
|
|
|
242 |
$destinationPath = $this->prefixer->prefixPath($destination);
|
|
|
243 |
|
|
|
244 |
$this->ensureRootDirectoryExists();
|
|
|
245 |
$this->ensureDirectoryExists(
|
|
|
246 |
dirname($destinationPath),
|
|
|
247 |
$this->resolveDirectoryVisibility($config->get(Config::OPTION_DIRECTORY_VISIBILITY))
|
|
|
248 |
);
|
|
|
249 |
|
|
|
250 |
if ( ! @rename($sourcePath, $destinationPath)) {
|
|
|
251 |
throw UnableToMoveFile::fromLocationTo($sourcePath, $destinationPath);
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
public function copy(string $source, string $destination, Config $config): void
|
|
|
256 |
{
|
|
|
257 |
$sourcePath = $this->prefixer->prefixPath($source);
|
|
|
258 |
$destinationPath = $this->prefixer->prefixPath($destination);
|
|
|
259 |
$this->ensureRootDirectoryExists();
|
|
|
260 |
$this->ensureDirectoryExists(
|
|
|
261 |
dirname($destinationPath),
|
|
|
262 |
$this->resolveDirectoryVisibility($config->get(Config::OPTION_DIRECTORY_VISIBILITY))
|
|
|
263 |
);
|
|
|
264 |
|
|
|
265 |
if ( ! @copy($sourcePath, $destinationPath)) {
|
|
|
266 |
throw UnableToCopyFile::fromLocationTo($sourcePath, $destinationPath);
|
|
|
267 |
}
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
public function read(string $path): string
|
|
|
271 |
{
|
|
|
272 |
$location = $this->prefixer->prefixPath($path);
|
|
|
273 |
error_clear_last();
|
|
|
274 |
$contents = @file_get_contents($location);
|
|
|
275 |
|
|
|
276 |
if ($contents === false) {
|
|
|
277 |
throw UnableToReadFile::fromLocation($path, error_get_last()['message'] ?? '');
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
return $contents;
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
public function readStream(string $path)
|
|
|
284 |
{
|
|
|
285 |
$location = $this->prefixer->prefixPath($path);
|
|
|
286 |
error_clear_last();
|
|
|
287 |
$contents = @fopen($location, 'rb');
|
|
|
288 |
|
|
|
289 |
if ($contents === false) {
|
|
|
290 |
throw UnableToReadFile::fromLocation($path, error_get_last()['message'] ?? '');
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
return $contents;
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
protected function ensureDirectoryExists(string $dirname, int $visibility): void
|
|
|
297 |
{
|
|
|
298 |
if (is_dir($dirname)) {
|
|
|
299 |
return;
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
error_clear_last();
|
|
|
303 |
|
|
|
304 |
if ( ! @mkdir($dirname, $visibility, true)) {
|
|
|
305 |
$mkdirError = error_get_last();
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
clearstatcache(true, $dirname);
|
|
|
309 |
|
|
|
310 |
if ( ! is_dir($dirname)) {
|
|
|
311 |
$errorMessage = isset($mkdirError['message']) ? $mkdirError['message'] : '';
|
|
|
312 |
|
|
|
313 |
throw UnableToCreateDirectory::atLocation($dirname, $errorMessage);
|
|
|
314 |
}
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
public function fileExists(string $location): bool
|
|
|
318 |
{
|
|
|
319 |
$location = $this->prefixer->prefixPath($location);
|
|
|
320 |
|
|
|
321 |
return is_file($location);
|
|
|
322 |
}
|
|
|
323 |
|
|
|
324 |
public function directoryExists(string $location): bool
|
|
|
325 |
{
|
|
|
326 |
$location = $this->prefixer->prefixPath($location);
|
|
|
327 |
|
|
|
328 |
return is_dir($location);
|
|
|
329 |
}
|
|
|
330 |
|
|
|
331 |
public function createDirectory(string $path, Config $config): void
|
|
|
332 |
{
|
|
|
333 |
$this->ensureRootDirectoryExists();
|
|
|
334 |
$location = $this->prefixer->prefixPath($path);
|
|
|
335 |
$visibility = $config->get(Config::OPTION_VISIBILITY, $config->get(Config::OPTION_DIRECTORY_VISIBILITY));
|
|
|
336 |
$permissions = $this->resolveDirectoryVisibility($visibility);
|
|
|
337 |
|
|
|
338 |
if (is_dir($location)) {
|
|
|
339 |
$this->setPermissions($location, $permissions);
|
|
|
340 |
|
|
|
341 |
return;
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
error_clear_last();
|
|
|
345 |
|
|
|
346 |
if ( ! @mkdir($location, $permissions, true)) {
|
|
|
347 |
throw UnableToCreateDirectory::atLocation($path, error_get_last()['message'] ?? '');
|
|
|
348 |
}
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
public function setVisibility(string $path, string $visibility): void
|
|
|
352 |
{
|
|
|
353 |
$path = $this->prefixer->prefixPath($path);
|
|
|
354 |
$visibility = is_dir($path) ? $this->visibility->forDirectory($visibility) : $this->visibility->forFile(
|
|
|
355 |
$visibility
|
|
|
356 |
);
|
|
|
357 |
|
|
|
358 |
$this->setPermissions($path, $visibility);
|
|
|
359 |
}
|
|
|
360 |
|
|
|
361 |
public function visibility(string $path): FileAttributes
|
|
|
362 |
{
|
|
|
363 |
$location = $this->prefixer->prefixPath($path);
|
|
|
364 |
clearstatcache(false, $location);
|
|
|
365 |
error_clear_last();
|
|
|
366 |
$fileperms = @fileperms($location);
|
|
|
367 |
|
|
|
368 |
if ($fileperms === false) {
|
|
|
369 |
throw UnableToRetrieveMetadata::visibility($path, error_get_last()['message'] ?? '');
|
|
|
370 |
}
|
|
|
371 |
|
|
|
372 |
$permissions = $fileperms & 0777;
|
|
|
373 |
$visibility = $this->visibility->inverseForFile($permissions);
|
|
|
374 |
|
|
|
375 |
return new FileAttributes($path, null, $visibility);
|
|
|
376 |
}
|
|
|
377 |
|
|
|
378 |
private function resolveDirectoryVisibility(?string $visibility): int
|
|
|
379 |
{
|
|
|
380 |
return $visibility === null ? $this->visibility->defaultForDirectories() : $this->visibility->forDirectory(
|
|
|
381 |
$visibility
|
|
|
382 |
);
|
|
|
383 |
}
|
|
|
384 |
|
|
|
385 |
public function mimeType(string $path): FileAttributes
|
|
|
386 |
{
|
|
|
387 |
$location = $this->prefixer->prefixPath($path);
|
|
|
388 |
error_clear_last();
|
|
|
389 |
|
|
|
390 |
if ( ! is_file($location)) {
|
|
|
391 |
throw UnableToRetrieveMetadata::mimeType($location, 'No such file exists.');
|
|
|
392 |
}
|
|
|
393 |
|
|
|
394 |
$mimeType = $this->mimeTypeDetector->detectMimeTypeFromFile($location);
|
|
|
395 |
|
|
|
396 |
if ($mimeType === null) {
|
|
|
397 |
throw UnableToRetrieveMetadata::mimeType($path, error_get_last()['message'] ?? '');
|
|
|
398 |
}
|
|
|
399 |
|
|
|
400 |
return new FileAttributes($path, null, null, null, $mimeType);
|
|
|
401 |
}
|
|
|
402 |
|
|
|
403 |
public function lastModified(string $path): FileAttributes
|
|
|
404 |
{
|
|
|
405 |
$location = $this->prefixer->prefixPath($path);
|
|
|
406 |
error_clear_last();
|
|
|
407 |
$lastModified = @filemtime($location);
|
|
|
408 |
|
|
|
409 |
if ($lastModified === false) {
|
|
|
410 |
throw UnableToRetrieveMetadata::lastModified($path, error_get_last()['message'] ?? '');
|
|
|
411 |
}
|
|
|
412 |
|
|
|
413 |
return new FileAttributes($path, null, null, $lastModified);
|
|
|
414 |
}
|
|
|
415 |
|
|
|
416 |
public function fileSize(string $path): FileAttributes
|
|
|
417 |
{
|
|
|
418 |
$location = $this->prefixer->prefixPath($path);
|
|
|
419 |
error_clear_last();
|
|
|
420 |
|
|
|
421 |
if (is_file($location) && ($fileSize = @filesize($location)) !== false) {
|
|
|
422 |
return new FileAttributes($path, $fileSize);
|
|
|
423 |
}
|
|
|
424 |
|
|
|
425 |
throw UnableToRetrieveMetadata::fileSize($path, error_get_last()['message'] ?? '');
|
|
|
426 |
}
|
|
|
427 |
|
|
|
428 |
public function checksum(string $path, Config $config): string
|
|
|
429 |
{
|
|
|
430 |
$algo = $config->get('checksum_algo', 'md5');
|
|
|
431 |
$location = $this->prefixer->prefixPath($path);
|
|
|
432 |
error_clear_last();
|
|
|
433 |
$checksum = @hash_file($algo, $location);
|
|
|
434 |
|
|
|
435 |
if ($checksum === false) {
|
|
|
436 |
throw new UnableToProvideChecksum(error_get_last()['message'] ?? '', $path);
|
|
|
437 |
}
|
|
|
438 |
|
|
|
439 |
return $checksum;
|
|
|
440 |
}
|
|
|
441 |
|
|
|
442 |
private function listDirectory(string $location): Generator
|
|
|
443 |
{
|
|
|
444 |
$iterator = new DirectoryIterator($location);
|
|
|
445 |
|
|
|
446 |
foreach ($iterator as $item) {
|
|
|
447 |
if ($item->isDot()) {
|
|
|
448 |
continue;
|
|
|
449 |
}
|
|
|
450 |
|
|
|
451 |
yield $item;
|
|
|
452 |
}
|
|
|
453 |
}
|
|
|
454 |
|
|
|
455 |
private function setPermissions(string $location, int $visibility): void
|
|
|
456 |
{
|
|
|
457 |
error_clear_last();
|
|
|
458 |
if ( ! @chmod($location, $visibility)) {
|
|
|
459 |
$extraMessage = error_get_last()['message'] ?? '';
|
|
|
460 |
throw UnableToSetVisibility::atLocation($this->prefixer->stripPrefix($location), $extraMessage);
|
|
|
461 |
}
|
|
|
462 |
}
|
|
|
463 |
}
|