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