|
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); |
|
|
|
|
|
|
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)) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
print(@fread($file, 1024 * 8)); |
|
|
|
|
|
|
51
|
|
|
ob_flush(); |
|
52
|
|
|
flush(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|