|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelFFMpeg\Filesystem; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
|
6
|
|
|
use Illuminate\Filesystem\FilesystemAdapter; |
|
7
|
|
|
use Illuminate\Support\Facades\Storage; |
|
8
|
|
|
use Illuminate\Support\Traits\ForwardsCalls; |
|
9
|
|
|
use League\Flysystem\Adapter\Local; |
|
10
|
|
|
use League\Flysystem\AdapterInterface; |
|
11
|
|
|
use League\Flysystem\Filesystem as LeagueFilesystem; |
|
12
|
|
|
use Spatie\TemporaryDirectory\TemporaryDirectory; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @mixin \Illuminate\Filesystem\FilesystemAdapter |
|
16
|
|
|
*/ |
|
17
|
|
|
class Disk |
|
18
|
|
|
{ |
|
19
|
|
|
use ForwardsCalls; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string|\Illuminate\Contracts\Filesystem\Filesystem |
|
23
|
|
|
*/ |
|
24
|
|
|
private $disk; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Spatie\TemporaryDirectory\TemporaryDirectory |
|
28
|
|
|
*/ |
|
29
|
|
|
private $temporaryDirectory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \Illuminate\Filesystem\FilesystemAdapter |
|
33
|
|
|
*/ |
|
34
|
|
|
private $filesystemAdapter; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct($disk) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->disk = $disk; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Little helper method to instantiate this class. |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function make($disk): self |
|
45
|
|
|
{ |
|
46
|
|
|
if ($disk instanceof self) { |
|
47
|
|
|
return $disk; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return new static($disk); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function makeTemporaryDisk(): self |
|
54
|
|
|
{ |
|
55
|
|
|
$filesystemAdapter = app('filesystem')->createLocalDriver([ |
|
56
|
|
|
'root' => TemporaryDirectories::create()->path(), |
|
57
|
|
|
]); |
|
58
|
|
|
|
|
59
|
|
|
return new static($filesystemAdapter); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Creates a fresh instance, mostly used to force a new TemporaryDirectory. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function clone(): self |
|
66
|
|
|
{ |
|
67
|
|
|
return new Disk($this->disk); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Creates a new TemporaryDirectory instance if none is set, otherwise |
|
72
|
|
|
* it returns the current one. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getTemporaryDirectory(): TemporaryDirectory |
|
75
|
|
|
{ |
|
76
|
|
|
if ($this->temporaryDirectory) { |
|
77
|
|
|
return $this->temporaryDirectory; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this->temporaryDirectory = TemporaryDirectories::create(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function makeMedia(string $path): Media |
|
84
|
|
|
{ |
|
85
|
|
|
return Media::make($this, $path); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns the name of the disk. It generates a name if the disk |
|
90
|
|
|
* is an instance of Flysystem. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getName(): string |
|
93
|
|
|
{ |
|
94
|
|
|
if (is_string($this->disk)) { |
|
95
|
|
|
return $this->disk; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return get_class($this->getFlysystemAdapter()) . "_" . md5(json_encode(serialize($this->getFlysystemAdapter()))); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getFilesystemAdapter(): FilesystemAdapter |
|
102
|
|
|
{ |
|
103
|
|
|
if ($this->filesystemAdapter) { |
|
104
|
|
|
return $this->filesystemAdapter; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if ($this->disk instanceof Filesystem) { |
|
108
|
|
|
return $this->filesystemAdapter = $this->disk; |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this->filesystemAdapter = Storage::disk($this->disk); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function getFlysystemDriver(): LeagueFilesystem |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->getFilesystemAdapter()->getDriver(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
private function getFlysystemAdapter(): AdapterInterface |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->getFlysystemDriver()->getAdapter(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function isLocalDisk(): bool |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->getFlysystemAdapter() instanceof Local; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Forwards all calls to Laravel's FilesystemAdapter which will pass |
|
131
|
|
|
* dynamic methods call onto Flysystem. |
|
132
|
|
|
*/ |
|
133
|
|
|
public function __call($method, $parameters) |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->forwardCallTo($this->getFilesystemAdapter(), $method, $parameters); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.