1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mostafaznv\Larupload\Concerns\Storage\Attachment; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Storage; |
6
|
|
|
use Mostafaznv\Larupload\Enums\LaruploadFileType; |
7
|
|
|
use Mostafaznv\Larupload\Larupload; |
8
|
|
|
use Mostafaznv\Larupload\Actions\FixExceptionNamesAction; |
9
|
|
|
|
10
|
|
|
trait StyleAttachment |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Handle styles |
14
|
|
|
* resize, crop and generate styles from original file |
15
|
|
|
* |
16
|
|
|
* @param string $id |
17
|
|
|
* @param string $class |
18
|
|
|
* @param bool $standalone |
19
|
|
|
*/ |
20
|
|
|
protected function handleStyles(string $id, string $class, bool $standalone = false): void |
21
|
|
|
{ |
22
|
|
|
switch ($this->type) { |
23
|
|
|
case LaruploadFileType::IMAGE: |
24
|
|
|
foreach ($this->imageStyles as $name => $style) { |
25
|
|
|
$path = $this->getBasePath($id, $name); |
|
|
|
|
26
|
|
|
$saveTo = $path . '/' . FixExceptionNamesAction::make($this->output['name'], $name)->run(); |
27
|
|
|
|
28
|
|
|
Storage::disk($this->disk)->makeDirectory($path); |
29
|
|
|
$this->img($this->file)->resize($saveTo, $style); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
break; |
33
|
|
|
|
34
|
|
|
case LaruploadFileType::VIDEO: |
35
|
|
|
if ($this->ffmpegQueue) { |
36
|
|
|
if ($this->driverIsNotLocal()) { |
|
|
|
|
37
|
|
|
$this->uploadOriginalFile($id, $this->localDisk); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->initializeFFMpegQueue($id, $class, $standalone); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
else { |
43
|
|
|
$this->handleVideoStyles($id); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
break; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Handle styles for videos |
52
|
|
|
* |
53
|
|
|
* @param $id |
54
|
|
|
*/ |
55
|
|
|
protected function handleVideoStyles($id): void |
56
|
|
|
{ |
57
|
|
|
foreach ($this->videoStyles as $name => $style) { |
58
|
|
|
$path = $this->getBasePath($id, $name); |
59
|
|
|
Storage::disk($this->disk)->makeDirectory($path); |
60
|
|
|
$saveTo = "$path/{$this->output['name']}"; |
61
|
|
|
|
62
|
|
|
$this->ffmpeg()->manipulate($style, $saveTo); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (count($this->streams)) { |
66
|
|
|
$fileName = pathinfo($this->output['name'], PATHINFO_FILENAME) . '.m3u8'; |
|
|
|
|
67
|
|
|
|
68
|
|
|
$path = $this->getBasePath($id, Larupload::STREAM_FOLDER); |
69
|
|
|
Storage::disk($this->disk)->makeDirectory($path); |
70
|
|
|
|
71
|
|
|
$this->ffmpeg()->stream($this->streams, $path, $fileName); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Prepare style path |
77
|
|
|
* this function will use to prepare full path of given style to generate url/download response |
78
|
|
|
* |
79
|
|
|
* @param string $style |
80
|
|
|
* @return string|null |
81
|
|
|
*/ |
82
|
|
|
protected function prepareStylePath(string $style): ?string |
83
|
|
|
{ |
84
|
|
|
$staticStyles = [ |
85
|
|
|
Larupload::ORIGINAL_FOLDER, |
86
|
|
|
Larupload::COVER_FOLDER, |
87
|
|
|
Larupload::STREAM_FOLDER |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
if (isset($this->id) and (in_array($style, $staticStyles) or array_key_exists($style, $this->imageStyles) or array_key_exists($style, $this->videoStyles))) { |
91
|
|
|
$name = $style == Larupload::COVER_FOLDER |
92
|
|
|
? $this->output['cover'] |
93
|
|
|
: $this->output['name']; |
94
|
|
|
|
95
|
|
|
$type = $this->output['type'] |
96
|
|
|
? LaruploadFileType::from($this->output['type']) |
97
|
|
|
: null; |
98
|
|
|
|
99
|
|
|
if ($name and $style == Larupload::STREAM_FOLDER) { |
100
|
|
|
if ($type === LaruploadFileType::VIDEO) { |
101
|
|
|
$name = pathinfo($name, PATHINFO_FILENAME) . '.m3u8'; |
|
|
|
|
102
|
|
|
$path = $this->getBasePath($this->id, $style); |
103
|
|
|
|
104
|
|
|
return "$path/$name"; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
else if ($name and $this->styleHasFile($style)) { |
|
|
|
|
110
|
|
|
$name = FixExceptionNamesAction::make($name, $style)->run(); |
111
|
|
|
$path = $this->getBasePath($this->id, $style); |
112
|
|
|
|
113
|
|
|
return "$path/$name"; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|