Passed
Push — dev5a ( 328ab1...cf3a23 )
by Ron
07:38
created

DownloadController::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Domains\Files\GetFiles;
6
7
use App\Http\Controllers\Controller;
8
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\Log;
11
12
class DownloadController extends Controller
13
{
14
    protected $user;
15
16
    public function __construct()
17
    {
18
        $this->user = isset(Auth::user()->full_name) ? Auth::user()->full_name : \Request::ip();
19
    }
20
21
    public function index($fileID, $fileName)
22
    {
23
        $fileObj = new GetFiles;
24
        if(!$path = $fileObj->validateFile($fileID, $fileName))
25
        {
26
            Log::error('User '.$this->user.' is attempting to download a file that does not exist.  Details - ', ['file_id' => $fileID, 'file_name' => $fileName]);
27
            abort(404, 'The file you are looking for cannot be found');
28
        }
29
30
        if(!$fileObj->canUserDownload($fileID, Auth::check()))
31
        {
32
            Log::error('User '.$this->user.' is attempting to download a file that they do not have permission to download.  Details - ', ['file_id' => $fileID, 'file_name' => $fileName]);
33
            abort(403, 'You do not have permission to download this file');
34
        }
35
36
        Log::info('User '.$this->user.' has downloaded a file.  Details - ', ['file_id' => $fileID, 'file_name' => $fileName, 'file_path' => $path]);
37
38
        $this->downloadFile(config('filesystems.disks.local.root').DIRECTORY_SEPARATOR./** @scrutinizer ignore-type */$path);
39
    }
40
41
    //  Download the file in chunks to allow for large file download
42
    protected function downloadFile($path)
43
    {
44
        $fileName = basename($path);
45
46
        //  Prepare header information for file download
47
        header('Content-Description:  File Transfer');
48
        // header('Content-Type:  '.$fileData->mime_type);
49
        header('Content-Type: application/octet-stream');
50
        header('Content-Disposition: attachment; filename='.basename($fileName));
51
        header('Content-Transfer-Encoding:  binary');
52
        header('Expires:  0');
53
        header('Cache-Control:  must-revalidate, post-check=0, pre-check=0');
54
        header('Pragma:  public');
55
        header('Content-Length:  '.filesize($path));
56
57
        //  Begin the file download.  File is broken into sections to better be handled by browser
58
        set_time_limit(0);
59
        $file = fopen($path, "rb");
60
        while(!feof(/** @scrutinizer ignore-type */$file))
61
        {
62
            print(@fread(/** @scrutinizer ignore-type */$file, 1024 * 8));
63
            ob_flush();
64
            flush();
65
        }
66
67
        return true;
68
    }
69
}
70