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
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Handle styles for videos |
62
|
|
|
* |
63
|
|
|
* @param $id |
64
|
|
|
*/ |
65
|
|
|
protected function handleVideoStyles($id): void |
66
|
|
|
{ |
67
|
|
|
foreach ($this->videoStyles as $name => $style) { |
68
|
|
|
$path = $this->getBasePath($id, $name); |
69
|
|
|
Storage::disk($this->disk)->makeDirectory($path); |
70
|
|
|
$saveTo = "$path/{$this->output['name']}"; |
71
|
|
|
|
72
|
|
|
$this->ffmpeg()->manipulate($style, $saveTo); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (count($this->streams)) { |
76
|
|
|
$fileName = pathinfo($this->output['name'], PATHINFO_FILENAME) . '.m3u8'; |
|
|
|
|
77
|
|
|
|
78
|
|
|
$path = $this->getBasePath($id, Larupload::STREAM_FOLDER); |
79
|
|
|
Storage::disk($this->disk)->makeDirectory($path); |
80
|
|
|
|
81
|
|
|
$this->ffmpeg()->stream($this->streams, $path, $fileName); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Prepare style path |
87
|
|
|
* this function will use to prepare full path of given style to generate url/download response |
88
|
|
|
* |
89
|
|
|
* @param string $style |
90
|
|
|
* @return string|null |
91
|
|
|
*/ |
92
|
|
|
protected function prepareStylePath(string $style): ?string |
93
|
|
|
{ |
94
|
|
|
$staticStyles = [ |
95
|
|
|
Larupload::ORIGINAL_FOLDER, |
96
|
|
|
Larupload::COVER_FOLDER, |
97
|
|
|
Larupload::STREAM_FOLDER |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
if (isset($this->id) and (in_array($style, $staticStyles) or array_key_exists($style, $this->imageStyles) or array_key_exists($style, $this->videoStyles))) { |
101
|
|
|
$name = $style == Larupload::COVER_FOLDER |
102
|
|
|
? $this->output['cover'] |
103
|
|
|
: $this->output['name']; |
104
|
|
|
|
105
|
|
|
$type = $this->output['type'] |
106
|
|
|
? LaruploadFileType::from($this->output['type']) |
107
|
|
|
: null; |
108
|
|
|
|
109
|
|
|
if ($name and $style == Larupload::STREAM_FOLDER) { |
110
|
|
|
if ($type === LaruploadFileType::VIDEO) { |
111
|
|
|
$name = pathinfo($name, PATHINFO_FILENAME) . '.m3u8'; |
|
|
|
|
112
|
|
|
$path = $this->getBasePath($this->id, $style); |
113
|
|
|
|
114
|
|
|
return "$path/$name"; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
else if ($name and $this->styleHasFile($style)) { |
|
|
|
|
120
|
|
|
$name = FixExceptionNamesAction::make($name, $style)->run(); |
121
|
|
|
$path = $this->getBasePath($this->id, $style); |
122
|
|
|
|
123
|
|
|
return "$path/$name"; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return null; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|