Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
18.33% covered (danger)
18.33%
11 / 60
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ClientFilesController
18.33% covered (danger)
18.33%
11 / 60
20.00% covered (danger)
20.00%
1 / 5
365.42
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 index
55.56% covered (warning)
55.56%
5 / 9
0.00% covered (danger)
0.00%
0 / 1
5.40
 store
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
42
 download
38.46% covered (danger)
38.46%
5 / 13
0.00% covered (danger)
0.00%
0 / 1
18.42
 destroy
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Exceptions\AppException;
6use App\Http\Controllers\Concerns\AuthorizesClientAccess;
7use App\Http\Requests\StoreClientFileRequest;
8use App\Http\Resources\ClientFileResource;
9use App\Models\TblClientFile;
10use Illuminate\Support\Facades\Storage;
11
12class ClientFilesController extends Controller
13{
14    use AuthorizesClientAccess;
15
16    private $userId;
17
18    public function __construct()
19    {
20        $this->userId = request()->header('backend-user-id');
21    }
22
23    /**
24     * GET /clients/{id}/files
25     * List files for a client.
26     */
27    public function index($id)
28    {
29        $id = (int) $id;
30
31        if ($this->isCommercial() && ! $this->commercialOwnsClient($id)) {
32            return $this->forbidden();
33        }
34
35        try {
36            return ClientFileResource::collection(
37                TblClientFile::where('client_id', $id)->get()
38            );
39
40        } catch (\Exception $e) {
41            report(AppException::fromException($e, 'GET_CLIENT_FILES_EXCEPTION'));
42
43            return response(['message' => 'KO', 'error' => $e->getMessage()]);
44        }
45    }
46
47    /**
48     * POST /clients/{id}/files
49     * Upload files for a client.
50     */
51    public function store(StoreClientFileRequest $request, $id)
52    {
53        $id = (int) $id;
54
55        if (! $this->canWrite()) {
56            return $this->forbidden();
57        }
58
59        if ($this->isCommercial() && ! $this->commercialOwnsClient($id)) {
60            return $this->forbidden();
61        }
62
63        try {
64            foreach ($request->file('files', []) as $file) {
65                $filename = time().'_'.$file->getClientOriginalName();
66                Storage::disk('s3')->putFileAs('uploads', $file, $filename, [
67                    'ContentType' => $file->getMimeType(),
68                ]);
69
70                TblClientFile::create([
71                    'client_id' => $id,
72                    'original_name' => $file->getClientOriginalName(),
73                    'filename' => $filename,
74                    'file_size' => $file->getSize(),
75                    'mime_type' => $file->getMimeType(),
76                    'uploaded_by' => $this->userId,
77                ]);
78            }
79
80            return ClientFileResource::collection(
81                TblClientFile::where('client_id', $id)->get()
82            );
83
84        } catch (\Exception $e) {
85            report(AppException::fromException($e, 'UPLOAD_CLIENT_FILES_EXCEPTION'));
86
87            return response(['message' => 'KO', 'error' => $e->getMessage()]);
88        }
89    }
90
91    /**
92     * GET /clients/files/{fileId}/download
93     * Download a client file.
94     */
95    public function download($fileId)
96    {
97        $fileId = (int) $fileId;
98        $file = TblClientFile::find($fileId);
99
100        if ($file && $this->isCommercial() && ! $this->commercialOwnsClient($file->client_id)) {
101            return $this->forbidden();
102        }
103
104        try {
105            if (! $file || ! Storage::disk('s3')->exists('uploads/'.$file->filename)) {
106                return response(['message' => 'KO']);
107            }
108
109            $content = Storage::disk('s3')->get('uploads/'.$file->filename);
110
111            return response($content)
112                ->header('Content-Type', $file->mime_type)
113                ->header('Content-Disposition', 'attachment; filename="'.$file->original_name.'"');
114
115        } catch (\Exception $e) {
116            report(AppException::fromException($e, 'DOWNLOAD_CLIENT_FILE_EXCEPTION'));
117
118            return response(['message' => 'KO', 'error' => $e->getMessage()]);
119        }
120    }
121
122    /**
123     * DELETE /clients/files/{fileId}
124     * Delete a client file.
125     */
126    public function destroy($fileId)
127    {
128        $fileId = (int) $fileId;
129
130        if (! $this->canWrite()) {
131            return $this->forbidden();
132        }
133
134        $file = TblClientFile::find($fileId);
135
136        if ($file && $this->isCommercial() && ! $this->commercialOwnsClient($file->client_id)) {
137            return $this->forbidden();
138        }
139
140        try {
141            if ($file) {
142                $file->delete();
143                Storage::disk('s3')->delete('uploads/'.$file->filename);
144            }
145
146            return response(['message' => 'OK']);
147
148        } catch (\Exception $e) {
149            report(AppException::fromException($e, 'DELETE_CLIENT_FILE_EXCEPTION'));
150
151            return response(['message' => 'KO', 'error' => $e->getMessage()]);
152        }
153    }
154}