1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Storage\Bucket; |
6
|
|
|
|
7
|
|
|
use JetBrains\PhpStorm\ExpectedValues; |
8
|
|
|
use League\Flysystem\FilesystemException; |
9
|
|
|
use League\Flysystem\FilesystemOperator; |
10
|
|
|
use Spiral\Storage\Exception\FileOperationException; |
11
|
|
|
use Spiral\Storage\Visibility; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @mixin ReadableInterface |
15
|
|
|
*/ |
16
|
|
|
trait ReadableTrait |
17
|
|
|
{ |
18
|
|
|
public function exists(string $pathname): bool |
19
|
|
|
{ |
20
|
|
|
$fs = $this->getOperator(); |
21
|
|
|
|
22
|
|
|
try { |
23
|
|
|
return $fs->fileExists($pathname); |
24
|
|
|
} catch (FilesystemException $e) { |
25
|
|
|
throw new FileOperationException($e->getMessage(), $e->getCode(), $e); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getContents(string $pathname): string |
30
|
|
|
{ |
31
|
|
|
$fs = $this->getOperator(); |
32
|
|
|
|
33
|
|
|
try { |
34
|
|
|
return $fs->read($pathname); |
35
|
|
|
} catch (FilesystemException $e) { |
36
|
|
|
throw new FileOperationException($e->getMessage(), $e->getCode(), $e); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getStream(string $pathname) |
41
|
|
|
{ |
42
|
|
|
$fs = $this->getOperator(); |
43
|
|
|
|
44
|
|
|
try { |
45
|
|
|
return $fs->readStream($pathname); |
46
|
|
|
} catch (FilesystemException $e) { |
47
|
|
|
throw new FileOperationException($e->getMessage(), $e->getCode(), $e); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return positive-int |
|
|
|
|
53
|
|
|
*/ |
54
|
|
|
public function getLastModified(string $pathname): int |
55
|
|
|
{ |
56
|
|
|
$fs = $this->getOperator(); |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
return $fs->lastModified($pathname); |
60
|
|
|
} catch (FilesystemException $e) { |
61
|
|
|
throw new FileOperationException($e->getMessage(), $e->getCode(), $e); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return int<0, max> |
67
|
|
|
*/ |
68
|
|
|
public function getSize(string $pathname): int |
69
|
|
|
{ |
70
|
|
|
$fs = $this->getOperator(); |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
return $fs->fileSize($pathname); |
74
|
|
|
} catch (FilesystemException $e) { |
75
|
|
|
throw new FileOperationException($e->getMessage(), $e->getCode(), $e); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getMimeType(string $pathname): string |
80
|
|
|
{ |
81
|
|
|
$fs = $this->getOperator(); |
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
return $fs->mimeType($pathname); |
85
|
|
|
} catch (FilesystemException $e) { |
86
|
|
|
throw new FileOperationException($e->getMessage(), $e->getCode(), $e); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Visibility::VISIBILITY_* |
92
|
|
|
*/ |
93
|
|
|
#[ExpectedValues(valuesFromClass: Visibility::class)] |
94
|
|
|
public function getVisibility(string $pathname): string |
95
|
|
|
{ |
96
|
|
|
$fs = $this->getOperator(); |
97
|
|
|
|
98
|
|
|
try { |
99
|
|
|
return $this->fromFlysystemVisibility( |
100
|
|
|
$fs->visibility($pathname), |
101
|
|
|
); |
102
|
|
|
} catch (FilesystemException $e) { |
103
|
|
|
throw new FileOperationException($e->getMessage(), $e->getCode(), $e); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
abstract protected function getOperator(): FilesystemOperator; |
108
|
|
|
|
109
|
|
|
#[ExpectedValues(valuesFromClass: Visibility::class)] |
110
|
|
|
private function fromFlysystemVisibility( |
111
|
|
|
#[ExpectedValues(valuesFromClass: \League\Flysystem\Visibility::class)] |
112
|
|
|
string $visibility, |
113
|
|
|
): string { |
114
|
|
|
return $visibility === \League\Flysystem\Visibility::PUBLIC |
115
|
|
|
? Visibility::VISIBILITY_PUBLIC |
116
|
|
|
: Visibility::VISIBILITY_PRIVATE; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|