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