|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\FileLinks; |
|
4
|
|
|
|
|
5
|
|
|
use App\Files; |
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
use App\FileLinks; |
|
8
|
|
|
use App\FileLinkFiles; |
|
9
|
|
|
use App\CustomerFileTypes; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
use Illuminate\Http\Request; |
|
12
|
|
|
use Illuminate\Http\UploadedFile; |
|
13
|
|
|
use Illuminate\Support\Facades\Log; |
|
14
|
|
|
use Illuminate\Support\Facades\Auth; |
|
15
|
|
|
use App\Http\Controllers\Controller; |
|
16
|
|
|
use Illuminate\Support\Facades\Route; |
|
17
|
|
|
use Illuminate\Support\Facades\Storage; |
|
18
|
|
|
use App\Http\Resources\FileLinksCollection; |
|
19
|
|
|
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver; |
|
20
|
|
|
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory; |
|
21
|
|
|
use Pion\Laravel\ChunkUpload\Handler\AbstractHandler; |
|
22
|
|
|
use App\Http\Resources\FileLinks as FileLinksResource; |
|
23
|
|
|
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException; |
|
24
|
|
|
|
|
25
|
|
|
class FileLinksController extends Controller |
|
26
|
|
|
{ |
|
27
|
|
|
// Only authorized users have access |
|
28
|
|
|
public function __construct(\Gate $gate) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
// Verify the user is logged in and has permissions for this page |
|
31
|
|
|
$this->middleware('auth'); |
|
32
|
|
|
$this->middleware(function ($request, $next) { |
|
33
|
|
|
$this->user = auth()->user(); |
|
|
|
|
|
|
34
|
|
|
$this->authorize('hasAccess', 'use_file_links'); |
|
35
|
|
|
return $next($request); |
|
36
|
|
|
}); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// Landing page shows all links that the user owns |
|
40
|
|
|
public function index() |
|
41
|
|
|
{ |
|
42
|
|
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by User ID-' . Auth::user()->user_id); |
|
43
|
|
|
return view('links.index'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Ajax call to show the links for a specific user |
|
47
|
|
|
public function find($id) |
|
48
|
|
|
{ |
|
49
|
|
|
if($id == 0) |
|
50
|
|
|
{ |
|
51
|
|
|
$id = Auth::user()->user_id; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
55
|
|
|
$links = new FileLinksCollection( |
|
56
|
|
|
FileLinks::where('user_id', $id) |
|
57
|
|
|
->withCount('FileLinkFiles') |
|
58
|
|
|
->orderBy('expire', 'desc')->get() |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
// Log::debug('blah', $links->toArray($links)); |
|
62
|
|
|
|
|
63
|
|
|
return $links; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Create a new file link form |
|
67
|
|
|
public function create() |
|
68
|
|
|
{ |
|
69
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
70
|
|
|
return view('links.newLink'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Submit the new file link form |
|
74
|
|
|
public function store(Request $request) |
|
75
|
|
|
{ |
|
76
|
|
|
$request->validate([ |
|
77
|
|
|
'name' => 'required', |
|
78
|
|
|
'expire' => 'required', |
|
79
|
|
|
'customerID' => 'exists:customers,cust_id|nullable' |
|
80
|
|
|
]); |
|
81
|
|
|
|
|
82
|
|
|
if(!empty($request->file)) |
|
83
|
|
|
{ |
|
84
|
|
|
$receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request)); |
|
85
|
|
|
|
|
86
|
|
|
// Verify that the upload is valid and being processed |
|
87
|
|
|
if($receiver->isUploaded() === false) |
|
88
|
|
|
{ |
|
89
|
|
|
Log::error('Upload File Missing - '.$request->toArray()); |
|
90
|
|
|
throw new UploadMissingFileException(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// Recieve and process the file |
|
94
|
|
|
$save = $receiver->receive(); |
|
95
|
|
|
|
|
96
|
|
|
// See if the uploade has finished |
|
97
|
|
|
if($save->isFinished()) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->saveFile($save->getFile()); |
|
100
|
|
|
|
|
101
|
|
|
return 'uploaded successfully'; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Get the current progress |
|
105
|
|
|
$handler = $save->handler(); |
|
106
|
|
|
|
|
107
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
108
|
|
|
Log::debug('File being uploaded. Percentage done - '.$handler->getPercentageDone()); |
|
109
|
|
|
return response()->json([ |
|
110
|
|
|
'done' => $handler->getPercentageDone(), |
|
111
|
|
|
'status' => true |
|
112
|
|
|
]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// If there are no files being uploaded or the file uploade process is done |
|
116
|
|
|
if(isset($request->_completed) && $request->_completed) |
|
117
|
|
|
{ |
|
118
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
119
|
|
|
Log::debug('Link data submitted. Data - ', $request->toArray()); |
|
120
|
|
|
$linkID = $this->createLink($request); |
|
121
|
|
|
if($request->session()->has('newLinkFile')) |
|
122
|
|
|
{ |
|
123
|
|
|
// If there were files uploaded to the link, process them |
|
124
|
|
|
$files = session('newLinkFile'); |
|
125
|
|
|
$path = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$linkID; |
|
126
|
|
|
|
|
127
|
|
|
foreach($files as $file) |
|
128
|
|
|
{ |
|
129
|
|
|
$data = Files::find($file); |
|
130
|
|
|
// Move the file to the proper folder |
|
131
|
|
|
Storage::move($data->file_link.$data->file_name, $path.DIRECTORY_SEPARATOR.$data->file_name); |
|
132
|
|
|
// Update file link in DB |
|
133
|
|
|
$data->update([ |
|
134
|
|
|
'file_link' => $path.DIRECTORY_SEPARATOR |
|
135
|
|
|
]); |
|
136
|
|
|
// Attach file to the link |
|
137
|
|
|
FileLinkFiles::create([ |
|
138
|
|
|
'link_id' => $linkID, |
|
139
|
|
|
'file_id' => $data->file_id, |
|
140
|
|
|
'user_id' => Auth::user()->user_id, |
|
141
|
|
|
'upload' => 0 |
|
142
|
|
|
]); |
|
143
|
|
|
|
|
144
|
|
|
Log::debug('File Added for link ID-'.$linkID.'. File ID-'.$data->file_id); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$request->session()->forget('newLinkFile'); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return response()->json(['link' => $linkID, 'name' => Str::slug($request->name)]); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return response()->json(['complete' => false]); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
// Create the new file link |
|
157
|
|
|
private function createLink($data) |
|
158
|
|
|
{ |
|
159
|
|
|
// Generate a random hash to use as the file link and make sure it is not already in use |
|
160
|
|
|
do |
|
161
|
|
|
{ |
|
162
|
|
|
$hash = strtolower(Str::random(15)); |
|
163
|
|
|
$dup = FileLinks::where('link_hash', $hash)->get()->count(); |
|
164
|
|
|
} while($dup != 0); |
|
165
|
|
|
|
|
166
|
|
|
// Create the new file link |
|
167
|
|
|
$link = FileLinks::create([ |
|
168
|
|
|
'user_id' => Auth::user()->user_id, |
|
169
|
|
|
'cust_id' => $data->customerID, |
|
170
|
|
|
'link_hash' => $hash, |
|
171
|
|
|
'link_name' => $data->name, |
|
172
|
|
|
'expire' => $data->expire, |
|
173
|
|
|
'allow_upload' => isset($data->allowUp) && $data->allowUp ? true : false |
|
174
|
|
|
]); |
|
175
|
|
|
|
|
176
|
|
|
Log::info('File Link Created for User ID-'.Auth::user()->user_id.'. Link ID-'.$link->link_id); |
|
177
|
|
|
|
|
178
|
|
|
return $link->link_id; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
// Save a file attached to the link |
|
182
|
|
|
private function saveFile(UploadedFile $file) |
|
183
|
|
|
{ |
|
184
|
|
|
$filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.'_tmp'; |
|
185
|
|
|
|
|
186
|
|
|
// Clean the file and store it |
|
187
|
|
|
$fileName = Files::cleanFilename($filePath, $file->getClientOriginalName()); |
|
188
|
|
|
$file->storeAs($filePath, $fileName); |
|
189
|
|
|
|
|
190
|
|
|
// Place file in Files table of DB |
|
191
|
|
|
$newFile = Files::create([ |
|
192
|
|
|
'file_name' => $fileName, |
|
193
|
|
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
|
194
|
|
|
]); |
|
195
|
|
|
$fileID = $newFile->file_id; |
|
196
|
|
|
|
|
197
|
|
|
// Save the file ID in the session array |
|
198
|
|
|
$fileArr = session('newLinkFile') != null ? session('newLinkFile') : []; |
|
199
|
|
|
$fileArr[] = $fileID; |
|
200
|
|
|
session(['newLinkFile' => $fileArr]); |
|
201
|
|
|
|
|
202
|
|
|
// Log stored file |
|
203
|
|
|
Log::info('File Stored', ['file_id' => $fileID, 'file_path' => $filePath.DIRECTORY_SEPARATOR.$fileName]); |
|
204
|
|
|
return $fileID; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
// Show details about a file link |
|
225
|
|
|
public function details($id, $name) |
|
|
|
|
|
|
226
|
|
|
{ |
|
227
|
|
|
// Verify that the link is a valid link |
|
228
|
|
|
$linkData = FileLinks::find($id); |
|
229
|
|
|
|
|
230
|
|
|
// If the link is invalid, return an error page |
|
231
|
|
|
if(empty($linkData)) |
|
232
|
|
|
{ |
|
233
|
|
|
Log::warning('User tried to view bad file link', ['user_id' => Auth::user()->user_id, 'link_id' => $id]); |
|
234
|
|
|
return view('links.badLink'); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
// Determine if the link has a customer attached or not |
|
238
|
|
|
// $hasCust = $linkData->cust_id != null ? true : false; |
|
239
|
|
|
|
|
240
|
|
|
// Get the possible file types for attaching a file to a customer |
|
241
|
|
|
// $fileTypes = CustomerFileTypes::all(); |
|
242
|
|
|
|
|
243
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
244
|
|
|
Log::debug('Link Detials - ', $linkData->toArray()); |
|
245
|
|
|
return view('links.details', [ |
|
246
|
|
|
'link_id' => $linkData->link_id, |
|
247
|
|
|
// 'has_cust' => $hasCust, |
|
248
|
|
|
// 'file_types' => $fileTypes |
|
249
|
|
|
]); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
// Ajax call te get JSON details of the link |
|
253
|
|
|
public function show($id) |
|
254
|
|
|
{ |
|
255
|
|
|
$linkData = new FileLinksResource(FileLinks::find($id)); |
|
256
|
|
|
|
|
257
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
258
|
|
|
// Log::debug('Link Details - ', $linkData->toArray()); |
|
259
|
|
|
return $linkData; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
|
|
263
|
|
|
|
|
264
|
|
|
|
|
265
|
|
|
|
|
266
|
|
|
|
|
267
|
|
|
|
|
268
|
|
|
|
|
269
|
|
|
|
|
270
|
|
|
|
|
271
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
|
|
277
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
// Update the link's details |
|
280
|
|
|
public function update(Request $request, $id) |
|
281
|
|
|
{ |
|
282
|
|
|
$request->validate([ |
|
283
|
|
|
'name' => 'required', |
|
284
|
|
|
'expire' => 'required', |
|
285
|
|
|
'customerTag' => 'exists:customers,cust_id|nullable' |
|
286
|
|
|
]); |
|
287
|
|
|
|
|
288
|
|
|
FileLinks::find($id)->update([ |
|
289
|
|
|
'link_name' => $request->name, |
|
290
|
|
|
'expire' => $request->expire, |
|
291
|
|
|
'allow_upload' => isset($request->allowUpload) && $request->allowUpload ? true : false, |
|
292
|
|
|
'cust_id' => $request->customerTag |
|
293
|
|
|
]); |
|
294
|
|
|
|
|
295
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
296
|
|
|
Log::debug('Updated Link Data for link ID-'.$id, $request->toArray()); |
|
297
|
|
|
Log::info('File Link Updated', ['link_id' => $id]); |
|
298
|
|
|
|
|
299
|
|
|
return response()->json(['success' => true]); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
// Update the customer that is attached to the customer |
|
303
|
|
|
// public function updateCustomer(Request $request, $id) |
|
304
|
|
|
// { |
|
305
|
|
|
// // If the "customer id" field is populated, separate the ID from the name and prepare for insertion. |
|
306
|
|
|
// if($request->customer_tag != null && $request->customer_tag != 'NULL') |
|
307
|
|
|
// { |
|
308
|
|
|
// $custID = explode(' ', $request->customer_tag); |
|
309
|
|
|
// $custID = $custID[0]; |
|
310
|
|
|
// } |
|
311
|
|
|
// else |
|
312
|
|
|
// { |
|
313
|
|
|
// $custID = null; |
|
314
|
|
|
// } |
|
315
|
|
|
|
|
316
|
|
|
// FileLinks::find($id)->update([ |
|
317
|
|
|
// 'cust_id' => $custID |
|
318
|
|
|
// ]); |
|
319
|
|
|
|
|
320
|
|
|
// Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
321
|
|
|
// Log::debug('Submitted Data -', $request->toArray()); |
|
322
|
|
|
// return response()->json(['success' => true]); |
|
323
|
|
|
// } |
|
324
|
|
|
|
|
325
|
|
|
|
|
326
|
|
|
|
|
327
|
|
|
|
|
328
|
|
|
|
|
329
|
|
|
|
|
330
|
|
|
|
|
331
|
|
|
|
|
332
|
|
|
|
|
333
|
|
|
|
|
334
|
|
|
|
|
335
|
|
|
|
|
336
|
|
|
|
|
337
|
|
|
|
|
338
|
|
|
|
|
339
|
|
|
|
|
340
|
|
|
|
|
341
|
|
|
|
|
342
|
|
|
|
|
343
|
|
|
|
|
344
|
|
|
// Disable a file linke, but do not remove it (set the expire date to a previous date) |
|
345
|
|
|
public function disableLink($id) |
|
346
|
|
|
{ |
|
347
|
|
|
// update the expire date for the link |
|
348
|
|
|
FileLinks::find($id)->update([ |
|
349
|
|
|
'expire' => Carbon::yesterday() |
|
350
|
|
|
]); |
|
351
|
|
|
|
|
352
|
|
|
Log::info('User ID '.Auth::user()->user_id.' disabled link ID - '.$id); |
|
353
|
|
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by User ID-' . Auth::user()->user_id); |
|
354
|
|
|
return response()->json(['success' => true]); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
// Delete a file link |
|
358
|
|
|
public function destroy($id) |
|
359
|
|
|
{ |
|
360
|
|
|
// Remove the file from database |
|
361
|
|
|
$data = FileLinkFiles::where('link_id', $id)->get(); |
|
362
|
|
|
if(!$data->isEmpty()) |
|
363
|
|
|
{ |
|
364
|
|
|
foreach($data as $file) |
|
365
|
|
|
{ |
|
366
|
|
|
$fileID = $file->file_id; |
|
367
|
|
|
$file->delete(); |
|
368
|
|
|
|
|
369
|
|
|
// Delete the file if it is no longer in use |
|
370
|
|
|
Files::deleteFile($fileID); |
|
371
|
|
|
} |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
FileLinks::find($id)->delete(); |
|
375
|
|
|
|
|
376
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
|
377
|
|
|
Log::info('File link deleted', ['link_id' => $id, 'user_id' => Auth::user()->user_id]); |
|
378
|
|
|
|
|
379
|
|
|
return response()->json([ |
|
380
|
|
|
'success' => true |
|
381
|
|
|
]); |
|
382
|
|
|
} |
|
383
|
|
|
} |
|
384
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.