1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelFFMpeg\Filesystem; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\FilesystemAdapter; |
6
|
|
|
|
7
|
|
|
class Media |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var \ProtoneMedia\LaravelFFMpeg\Filesystem\Disk |
11
|
|
|
*/ |
12
|
|
|
private $disk; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $path; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Spatie\TemporaryDirectory\TemporaryDirectory |
21
|
|
|
*/ |
22
|
|
|
private $temporaryDirectory; |
23
|
|
|
|
24
|
|
|
public function __construct(Disk $disk, string $path) |
25
|
|
|
{ |
26
|
|
|
$this->disk = $disk; |
27
|
|
|
$this->path = $path; |
28
|
|
|
|
29
|
|
|
$this->makeDirectory(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function make($disk, string $path): self |
33
|
|
|
{ |
34
|
|
|
return new static(Disk::make($disk), $path); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getDisk(): Disk |
38
|
|
|
{ |
39
|
|
|
return $this->disk; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getPath(): string |
43
|
|
|
{ |
44
|
|
|
return $this->path; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getDirectory(): ?string |
48
|
|
|
{ |
49
|
|
|
$directory = rtrim(pathinfo($this->getPath())['dirname'], DIRECTORY_SEPARATOR); |
50
|
|
|
|
51
|
|
|
if ($directory === '.') { |
52
|
|
|
$directory = ''; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($directory) { |
56
|
|
|
$directory .= DIRECTORY_SEPARATOR; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $directory; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function makeDirectory(): void |
63
|
|
|
{ |
64
|
|
|
$directory = $this->getDirectory(); |
65
|
|
|
|
66
|
|
|
$adapter = $this->getDisk()->isLocalDisk() |
67
|
|
|
? $this->getDisk()->getFilesystemAdapter() |
68
|
|
|
: $this->temporaryDirectoryAdapter(); |
69
|
|
|
|
70
|
|
|
if ($adapter->has($directory)) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$adapter->makeDirectory($directory); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getFilenameWithoutExtension(): string |
78
|
|
|
{ |
79
|
|
|
return pathinfo($this->getPath())['filename']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getFilename(): string |
83
|
|
|
{ |
84
|
|
|
return pathinfo($this->getPath())['basename']; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function temporaryDirectoryAdapter(): FilesystemAdapter |
88
|
|
|
{ |
89
|
|
|
if (!$this->temporaryDirectory) { |
90
|
|
|
$this->temporaryDirectory = $this->getDisk()->getTemporaryDirectory(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return app('filesystem')->createLocalDriver( |
94
|
|
|
['root' => $this->temporaryDirectory->path()] |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getLocalPath(): string |
99
|
|
|
{ |
100
|
|
|
$disk = $this->getDisk(); |
101
|
|
|
$path = $this->getPath(); |
102
|
|
|
|
103
|
|
|
if ($disk->isLocalDisk()) { |
104
|
|
|
return $disk->path($path); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$temporaryDirectoryAdapter = $this->temporaryDirectoryAdapter(); |
108
|
|
|
|
109
|
|
|
if ($disk->exists($path) && !$temporaryDirectoryAdapter->exists($path)) { |
|
|
|
|
110
|
|
|
$temporaryDirectoryAdapter->writeStream($path, $disk->readStream($path)); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $temporaryDirectoryAdapter->path($path); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function copyAllFromTemporaryDirectory(string $visibility = null) |
117
|
|
|
{ |
118
|
|
|
if (!$this->temporaryDirectory) { |
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$temporaryDirectoryAdapter = $this->temporaryDirectoryAdapter(); |
123
|
|
|
|
124
|
|
|
$destinationAdapater = $this->getDisk()->getFilesystemAdapter(); |
125
|
|
|
|
126
|
|
|
foreach ($temporaryDirectoryAdapter->allFiles() as $path) { |
127
|
|
|
$destinationAdapater->writeStream($path, $temporaryDirectoryAdapter->readStream($path)); |
128
|
|
|
|
129
|
|
|
if ($visibility) { |
130
|
|
|
$destinationAdapater->setVisibility($path, $visibility); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function setVisibility(string $visibility = null) |
138
|
|
|
{ |
139
|
|
|
$disk = $this->getDisk(); |
140
|
|
|
|
141
|
|
|
if ($visibility && $disk->isLocalDisk()) { |
142
|
|
|
$disk->setVisibility($visibility); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|