1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\VideoUploadRequest; |
6
|
|
|
use App\Repositories\UserRepository as User; |
7
|
|
|
use App\Repositories\VideoRepository as Video; |
8
|
|
|
use App\Transformers\VideoTransformer; |
9
|
|
|
use Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
use Illuminate\Support\Facades\Storage; |
12
|
|
|
use Linkthrow\Ffmpeg\Classes\FFMPEG; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class VideoController. |
16
|
|
|
*/ |
17
|
|
|
class VideoController extends ApiGuardController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var VideoTransformer |
21
|
|
|
*/ |
22
|
|
|
protected $videoTransformer; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Video |
26
|
|
|
*/ |
27
|
|
|
protected $video; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $apiMethods = [ |
33
|
|
|
'getAllVideos' => [ |
34
|
|
|
'keyAuthentication' => false, |
35
|
|
|
], |
36
|
|
|
'show' => [ |
37
|
|
|
'keyAuthentication' => false, |
38
|
|
|
], |
39
|
|
|
'getBestVideos' => [ |
40
|
|
|
'keyAuthentication' => false, |
41
|
|
|
], |
42
|
|
|
'getVideosUser' => [ |
43
|
|
|
'keyAuthentication' => false, |
44
|
|
|
], |
45
|
|
|
'getVideosForCategory' => [ |
46
|
|
|
'keyAuthentication' => false, |
47
|
|
|
], |
48
|
|
|
'getVideosForSearch' => [ |
49
|
|
|
'keyAuthentication' => false, |
50
|
|
|
], |
51
|
|
|
'store' => [ |
52
|
|
|
'limits' => [ |
53
|
|
|
'key' => [ |
54
|
|
|
'increment' => '1 hour', |
55
|
|
|
'limit' => 10, |
56
|
|
|
], |
57
|
|
|
], |
58
|
|
|
], |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* VideoController constructor. |
63
|
|
|
* |
64
|
|
|
* @param VideoTransformer $videoTransformer |
65
|
|
|
* @param Video $video |
66
|
|
|
* @param User $user |
67
|
|
|
*/ |
68
|
12 |
|
public function __construct(Video $video, User $user, VideoTransformer $videoTransformer) |
69
|
|
|
{ |
70
|
12 |
|
parent::__construct(); |
71
|
|
|
|
72
|
12 |
|
$this->videoTransformer = $videoTransformer; |
73
|
12 |
|
$this->video = $video; |
74
|
12 |
|
$this->user = $user; |
75
|
12 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return all Videos. |
79
|
|
|
*/ |
80
|
5 |
|
public function getAllVideos() |
81
|
|
|
{ |
82
|
5 |
|
$video = $this->video->all(); |
83
|
|
|
|
84
|
5 |
|
return $this->response->withCollection($video, $this->videoTransformer); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return the best Videos. |
89
|
|
|
*/ |
90
|
1 |
|
public function getBestVideos() |
91
|
|
|
{ |
92
|
1 |
|
$video = $this->video->best(); |
93
|
|
|
|
94
|
1 |
|
return $this->response->withCollection($video, $this->videoTransformer); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return Videos only user. |
99
|
|
|
*/ |
100
|
1 |
|
public function getVideosUser($id) |
101
|
|
|
{ |
102
|
1 |
|
$user = $this->user->findOrFail($id); |
103
|
1 |
|
$video = $user->getVideos; |
104
|
|
|
|
105
|
1 |
|
return $this->response->withCollection($video, $this->videoTransformer); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return Videos for Category. |
110
|
|
|
*/ |
111
|
1 |
|
public function getVideosForCategory($name) |
112
|
|
|
{ |
113
|
1 |
|
$video = $this->video->findBy('category', $name); |
114
|
|
|
|
115
|
1 |
|
return $this->response->withCollection($video, $this->videoTransformer); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Return Videos for Search. |
120
|
|
|
*/ |
121
|
1 |
|
public function getVideosForSearch($search) |
122
|
|
|
{ |
123
|
1 |
|
$video = $this->video->search('name', $search); |
124
|
|
|
|
125
|
1 |
|
return $this->response->withCollection($video, $this->videoTransformer); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Store a newly created video in storage. |
130
|
|
|
* |
131
|
|
|
* @param VideoUploadRequest $request |
132
|
|
|
* |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
1 |
|
public function store(VideoUploadRequest $request) |
136
|
|
|
{ |
137
|
1 |
|
$user = $this->user->authenticated(); |
138
|
1 |
|
$file = $request->video; |
|
|
|
|
139
|
1 |
|
$nameFile = str_replace(' ', '', $request->input('name').$user->id); |
140
|
|
|
|
141
|
|
|
$data = [ |
142
|
1 |
|
'name' => $request->input('name'), |
143
|
1 |
|
'category' => $request->input('category'), |
144
|
1 |
|
'path' => Storage::url('videos/'.$nameFile), |
145
|
1 |
|
'user_id' => $user->id, |
146
|
1 |
|
]; |
147
|
|
|
|
148
|
1 |
|
$video = $this->video->create($data); |
149
|
|
|
|
150
|
1 |
|
$this->saveAndConvert($file, $nameFile); |
151
|
|
|
|
152
|
1 |
|
return $this->response->withItem($video, $this->videoTransformer); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param $id |
157
|
|
|
* |
158
|
|
|
* @return mixed |
159
|
|
|
*/ |
160
|
2 |
|
public function show($id) |
161
|
|
|
{ |
162
|
2 |
|
$video = $this->video->findOrFail($id); |
163
|
|
|
|
164
|
1 |
|
return $this->response->withItem($video, $this->videoTransformer); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Update the specified video in storage. |
169
|
|
|
* |
170
|
|
|
* @param Request $request |
171
|
|
|
* @param $id |
172
|
|
|
* |
173
|
|
|
* @return mixed |
174
|
|
|
*/ |
175
|
1 |
|
public function update(Request $request, $id) |
176
|
|
|
{ |
177
|
1 |
|
$video = $this->video->findOrFail($id); |
178
|
|
|
|
179
|
1 |
|
$this->video->update($request->all(), $id); |
180
|
|
|
|
181
|
1 |
|
return $this->response->withItem($video, $this->videoTransformer); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Remove the specified video from storage. |
186
|
|
|
* |
187
|
|
|
* @param $id |
188
|
|
|
* |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
1 |
|
public function destroy($id) |
192
|
|
|
{ |
193
|
1 |
|
$video = $this->video->findOrFail($id); |
194
|
|
|
|
195
|
1 |
|
$nameFile = str_replace('storage/', '', $video->path); |
196
|
|
|
|
197
|
1 |
|
Storage::disk('public')->delete([$nameFile.'.mp4', $nameFile.'.webm']); |
198
|
|
|
|
199
|
1 |
|
$this->video->delete($id); |
200
|
1 |
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Save and Convert to webm video. |
204
|
|
|
* |
205
|
|
|
* @param $video |
206
|
|
|
* @param $name |
207
|
|
|
*/ |
208
|
1 |
|
private function saveAndConvert($video, $name) |
209
|
|
|
{ |
210
|
1 |
|
Storage::disk('public')->put('videos/'.$name.'.mp4', file_get_contents($video->getRealPath())); |
211
|
|
|
|
212
|
1 |
|
FFMPEG::convert() |
213
|
1 |
|
->input($video->getRealPath()) |
214
|
1 |
|
->output(storage_path('app/public/videos/').$name.'.webm') |
215
|
1 |
|
->overwrite(true) |
216
|
1 |
|
->go(); |
217
|
1 |
|
} |
218
|
|
|
} |
219
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: