1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mostafaznv\Larupload\Concerns\Storage\Attachment; |
4
|
|
|
|
5
|
|
|
use FFMpeg\Format\Audio\Aac; |
6
|
|
|
use FFMpeg\Format\Audio\Flac; |
7
|
|
|
use FFMpeg\Format\Audio\Mp3; |
8
|
|
|
use FFMpeg\Format\Audio\Wav; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
use Illuminate\Support\Facades\Storage; |
11
|
|
|
use Mostafaznv\Larupload\DTOs\Style\AudioStyle; |
12
|
|
|
use Mostafaznv\Larupload\Enums\LaruploadFileType; |
13
|
|
|
use Mostafaznv\Larupload\Larupload; |
14
|
|
|
use Mostafaznv\Larupload\Actions\FixExceptionNamesAction; |
15
|
|
|
|
16
|
|
|
trait StyleAttachment |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Handle styles |
20
|
|
|
* resize, crop and generate styles from original file |
21
|
|
|
* |
22
|
|
|
* @param string $id |
23
|
|
|
* @param Model|string $class |
24
|
|
|
* @param bool $standalone |
25
|
|
|
*/ |
26
|
|
|
protected function handleStyles(string $id, Model|string $model, bool $standalone = false): void |
27
|
|
|
{ |
28
|
|
|
switch ($this->type) { |
29
|
|
|
case LaruploadFileType::IMAGE: |
30
|
|
|
foreach ($this->imageStyles as $name => $style) { |
31
|
|
|
$path = $this->getBasePath($id, $name); |
|
|
|
|
32
|
|
|
$saveTo = $path . '/' . FixExceptionNamesAction::make($this->output['name'], $name)->run(); |
33
|
|
|
|
34
|
|
|
Storage::disk($this->disk)->makeDirectory($path); |
35
|
|
|
$this->img($this->file)->resize($saveTo, $style); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
break; |
39
|
|
|
|
40
|
|
|
case LaruploadFileType::VIDEO: |
41
|
|
|
if ($this->ffmpegQueue) { |
42
|
|
|
if ($this->driverIsNotLocal()) { |
|
|
|
|
43
|
|
|
$this->uploadOriginalFile($id, $this->localDisk); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($model instanceof Model) { |
47
|
|
|
$this->initializeFFMpegQueue( |
|
|
|
|
48
|
|
|
$model->id, $model->getMorphClass(), $standalone |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
else { |
52
|
|
|
$this->initializeFFMpegQueue( |
53
|
|
|
$id, $model, $standalone |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
else { |
58
|
|
|
$this->handleVideoStyles($id); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
break; |
62
|
|
|
|
63
|
|
|
case LaruploadFileType::AUDIO: |
64
|
|
|
if ($this->ffmpegQueue) { |
65
|
|
|
if ($this->driverIsNotLocal()) { |
66
|
|
|
$this->uploadOriginalFile($id, $this->localDisk); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($model instanceof Model) { |
70
|
|
|
$this->initializeFFMpegQueue( |
71
|
|
|
$model->id, $model->getMorphClass(), $standalone |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
else { |
75
|
|
|
$this->initializeFFMpegQueue( |
76
|
|
|
$id, $model, $standalone |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
else { |
81
|
|
|
$this->handleAudioStyles($id); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
break; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Handle styles for videos |
90
|
|
|
* |
91
|
|
|
* @param $id |
92
|
|
|
*/ |
93
|
|
|
protected function handleVideoStyles($id): void |
94
|
|
|
{ |
95
|
|
|
foreach ($this->videoStyles as $name => $style) { |
96
|
|
|
$path = $this->getBasePath($id, $name); |
97
|
|
|
Storage::disk($this->disk)->makeDirectory($path); |
98
|
|
|
|
99
|
|
|
$saveTo = "$path/{$this->output['name']}"; |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
if ($style->isAudioFormat()) { |
103
|
|
|
$this->ffmpeg(null, $style)->audio( |
|
|
|
|
104
|
|
|
style: AudioStyle::make($style->name, $style->format), |
105
|
|
|
saveTo: $saveTo |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->ffmpeg(null, $style)->manipulate($style, $saveTo); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (count($this->streams)) { |
115
|
|
|
$fileName = pathinfo($this->output['name'], PATHINFO_FILENAME) . '.m3u8'; |
|
|
|
|
116
|
|
|
|
117
|
|
|
$path = $this->getBasePath($id, Larupload::STREAM_FOLDER); |
118
|
|
|
Storage::disk($this->disk)->makeDirectory($path); |
119
|
|
|
|
120
|
|
|
$this->ffmpeg()->stream($this->streams, $path, $fileName); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Handle styles for audios |
126
|
|
|
* |
127
|
|
|
* @param $id |
128
|
|
|
*/ |
129
|
|
|
protected function handleAudioStyles($id): void |
130
|
|
|
{ |
131
|
|
|
foreach ($this->audioStyles as $name => $style) { |
132
|
|
|
$path = $this->getBasePath($id, $name); |
133
|
|
|
Storage::disk($this->disk)->makeDirectory($path); |
134
|
|
|
$saveTo = "$path/{$this->output['name']}"; |
135
|
|
|
|
136
|
|
|
$this->ffmpeg()->audio($style, $saveTo); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Prepare style path |
142
|
|
|
* this function will use to prepare full path of given style to generate url/download response |
143
|
|
|
* |
144
|
|
|
* @param string $style |
145
|
|
|
* @return string|null |
146
|
|
|
*/ |
147
|
|
|
protected function prepareStylePath(string $style): ?string |
148
|
|
|
{ |
149
|
|
|
$staticStyles = [ |
150
|
|
|
Larupload::ORIGINAL_FOLDER, |
151
|
|
|
Larupload::COVER_FOLDER, |
152
|
|
|
Larupload::STREAM_FOLDER |
153
|
|
|
]; |
154
|
|
|
|
155
|
|
|
if (isset($this->id) and (in_array($style, $staticStyles) or array_key_exists($style, $this->imageStyles) or array_key_exists($style, $this->videoStyles) or array_key_exists($style, $this->audioStyles))) { |
156
|
|
|
$name = $style == Larupload::COVER_FOLDER |
157
|
|
|
? $this->output['cover'] |
158
|
|
|
: $this->output['name']; |
159
|
|
|
|
160
|
|
|
$type = $this->output['type'] |
161
|
|
|
? LaruploadFileType::from($this->output['type']) |
162
|
|
|
: null; |
163
|
|
|
|
164
|
|
|
if ($name and $style == Larupload::STREAM_FOLDER) { |
165
|
|
|
if ($type === LaruploadFileType::VIDEO) { |
166
|
|
|
$name = pathinfo($name, PATHINFO_FILENAME) . '.m3u8'; |
|
|
|
|
167
|
|
|
$path = $this->getBasePath($this->id, $style); |
168
|
|
|
|
169
|
|
|
return "$path/$name"; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return null; |
173
|
|
|
} |
174
|
|
|
else if ($name and $this->styleHasFile($style)) { |
|
|
|
|
175
|
|
|
$name = FixExceptionNamesAction::make($name, $style, $this->getStyle($style))->run(); |
|
|
|
|
176
|
|
|
$path = $this->getBasePath($this->id, $style); |
177
|
|
|
|
178
|
|
|
return "$path/$name"; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return null; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|