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

DownloadController::__invoke()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 18
rs 10
cc 4
nc 4
nop 2
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