ProblemController::upload()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 14
rs 9.6111
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Repositories\Criteria\Like;
6
use App\Repositories\Criteria\Where;
7
use App\Repositories\ProblemRepository;
8
use App\Services\DataProvider;
9
use Czim\Repository\Criteria\Common\WithRelations;
10
use Illuminate\Database\Eloquent\ModelNotFoundException;
11
use Illuminate\Filesystem\Filesystem;
12
use Illuminate\Http\UploadedFile;
13
14
class ProblemController extends DataController
15
{
16
    public function index()
17
    {
18
        if (request()->filled('id')) {
19
            $this->repository->pushCriteria(new Where('id', request('id')));
20
        }
21
22
        if (request()->filled('title')) {
23
            $this->repository->pushCriteria(new Like('title', request('title')));
24
        }
25
26
        if (request()->filled('source')) {
27
            $this->repository->pushCriteria(new Like('source', request('source')));
28
        }
29
        if (request()->filled('status')) {
30
            $this->repository->pushCriteria(new Where('status', request('status')));
31
        }
32
33
        $this->repository->pushCriteria(new WithRelations(['author']));
34
35
        return parent::index();
36
    }
37
38
    public function getFile($id, $name)
39
    {
40
        $name = htmlspecialchars($name);
41
        $problem = $this->repository->find($id);
42
43
        if ($problem) {
44
            $dp = app(DataProvider::class);
45
46
            $path = $dp->getDataPath($id).$name;
47
48
            return response()->download($path, $name);
49
        }
50
51
        return '';
52
    }
53
54
    public function removeFile($id, $name)
55
    {
56
        $name = htmlspecialchars($name);
57
58
        try {
59
            $this->repository->findOrFail($id);
60
            $fs = new Filesystem();
61
            $path = config('hustoj.data_path').'/'.$id.'/'.$name;
62
            if ($fs->exists($path) && $fs->delete($path)) {
63
                return ['code' => 0];
64
            }
65
66
            return [
67
                'code'    => -1,
68
                'message' => 'file not exist or you have no permission delete it!',
69
            ];
70
        } catch (ModelNotFoundException $e) {
71
            return [
72
                'code'    => -1,
73
                'message' => 'File not found!',
74
            ];
75
        }
76
    }
77
78
    public function dataFiles($id)
79
    {
80
        $problem = $this->repository->find($id);
81
82
        if ($problem) {
83
            $fs = new Filesystem();
84
            $path = config('hustoj.data_path').'/'.$id;
85
            if (! $fs->exists($path)) {
86
                $fs->makeDirectory($path);
87
            }
88
            $files = $fs->files($path);
89
90
            $result = [];
91
            foreach ($files as $file) {
92
                $result[] = $fs->basename($file);
93
            }
94
95
            return $result;
96
        }
97
98
        return [];
99
    }
100
101
    public function upload($id)
102
    {
103
        $problem = $this->repository->find($id);
104
105
        if ($problem && request()->hasFile('files')) {
106
            $fs = new Filesystem();
107
            $path = config('hustoj.data_path').'/'.$id;
108
            if (! $fs->exists($path)) {
109
                $fs->makeDirectory($path);
110
            }
111
            /** @var UploadedFile[] $files */
112
            $files = request()->allFiles();
113
            foreach ($files as $file) {
114
                $file->move($path, $file->getClientOriginalName());
115
            }
116
        }
117
    }
118
119
    protected function getRepository()
120
    {
121
        return ProblemRepository::class;
122
    }
123
}
124