FilesController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace App\Http\Controllers;
2
3
use App\Exceptions\Common\ValidationException;
4
use App\Exceptions\FileStream as FileStreamExceptions;
5
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response as IlluminateResponse;
8
9
class FilesController extends Controller
10
{
11
    public function __construct()
12
    {
13
        $this->middleware('auth');
14
    }
15
16
    /**
17
     * @return \Illuminate\Http\JsonResponse
18
     */
19
    public function index()
20
    {
21
        /** @var \App\Models\User $me */
22
        $me = app('sentinel')->getUser();
23
24
        return response()->json($me->files->toArray());
25
    }
26
27
    /**
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function create()
31
    {
32
        return view('file.create');
33
    }
34
35
    /**
36
     * @param \Illuminate\Http\Request $request
37
     *
38
     * @return \Illuminate\Http\JsonResponse
39
     * @throws \App\Exceptions\Common\ValidationException
40
     */
41
    public function store(Request $request)
42
    {
43
        $validator = app('validator')->make($request->all(), [
44
            'qquuid' => 'required|string|size:36',
45
            'qqfilename' => 'required|string',
46
            'qqtotalfilesize' => 'required|numeric',
47
            'qqtag' => 'required|string|in:' . implode(',', array_keys(config('filesystems.allowed_tags_and_limits'))),
48
            'qqtotalparts' => 'required_with_all:qqpartindex,qqpartbyteoffset,qqchunksize|numeric',
49
            'qqpartindex' => 'required_with_all:qqtotalparts,qqpartbyteoffset,qqchunksize|numeric',
50
            'qqpartbyteoffset' => 'required_with_all:qqpartindex,qqtotalparts,qqchunksize|numeric',
51
            'qqchunksize' => 'required_with_all:qqpartindex,qqpartbyteoffset,qqtotalparts|numeric',
52
            'qqresume' => 'sometimes|required_with_all:qqpartindex,qqpartbyteoffset,qqtotalparts,qqchunksize|string'
53
        ]);
54
        if ($validator->fails()) {
55
            throw new ValidationException($validator);
56
        }
57
58
        if (strpos($request->header('content-type'), 'multipart/form-data') === false && !$request->has('post-process')) {
59
            throw new FileStreamExceptions\UploadRequestIsNotMultipartFormDataException;
60
        }
61
62
        $request->has('qqresume') && $request->get('qqresume') === 'true' && app('filestream')->isUploadResumable($request);
63
64
        return app('filestream')->handleUpload($request);
65
    }
66
67
68
    /**
69
     * @param string $hash
70
     *
71
     * @return \Illuminate\Http\JsonResponse
72
     *
73
     */
74
    public function show($hash)
75
    {
76
        /** @var \App\Models\User $me */
77
        $me = app('sentinel')->getUser();
78
79
        $file = $me->with(['files' => function (BelongsToMany $query) use ($hash) {
80
            $query->where('hash', '=', $hash);
81
        }])->first()->files->first();
82
83
84
        return response()->json($file);
85
    }
86
87
88
    /**
89
     * @param string $hash
90
     *
91
     * @return \Illuminate\Http\JsonResponse
92
     */
93
    public function destroy($hash)
94
    {
95
        app('filestream')->deleteFile($hash);
96
97
        return response()->json()->setStatusCode(IlluminateResponse::HTTP_NO_CONTENT);
98
    }
99
}
100