Passed
Push — main ( 44f9b3...d7b1c0 )
by Yaroslav
02:39
created

VideoController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 78.05%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 91
ccs 32
cts 41
cp 0.7805
rs 10
c 0
b 0
f 0
wmc 16

2 Methods

Rating   Name   Duplication   Size   Complexity  
B findField() 0 19 9
B store() 0 60 7
1
<?php
2
3
namespace NovaChunkedVideo\Http\Controllers;
4
5
use Illuminate\Routing\Controller;
6
use Illuminate\Support\Facades\Storage;
7
use Illuminate\Validation\ValidationException;
8
use Laravel\Nova\Fields\Field;
9
use Laravel\Nova\Http\Requests\NovaRequest;
10
use NovaChunkedVideo\ChunkedVideo;
11
12
class VideoController extends Controller
13
{
14 2
    public function store(NovaRequest $request)
15
    {
16 2
        $resource = $request->findResourceOrFail();
17
18 2
        $resource->authorizeToUpdate($request);
19
20 2
        $model = $resource->model();
21
22 2
        if (!$model) {
23
            throw ValidationException::withMessages(['file' => 'not valid model']);
24
        }
25
26 2
        $fields = $resource->updateFields($request);
27
28
        /** @var ChunkedVideo|null $field */
29 2
        $field = $this->findField($request, $fields);
30 2
        if (!$field) {
0 ignored issues
show
introduced by
$field is of type NovaChunkedVideo\ChunkedVideo, thus it always evaluated to true.
Loading history...
31
            throw new \Exception("Not valid field [{$request->attribute}]");
32
        }
33
34 2
        $file      = $request->file('file');
35 2
        $storage   = Storage::disk($field->getStorageDisk());
36 2
        $chunksDir = $field->getChunksFolder();
37
38 2
        $fileName = "{$model->getKey()}-{$request->user()?->getKey()}-{$field->attribute}-{$file->getClientOriginalName()}";
39 2
        $path     = "{$chunksDir}{$fileName}";
40
41
        // Create empty file if file not exists
42 2
        if (!$storage->exists($path)) {
43 2
            $storage->put($path, '');
44
        }
45
46
        // Set optimal memory limit
47 2
        $newLimit   = ceil(($storage->size($path) + $file->getSize()) / 1000000) + 100;
48 2
        ini_set('memory_limit', "{$newLimit}M");
49
        // Append new content to file
50 2
        file_put_contents($storage->path($path), $file->get(), FILE_APPEND);
51
52
        // Validate file size
53 2
        if ($storage->size($path) > $field->getMaxSize()) {
54
            $storage->delete($path);
55
56
            throw ValidationException::withMessages(['file' => 'file greater than max size']);
57
        }
58
59 2
        if ($request->has('is_last') && $request->boolean('is_last')) {
60 1
            $fileName = basename($path, '.part');
61 1
            $newPath  = "{$chunksDir}{$fileName}";
62
63
            // remove old file if exists
64 1
            $storage->delete($newPath);
65
            // move file
66 1
            $storage->move($path, $newPath);
67
68 1
            $videoUrl = $field->storeFile($request, $newPath);
69
70 1
            return response()->json(['uploaded' => true, 'video_url' => $videoUrl]);
71
        }
72
73 1
        return response()->json(['uploaded' => true]);
74
    }
75
76
    /**
77
     * This function support "dependency container search"
78
     *
79
     * @param  NovaRequest  $request
80
     * @param $fields
81
     *
82
     * @return Field|null
83
     */
84 2
    protected function findField(NovaRequest $request, $fields)
85
    {
86
        /** @var Field $field */
87 2
        foreach ($fields as $field) {
88 2
            if (get_class($field) == 'Alexwenzel\DependencyContainer\DependencyContainer') {
89
                if (!empty($field->meta['fields'])) {
90
                    $field = $this->findField($request, $field->meta['fields']);
91
                    if ($field && $field instanceof ChunkedVideo) {
92
                        return $field;
93
                    }
94
                }
95
            } else {
96 2
                if ($field instanceof ChunkedVideo && !empty($field->attribute) && $field->attribute == $request->field) {
97 2
                    return $field;
98
                }
99
            }
100
        }
101
102
        return null;
103
    }
104
}
105