|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use PDF; |
|
6
|
|
|
use Zip; |
|
7
|
|
|
use App\Files; |
|
8
|
|
|
use App\TechTips; |
|
9
|
|
|
use App\Customers; |
|
10
|
|
|
use Carbon\Carbon; |
|
11
|
|
|
use App\CustomerNotes; |
|
12
|
|
|
use App\Domains\DownloadDomain; |
|
13
|
|
|
use App\Http\Requests\DownloadArchiveRequest; |
|
14
|
|
|
use Illuminate\Http\Request; |
|
15
|
|
|
use Illuminate\Support\Facades\Log; |
|
16
|
|
|
use Illuminate\Support\Facades\Auth; |
|
17
|
|
|
use Illuminate\Support\Facades\Route; |
|
18
|
|
|
use Illuminate\Support\Facades\Storage; |
|
19
|
|
|
use Illuminate\Support\Facades\Response; |
|
20
|
|
|
use Illuminate\Http\File; |
|
21
|
|
|
|
|
22
|
|
|
class DownloadController extends Controller |
|
23
|
|
|
{ |
|
24
|
|
|
// File locations for stored files |
|
25
|
|
|
protected $tmpFolder = 'archive_downloads'.DIRECTORY_SEPARATOR; |
|
26
|
|
|
protected $root; |
|
27
|
|
|
protected $path; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->root = config('filesystems.disks.local.root').DIRECTORY_SEPARATOR; |
|
32
|
|
|
$this->path = $this->root.$this->tmpFolder; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// Download one file |
|
36
|
|
|
public function index($fileID, $fileName) |
|
37
|
|
|
{ |
|
38
|
|
|
$fileObj = new DownloadDomain; |
|
39
|
|
|
$fileObj->setFileDetails($fileID, $fileName); |
|
40
|
|
|
|
|
41
|
|
|
if($fileObj->isFileValid() && $fileObj->canUserDownload()) |
|
42
|
|
|
{ |
|
43
|
|
|
$path = config('filesystems.disks.local.root').$fileObj->getFileLinkForDownload(); |
|
44
|
|
|
if(!$this->downloadFile($path)) |
|
45
|
|
|
{ |
|
46
|
|
|
abort(500); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
exit; |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return view('err.badFile'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Create an archive of files and download them |
|
56
|
|
|
public function archive(DownloadArchiveRequest $request) |
|
57
|
|
|
{ |
|
58
|
|
|
$fileObj = new DownloadDomain; |
|
59
|
|
|
$archive = $fileObj->createArchive($request->fileList); |
|
60
|
|
|
|
|
61
|
|
|
return response()->json(['archive' => $archive]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// Download multiple files as part of a zip archive that was put together |
|
65
|
|
|
public function downloadArchive($fileName) |
|
66
|
|
|
{ |
|
67
|
|
|
$fileObj = new DownloadDomain; |
|
68
|
|
|
if($aboslutePath = $fileObj->validateArchive($fileName)) |
|
69
|
|
|
{ |
|
70
|
|
|
if(!$this->downloadFile($aboslutePath)) |
|
71
|
|
|
{ |
|
72
|
|
|
abort(500); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$fileObj->deleteArchive($fileName); |
|
76
|
|
|
exit; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return view('err.badFile'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
// Download Customer Note as PDF |
|
88
|
|
|
public function downloadCustNote($id) |
|
89
|
|
|
{ |
|
90
|
|
|
// Debug Data |
|
91
|
|
|
$this->middleware('auth'); |
|
92
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name); |
|
93
|
|
|
|
|
94
|
|
|
$note = CustomerNotes::find($id); |
|
95
|
|
|
$cust = Customers::find($note->cust_id); |
|
96
|
|
|
|
|
97
|
|
|
$pdf = PDF::loadView('pdf.customerNote', [ |
|
98
|
|
|
'cust_name' => $cust->name, |
|
99
|
|
|
'note_subj' => $note->subject, |
|
100
|
|
|
'description' => $note->description |
|
101
|
|
|
]); |
|
102
|
|
|
|
|
103
|
|
|
Log::info('Customer note downloaded by '.Auth::user()->full_name.'. Data: ', ['Customer ID: ' => $cust->id, 'Customer Name: ' => $cust->name, 'Note ID: ' => $id, 'Note Subject: ' => $note->subject]); |
|
104
|
|
|
return $pdf->download($cust->name.' - Note: '.$note->subject.'.pdf'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// Download Tech Tip as PDF |
|
108
|
|
|
public function downloadTechTip($id) |
|
109
|
|
|
{ |
|
110
|
|
|
// Debug Data |
|
111
|
|
|
$this->middleware('auth'); |
|
112
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name); |
|
113
|
|
|
|
|
114
|
|
|
// TODO - Makt this a better looking pdf |
|
115
|
|
|
$tip = TechTips::where('tip_id', $id)->with('User')->with('SystemTypes')->first(); |
|
116
|
|
|
|
|
117
|
|
|
$pdf = PDF::loadView('pdf.techTip', [ |
|
118
|
|
|
'data' => $tip, |
|
119
|
|
|
'comments' => collect([]) |
|
120
|
|
|
]); |
|
121
|
|
|
|
|
122
|
|
|
Log::info('Tech Tip downloaded as PDF by '.Auth::user()->full_name.'. Data: ', ['Tip ID: ' => $tip->tip_id, 'Subject: ' => $tip->subject]); |
|
123
|
|
|
return $pdf->download('Tech Tip - '.$tip->subject.'.pdf'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
// Download the file in chunks to allow for large file download |
|
133
|
|
|
protected function downloadFile($path) |
|
134
|
|
|
{ |
|
135
|
|
|
$fileName = basename($path); |
|
136
|
|
|
|
|
137
|
|
|
// Prepare header information for file download |
|
138
|
|
|
header('Content-Description: File Transfer'); |
|
139
|
|
|
// header('Content-Type: '.$fileData->mime_type); |
|
140
|
|
|
header('Content-Type: application/octet-stream'); |
|
141
|
|
|
header('Content-Disposition: attachment; filename='.basename($fileName)); |
|
142
|
|
|
header('Content-Transfer-Encoding: binary'); |
|
143
|
|
|
header('Expires: 0'); |
|
144
|
|
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
|
145
|
|
|
header('Pragma: public'); |
|
146
|
|
|
header('Content-Length: '.filesize($path)); |
|
147
|
|
|
|
|
148
|
|
|
// Begin the file download. File is broken into sections to better be handled by browser |
|
149
|
|
|
set_time_limit(0); |
|
150
|
|
|
$file = fopen($path, "rb"); |
|
151
|
|
|
while(!feof($file)) |
|
|
|
|
|
|
152
|
|
|
{ |
|
153
|
|
|
print(@fread($file, 1024 * 8)); |
|
|
|
|
|
|
154
|
|
|
ob_flush(); |
|
155
|
|
|
flush(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return true; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.