Passed
Push — dev6 ( 794b39...988b8a )
by Ron
17:48
created

DownloadController::__invoke()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 19
rs 10
cc 4
nc 4
nop 2
1
<?php
2
3
namespace App\Http\Controllers\Home;
4
5
use App\Events\Home\DownloadedFileEvent;
6
use App\Http\Controllers\Controller;
7
use App\Models\FileUploads;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\Storage;
11
12
class DownloadController extends Controller
13
{
14
    /**
15
     * Download a file stored in the database
16
     */
17
    public function __invoke($fileId, $filename)
18
    {
19
        //  Verify file ID and filename match
20
        $file = FileUploads::where('file_id', $fileId)->where('file_name', $filename)->firstOrFail();
21
22
        //  If file is not being downloaded by user, verify file is public accessable
23
        if(!Auth::check() && !$file->public)
24
        {
25
            abort(403, 'You do not have permission to download this file');
26
        }
27
28
        //  Verify that the file is in place
29
        if(!Storage::disk($file->disk)->exists($file->folder.DIRECTORY_SEPARATOR.$file->file_name))
30
        {
31
            abort(404, 'Unable to find the file specified');
32
        }
33
34
        event(new DownloadedFileEvent($file));
35
        return Storage::disk($file->disk)->download($file->folder.DIRECTORY_SEPARATOR.$file->file_name);
36
    }
37
}
38