Passed
Push — dev5a ( e86993...328ab1 )
by Ron
10:15 queued 39s
created

DownloadController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 23
c 3
b 0
f 0
dl 0
loc 45
ccs 0
cts 25
cp 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 14 3
A downloadFile() 0 26 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Domains\Files\GetFiles;
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Log;
10
11
class DownloadController extends Controller
12
{
13
    public function index($fileID, $fileName)
14
    {
15
        $fileObj = new GetFiles;
16
        if(!$path = $fileObj->validateFile($fileID, $fileName))
17
        {
18
            abort(404, 'The file you are looking for cannot be found');
19
        }
20
21
        if(!$fileObj->canUserDownload($fileID, Auth::check()))
22
        {
23
            abort(403, 'You do not have permission to download this file');
24
        }
25
26
        $this->downloadFile(config('filesystems.disks.local.root').DIRECTORY_SEPARATOR.$path);
0 ignored issues
show
Bug introduced by
Are you sure $path of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        $this->downloadFile(config('filesystems.disks.local.root').DIRECTORY_SEPARATOR./** @scrutinizer ignore-type */ $path);
Loading history...
27
    }
28
29
    //  Download the file in chunks to allow for large file download
30
    protected function downloadFile($path)
31
    {
32
        $fileName = basename($path);
33
34
        //  Prepare header information for file download
35
        header('Content-Description:  File Transfer');
36
        // header('Content-Type:  '.$fileData->mime_type);
37
        header('Content-Type: application/octet-stream');
38
        header('Content-Disposition: attachment; filename='.basename($fileName));
39
        header('Content-Transfer-Encoding:  binary');
40
        header('Expires:  0');
41
        header('Cache-Control:  must-revalidate, post-check=0, pre-check=0');
42
        header('Pragma:  public');
43
        header('Content-Length:  '.filesize($path));
44
45
        //  Begin the file download.  File is broken into sections to better be handled by browser
46
        set_time_limit(0);
47
        $file = fopen($path, "rb");
48
        while(!feof($file))
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        while(!feof(/** @scrutinizer ignore-type */ $file))
Loading history...
49
        {
50
            print(@fread($file, 1024 * 8));
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
            print(@fread(/** @scrutinizer ignore-type */ $file, 1024 * 8));
Loading history...
51
            ob_flush();
52
            flush();
53
        }
54
55
        return true;
56
    }
57
}
58