Completed
Pull Request — master (#19)
by Şəhriyar
10:40
created

FilesController::__construct()   A

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 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 4
ccs 0
cts 4
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 App\Models\File;
6
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\Response as IlluminateResponse;
9
10
class FilesController extends Controller
11
{
12
    public function __construct()
13
    {
14
        $this->middleware('auth');
15
    }
16
17
    /**
18
     * @return \Illuminate\Http\JsonResponse
19
     */
20
    public function index()
21
    {
22
        /** @var \App\Models\User $me */
23
        $me = app('sentinel')->getUser();
24
25
        return response()->json($me->files->toArray());
26
    }
27
28
    /**
29
     * @return \Illuminate\Http\Response
30
     */
31
    public function create()
32
    {
33
        return view('file.create');
34
    }
35
36
37
    /**
38
     * @param \Illuminate\Http\Request $request
39
     *
40
     * @return \Illuminate\Http\Response
41
     */
42
    public function store(Request $request)
43
    {
44
        $validator = app('validator')->make($request->all(), [
45
            'qquuid' => 'required|string|size:36',
46
            'qqfilename' => 'required|string',
47
            'qqtotalfilesize' => 'required|numeric',
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
        /** @var \App\Models\File $file */
96
        $file = File::findOrFail($hash);
97
        if ($file->load('uploaders')->count()) {
0 ignored issues
show
Documentation Bug introduced by
The method count does not exist on object<App\Models\File>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
98
            /** @var \App\Models\User $me */
99
            $me = app('sentinel')->getUser();
100
            if ($file->uploaders->contains('id', $me->id)) {
101
                $file->uploaders()->detach($me->id);
102
                $file->load('uploaders');
103
            }
104
            !$file->uploaders->count() && app('filesystem')->disk($file->disk)->delete($file->path) && $file->delete();
105
        }
106
107
        return response()->json()->setStatusCode(IlluminateResponse::HTTP_NO_CONTENT);
108
    }
109
}
110