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 Illuminate\Http\Request; |
13
|
|
|
use Illuminate\Support\Facades\Log; |
14
|
|
|
use Illuminate\Support\Facades\Auth; |
15
|
|
|
use Illuminate\Support\Facades\Route; |
16
|
|
|
use Illuminate\Support\Facades\Storage; |
17
|
|
|
|
18
|
|
|
// use ZanySoft\Zip; |
19
|
|
|
|
20
|
|
|
class DownloadController extends Controller |
21
|
|
|
{ |
22
|
|
|
// File locations for stored files |
23
|
|
|
private $tmpFolder = 'archive_downloads'.DIRECTORY_SEPARATOR; |
24
|
|
|
private $root; |
25
|
|
|
private $path; |
26
|
|
|
|
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->root = config('filesystems.disks.local.root').DIRECTORY_SEPARATOR; |
30
|
|
|
$this->path = $this->root.$this->tmpFolder; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Download one file |
34
|
|
|
public function index($fileID, $fileName) |
35
|
|
|
{ |
36
|
|
|
$fileData = Files::where('file_id', $fileID)->where('file_name', $fileName)->first(); |
37
|
|
|
// Check that the file exists before allowing it to be downloaded |
38
|
|
|
if(!empty($fileData) && Storage::exists($fileData->file_link.$fileData->file_name)) |
39
|
|
|
{ |
40
|
|
|
Log::info('File Downloaded', ['file_id' => $fileID]); |
41
|
|
|
return Storage::download($fileData->file_link.$fileData->file_name); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
45
|
|
|
Log::notice('File Not Found', ['file_id' => $fileID, 'file_name' => $fileName]); |
46
|
|
|
return view('err.badFile'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Package multiple files and prepare them for download |
50
|
|
|
public function archiveFiles(Request $request) |
51
|
|
|
{ |
52
|
|
|
// Validate the array |
53
|
|
|
$request->validate([ |
54
|
|
|
'fileList' => 'required' |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
// Filename of zip archive |
58
|
|
|
$name = 'zip_archive_'.Carbon::now()->timestamp.'.zip'; |
59
|
|
|
|
60
|
|
|
// The archive_downloads directory does not exist by default. Touching a file in it ensures the directory is created and usable |
61
|
|
|
Storage::put($this->tmpFolder.'.ignore', ''); |
62
|
|
|
|
63
|
|
|
// Create the zip file |
64
|
|
|
$zip = Zip::create($this->path.$name); |
65
|
|
|
foreach($request->fileList as $file) |
66
|
|
|
{ |
67
|
|
|
// Place each file inside of the zip |
68
|
|
|
$data = Files::find($file); |
69
|
|
|
if($data->count() > 0) |
70
|
|
|
{ |
71
|
|
|
// Log::debug('file exists', $data->toArray()); |
72
|
|
|
$zip->add($this->root.$data->file_link.$data->file_name); |
73
|
|
|
Log::debug('File Added - '.$this->root.$data->file_link.$data->file_name); |
74
|
|
|
} |
75
|
|
|
else |
76
|
|
|
{ |
77
|
|
|
Log::notice('User tried to download an empty zip archive.'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
// Close zip file to be processed |
81
|
|
|
$zip->close(); |
82
|
|
|
|
83
|
|
|
// Return the name of the zip file |
84
|
|
|
return response()->json(['archive' => $name]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Download multiple files as part of a zip archive that was put together |
88
|
|
|
public function downloadArchive($fileName) |
89
|
|
|
{ |
90
|
|
|
// Check if the requested archive exists |
91
|
|
|
if(Storage::exists($this->tmpFolder.$fileName)) |
92
|
|
|
{ |
93
|
|
|
Log::info('Archive Downloaded - '.$fileName); |
94
|
|
|
// return Storage::download($this->tmpFolder.$fileName); |
95
|
|
|
return response()->download($this->path.$fileName, 'download.zip')->deleteFileAfterSend(true); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
Log::notice('Archive Not Found - '.$fileName); |
99
|
|
|
return view('err.badFile'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Download Customer Note as PDF |
103
|
|
|
public function downloadCustNote($id) |
104
|
|
|
{ |
105
|
|
|
$note = CustomerNotes::find($id); |
106
|
|
|
$cust = Customers::find($note->cust_id); |
107
|
|
|
|
108
|
|
|
$pdf = PDF::loadView('pdf.customerNote', [ |
109
|
|
|
'cust_name' => $cust->name, |
110
|
|
|
'note_subj' => $note->subject, |
111
|
|
|
'description' => $note->description |
112
|
|
|
]); |
113
|
|
|
|
114
|
|
|
return $pdf->download($cust->name.' - Note: '.$note->subject.'.pdf'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Download Tech Tip as PDF |
118
|
|
|
public function downloadTechTip($id) |
119
|
|
|
{ |
120
|
|
|
// TODO - Makt this a better looking pdf |
121
|
|
|
$tip = TechTips::where('tip_id', $id)->with('User')->with('SystemTypes')->first(); |
122
|
|
|
|
123
|
|
|
$pdf = PDF::loadView('pdf.techTip', [ |
124
|
|
|
'data' => $tip, |
125
|
|
|
'comments' => collect([]) |
126
|
|
|
]); |
127
|
|
|
|
128
|
|
|
return $pdf->download('Tech Tip - '.$tip->subject.'.pdf'); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|