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