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