1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mostafaznv\Larupload\Concerns\Storage\Attachment; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException; |
6
|
|
|
use Illuminate\Http\UploadedFile; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use Illuminate\Support\Facades\Storage; |
9
|
|
|
use Illuminate\Support\Facades\URL; |
10
|
|
|
use Mostafaznv\Larupload\Actions\GuessLaruploadFileTypeAction; |
11
|
|
|
use Mostafaznv\Larupload\Enums\LaruploadFileType; |
12
|
|
|
use Mostafaznv\Larupload\Jobs\ProcessFFMpeg; |
13
|
|
|
use Mostafaznv\Larupload\Larupload; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
trait QueueAttachment |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Handle FFMpeg queue on running ffmpeg queue:work |
20
|
|
|
*/ |
21
|
|
|
public function handleFFMpegQueue(bool $isLastOne = false, bool $standalone = false): void |
22
|
|
|
{ |
23
|
|
|
$this->file = $this->prepareFileForFFMpegProcess(); |
|
|
|
|
24
|
|
|
$this->type = GuessLaruploadFileTypeAction::make($this->file)->calc(); |
|
|
|
|
25
|
|
|
|
26
|
|
|
if ($this->type == LaruploadFileType::VIDEO) { |
27
|
|
|
$this->handleVideoStyles($this->id); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
else if ($this->type == LaruploadFileType::AUDIO) { |
30
|
|
|
$this->handleAudioStyles($this->id); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ($this->driverIsNotLocal() and $isLastOne) { |
|
|
|
|
34
|
|
|
Storage::disk($this->localDisk)->deleteDirectory( |
35
|
|
|
$standalone ? "$this->name" : "$this->folder/$this->id" |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function initializeFFMpegQueue(int $id, string $class, bool $standalone = false): void |
41
|
|
|
{ |
42
|
|
|
$maxQueueNum = $this->ffmpegMaxQueueNum; |
43
|
|
|
$flag = false; |
44
|
|
|
|
45
|
|
|
if ($maxQueueNum == 0) { |
46
|
|
|
$flag = true; |
47
|
|
|
} |
48
|
|
|
else { |
49
|
|
|
$availableQueues = DB::table(Larupload::FFMPEG_QUEUE_TABLE) |
50
|
|
|
->where('status', 0) |
51
|
|
|
->count(); |
52
|
|
|
|
53
|
|
|
if ($availableQueues < $maxQueueNum) { |
54
|
|
|
$flag = true; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
if ($flag) { |
60
|
|
|
// save a copy of original file to use it on process ffmpeg queue, then delete it |
61
|
|
|
if ($this->driverIsNotLocal()) { |
62
|
|
|
$path = $this->getBasePath($id, Larupload::ORIGINAL_FOLDER); |
|
|
|
|
63
|
|
|
Storage::disk($this->localDisk)->putFileAs($path, $this->file, $this->output['name']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$queueId = DB::table(Larupload::FFMPEG_QUEUE_TABLE)->insertGetId([ |
67
|
|
|
'record_id' => $id, |
68
|
|
|
'record_class' => $class, |
69
|
|
|
'created_at' => now(), |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
$serializedClass = null; |
73
|
|
|
|
74
|
|
|
if ($standalone) { |
75
|
|
|
unset($this->file); |
76
|
|
|
unset($this->cover); |
77
|
|
|
unset($this->image); |
78
|
|
|
unset($this->ffmpeg); |
79
|
|
|
|
80
|
|
|
$serializedClass = base64_encode(serialize($this)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
ProcessFFMpeg::dispatch($queueId, $id, $this->name, $class, $serializedClass); |
85
|
|
|
} |
86
|
|
|
else { |
87
|
|
|
throw new HttpResponseException(redirect(URL::previous())->withErrors([ |
88
|
|
|
'ffmpeg_queue_max_num' => trans('larupload::messages.max-queue-num-exceeded') |
89
|
|
|
])); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function prepareFileForFFMpegProcess(): UploadedFile |
94
|
|
|
{ |
95
|
|
|
$basePath = $this->getBasePath($this->id, Larupload::ORIGINAL_FOLDER); |
96
|
|
|
$path = $basePath . '/' . $this->output['name']; |
97
|
|
|
$disk = $this->driverIsLocal() ? $this->disk : $this->localDisk; |
|
|
|
|
98
|
|
|
|
99
|
|
|
$path = Storage::disk($disk)->path($path); |
100
|
|
|
|
101
|
|
|
return new UploadedFile($path, $this->output['name'], null, null, true); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|