Test Failed
Push — dev6 ( 033ddc...06d260 )
by Ron
20:10
created

DownloadController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 4
eloc 7
c 4
b 0
f 0
dl 0
loc 23
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 4
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\FileUploads;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Storage;
9
10
class DownloadController extends Controller
11
{
12
    /**
13
     *  Download a file from the database
14
     */
15
    public function __invoke($id, $name)
16
    {
17
        //  Make sure that the file actually exists in the database - both file ID and name must match
18
        $file = FileUploads::where('file_id', $id)->where('file_name', $name)->firstOrFail();
19
20
        //  Determine if the person downloading the file is allowed to download it
21
        if(!Auth::check() && !$file->public)
22
        {
23
            abort(403, 'You Do Not Have Permission To Download This File');
24
        }
25
26
        //  Determine that the file itself exists
27
        if(!Storage::disk($file->disk)->exists($file->folder.DIRECTORY_SEPARATOR.$file->file_name))
28
        {
29
            abort(404, 'Cannot Find the File Specified');
30
        }
31
32
        return Storage::disk($file->disk)->download($file->folder.DIRECTORY_SEPARATOR.$file->file_name);
33
    }
34
}
35