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