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

DownloadController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 56
ccs 0
cts 31
cp 0
rs 10
c 3
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 18 3
A __construct() 0 3 2
A downloadFile() 0 26 2
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