1 | <?php |
||
14 | class Service |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * ViddlerClient |
||
19 | */ |
||
20 | protected $client = null; |
||
21 | |||
22 | /** |
||
23 | * Checks the encoding status of a Viddler video |
||
24 | */ |
||
25 | 6 | public function check(Viddler $model) |
|
26 | { |
||
27 | 6 | $model = $model->check(); |
|
28 | 3 | return $model; |
|
29 | } |
||
30 | |||
31 | /** |
||
32 | * Convert, Upload and Store in Database from a video file |
||
33 | */ |
||
34 | 24 | public function create(UploadedFile $file, $title) |
|
35 | { |
||
36 | 24 | if ($file->isValid()) { |
|
37 | //Check file type |
||
38 | 21 | $ok = $this->isMimeSupported($file->getMimeType()); |
|
39 | |||
40 | 21 | if ($ok === true) { |
|
41 | //Store the file |
||
42 | 18 | $filename = $file->hashName(); |
|
43 | 18 | $disk = Storage::disk(config('viddler.disk')); |
|
44 | 18 | $contents = file_get_contents($file->getRealPath()); |
|
45 | 18 | $disk->put("new/".$filename, $contents); |
|
46 | |||
47 | 18 | $class = config('viddler.model'); |
|
48 | |||
49 | 18 | $video = new $class([ |
|
50 | 18 | 'disk' => config('viddler.disk'), |
|
51 | 18 | 'path' => 'new', |
|
52 | 18 | 'filename' => $filename, |
|
53 | 18 | 'title' => $title, |
|
54 | 18 | 'status' => 'new', |
|
55 | 18 | 'mime' => $file->getMimeType(), |
|
56 | 18 | 'extension' => $file->extension(), |
|
57 | 18 | ]); |
|
58 | 18 | $video->save(); |
|
59 | |||
60 | //Done |
||
61 | 18 | return $video; |
|
62 | } else { |
||
63 | 3 | $msg = []; |
|
64 | 3 | $msg[] = "Incorrect file type!"; |
|
65 | 3 | if (is_object($file)) { |
|
66 | 3 | $msg[] = $file->getClientOriginalExtension(); |
|
67 | 3 | $msg[] = "(".$file->getMimeType().")"; |
|
68 | 3 | } |
|
69 | 3 | throw new ViddlerIncorrectVideoTypeException(implode(" ", $msg)); |
|
70 | } |
||
71 | } else { |
||
72 | 3 | throw new ViddlerUploadFailedException("Upload Failed"); |
|
73 | } |
||
74 | } |
||
75 | |||
76 | 6 | public function setClient(ViddlerClient $client) |
|
80 | |||
81 | public function getClient() |
||
82 | { |
||
88 | |||
89 | /** |
||
90 | * Check if a mime is in the supported list |
||
91 | * @param string|null $mime |
||
92 | */ |
||
93 | 21 | protected function isMimeSupported($mime) |
|
98 | |||
99 | 21 | protected function getSupportedMimes() |
|
114 | } |
||
115 |