1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace LeadThread\Viddler\Upload\Components;
|
4
|
|
|
|
5
|
|
|
use LeadThread\Viddler\Upload\Traits\CanLog;
|
6
|
|
|
use LeadThread\Viddler\Upload\Exceptions\ViddlerVideoConversionFailedException;
|
7
|
|
|
use LeadThread\Viddler\Upload\Models\Viddler;
|
8
|
|
|
use Storage;
|
9
|
|
|
use File;
|
10
|
|
|
|
11
|
|
|
class VideoFile
|
12
|
|
|
{
|
13
|
|
|
use CanLog;
|
14
|
|
|
|
15
|
|
|
protected $model;
|
16
|
|
|
|
17
|
18 |
|
public function __construct(Viddler &$model)
|
18
|
|
|
{
|
19
|
18 |
|
$this->model = $model;
|
20
|
18 |
|
}
|
21
|
|
|
|
22
|
18 |
|
public function convert()
|
23
|
|
|
{
|
24
|
18 |
|
$this->info("{$this->model}'s video file is starting convert");
|
25
|
18 |
|
if ($this->model->isNotResolved() && config('viddler.convert.enabled')) {
|
26
|
|
|
// Move to appropriate disk
|
27
|
18 |
|
$this->model->updateStatusTo('converting');
|
28
|
|
|
|
29
|
|
|
// Check if conversion is needed
|
30
|
18 |
|
if ($this->shouldConvert($this->model)) {
|
31
|
3 |
|
$output = config('viddler.convert.instructions')[$this->model->mime];
|
32
|
|
|
switch ($output) {
|
33
|
3 |
|
case "video/mp4":
|
34
|
3 |
|
$this->convertToMp4();
|
35
|
3 |
|
break;
|
36
|
|
|
default:
|
37
|
|
|
throw new ViddlerVideoConversionFailedException("{$output} is not a supported output type.");
|
38
|
|
|
}
|
39
|
3 |
|
}
|
40
|
18 |
|
}
|
41
|
18 |
|
return $this->model;
|
42
|
|
|
}
|
43
|
|
|
|
44
|
18 |
|
public function moveTo($status)
|
45
|
|
|
{
|
46
|
18 |
|
$this->info("{$this->model}'s video file is moving to {$status}");
|
47
|
18 |
|
if ($this->model->isNotResolved()) {
|
48
|
18 |
|
$disk = $this->getDisk();
|
49
|
18 |
|
$dest = "{$status}/{$this->model->filename}";
|
50
|
|
|
|
51
|
|
|
// Delete a prexisting file
|
52
|
18 |
|
if ($disk->exists($dest)) {
|
53
|
3 |
|
$disk->delete($dest);
|
54
|
3 |
|
}
|
55
|
|
|
|
56
|
|
|
// Do the move
|
57
|
18 |
|
$disk->move($this->getPathOnDisk(), $dest);
|
58
|
|
|
|
59
|
|
|
//Update the Model
|
60
|
18 |
|
$this->model->path = $status;
|
61
|
18 |
|
$this->model->save();
|
62
|
18 |
|
}
|
63
|
|
|
|
64
|
18 |
|
return $this->model;
|
65
|
|
|
}
|
66
|
|
|
|
67
|
3 |
|
protected function convertToMp4()
|
68
|
|
|
{
|
69
|
3 |
|
$this->info("{$this->model}'s video file is coverting to mp4");
|
70
|
3 |
|
$disk = $this->getDisk();
|
71
|
3 |
|
$pathDisk = $this->getPathToDisk();
|
72
|
3 |
|
$pathOld = $this->getPathOnDisk();
|
73
|
3 |
|
$pathNew = explode(".", $pathOld)[0].".mp4";
|
74
|
3 |
|
$exit = 0;
|
75
|
3 |
|
$output = [];
|
76
|
3 |
|
$command = 'ffmpeg -i '.$pathDisk.$pathOld.' -c copy '.$pathDisk.$pathNew.' 2>&1';
|
77
|
3 |
|
exec($command, $output, $exit);
|
78
|
3 |
|
if ($exit === 0) {
|
79
|
3 |
|
$parts = explode('/', $pathNew);
|
80
|
3 |
|
$disk->delete($pathOld);
|
81
|
3 |
|
$this->model->mime = File::mimeType($pathDisk.$pathNew);
|
82
|
3 |
|
$this->model->extension = File::extension($pathDisk.$pathNew);
|
83
|
3 |
|
$this->model->filename = end($parts);
|
84
|
3 |
|
$this->model->updateStatusTo('converted');
|
85
|
3 |
|
} else {
|
86
|
|
|
throw new ViddlerVideoConversionFailedException(implode(PHP_EOL, $output));
|
87
|
|
|
}
|
88
|
|
|
|
89
|
3 |
|
return $this;
|
90
|
|
|
}
|
91
|
|
|
|
92
|
18 |
|
public function removeFile()
|
93
|
|
|
{
|
94
|
6 |
|
if ($this->model->status !== 'error' && !empty($this->model->disk)) {
|
95
|
18 |
|
$this->info("{$this->model}'s video file is being removed");
|
96
|
6 |
|
$disk = $this->getDisk();
|
97
|
6 |
|
$dest = "{$this->model->path}/{$this->model->filename}";
|
98
|
|
|
|
99
|
|
|
// Delete a prexisting file
|
100
|
6 |
|
if ($disk->exists($dest)) {
|
101
|
3 |
|
$disk->delete($dest);
|
102
|
3 |
|
}
|
103
|
6 |
|
}
|
104
|
6 |
|
}
|
105
|
|
|
|
106
|
18 |
|
protected function shouldConvert(Viddler $model)
|
107
|
|
|
{
|
108
|
18 |
|
return array_key_exists($model->mime, config('viddler.convert.instructions'));
|
109
|
|
|
}
|
110
|
|
|
|
111
|
18 |
|
protected function getDisk()
|
112
|
|
|
{
|
113
|
18 |
|
return Storage::disk($this->model->disk);
|
114
|
|
|
}
|
115
|
|
|
|
116
|
18 |
|
public function getPathOnDisk()
|
117
|
|
|
{
|
118
|
18 |
|
return "{$this->model->path}/{$this->model->filename}";
|
119
|
|
|
}
|
120
|
|
|
|
121
|
18 |
|
public function getFullPath()
|
122
|
|
|
{
|
123
|
18 |
|
return $this->getPathToDisk() . $this->getPathOnDisk();
|
124
|
|
|
}
|
125
|
|
|
|
126
|
18 |
|
public function getPathToDisk()
|
127
|
3 |
|
{
|
128
|
18 |
|
return $this->getDisk()->getDriver()->getAdapter()->getPathPrefix();
|
129
|
|
|
}
|
130
|
|
|
}
|
131
|
|
|
|