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 App\Http\Resources\CustomerFileTypesCollection; |
20
|
|
|
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver; |
21
|
|
|
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory; |
22
|
|
|
use App\Http\Resources\FileLinks as FileLinksResource; |
23
|
|
|
|
24
|
|
|
class FileLinksController extends Controller |
25
|
|
|
{ |
26
|
|
|
// Only authorized users have access |
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
76 |
|
// Verify the user is logged in and has permissions for this page |
30
|
|
|
$this->middleware('auth'); |
31
|
|
|
$this->middleware(function($request, $next) { |
32
|
76 |
|
$this->user = auth()->user(); |
|
|
|
|
33
|
|
|
$this->authorize('hasAccess', 'Use File Links'); |
34
|
58 |
|
return $next($request); |
35
|
58 |
|
}); |
36
|
40 |
|
} |
37
|
76 |
|
|
38
|
76 |
|
// Landing page shows all links that the user owns |
39
|
|
|
public function index() |
40
|
|
|
{ |
41
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name); |
42
|
|
|
return view('links.index'); |
43
|
2 |
|
} |
44
|
2 |
|
|
45
|
|
|
// Ajax call to show the links for a specific user |
46
|
|
|
public function find($id) |
47
|
|
|
{ |
48
|
8 |
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name); |
49
|
|
|
|
50
|
8 |
|
// Verify if the user is trying to pull their own links |
51
|
|
|
if($id == 0) |
52
|
|
|
{ |
53
|
8 |
|
$id = Auth::user()->user_id; |
54
|
|
|
} |
55
|
2 |
|
// If the user is trying to pull someone elses links, they must be able to manage users |
56
|
|
|
else if($id != Auth::user()->user_id) |
57
|
|
|
{ |
58
|
6 |
|
$this->authorize('hasAccess', 'manage_users'); |
59
|
|
|
} |
60
|
4 |
|
|
61
|
|
|
$links = new FileLinksCollection( |
62
|
|
|
FileLinks::where('user_id', $id) |
63
|
6 |
|
->withCount('FileLinkFiles') |
64
|
6 |
|
->orderBy('expire', 'desc')->get() |
65
|
6 |
|
); |
66
|
6 |
|
|
67
|
|
|
Log::debug('File links for User ID - '.$id.': ', array($links)); |
68
|
|
|
|
69
|
6 |
|
return $links; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Create a new file link form |
73
|
2 |
|
public function create() |
74
|
|
|
{ |
75
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name); |
76
|
2 |
|
return view('links.newLink'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Submit the new file link form |
80
|
12 |
|
public function store(Request $request) |
81
|
|
|
{ |
82
|
12 |
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name.'. Submitted Data - ', $request->toArray()); |
83
|
|
|
|
84
|
12 |
|
$request->validate([ |
85
|
12 |
|
'name' => 'required', |
86
|
|
|
'expire' => 'required', |
87
|
|
|
'customerID' => 'exists:customers,cust_id|nullable' |
88
|
|
|
]); |
89
|
|
|
|
90
|
8 |
|
// If there are files, process them first in their chunks |
91
|
|
|
if(!empty($request->file)) |
92
|
2 |
|
{ |
93
|
|
|
$receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request)); |
94
|
|
|
|
95
|
2 |
|
// Recieve and process the file |
96
|
|
|
$save = $receiver->receive(); |
97
|
|
|
|
98
|
2 |
|
// See if the uploade has finished |
99
|
|
|
if($save->isFinished()) |
100
|
2 |
|
{ |
101
|
2 |
|
$this->saveFile($save->getFile()); |
102
|
|
|
return response()->json(['upload_success' => true]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Get the current progress if the file is not done being uploaded |
106
|
|
|
$handler = $save->handler(); |
107
|
|
|
|
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
|
8 |
|
// // If there are no files being uploaded or the file uploade process is done |
116
|
|
|
$linkID = $this->createLink($request); |
117
|
8 |
|
if($request->session()->has('newLinkFile')) |
118
|
8 |
|
{ |
119
|
|
|
// If there were files uploaded to the link, process them |
120
|
|
|
$files = session('newLinkFile'); |
121
|
2 |
|
$path = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$linkID; |
122
|
2 |
|
|
123
|
|
|
foreach($files as $file) |
124
|
2 |
|
{ |
125
|
|
|
$data = Files::find($file); |
126
|
2 |
|
// Move the file to the proper folder |
127
|
|
|
Storage::move($data->file_link.$data->file_name, $path.DIRECTORY_SEPARATOR.$data->file_name); |
128
|
2 |
|
// Update file link in DB |
129
|
|
|
$data->update([ |
130
|
2 |
|
'file_link' => $path.DIRECTORY_SEPARATOR |
131
|
2 |
|
]); |
132
|
|
|
// Attach file to the link |
133
|
|
|
FileLinkFiles::create([ |
134
|
2 |
|
'link_id' => $linkID, |
135
|
2 |
|
'file_id' => $data->file_id, |
136
|
2 |
|
'user_id' => Auth::user()->user_id, |
137
|
2 |
|
'upload' => 0 |
138
|
2 |
|
]); |
139
|
|
|
|
140
|
|
|
Log::info('File Added for link ID - '.$linkID.' by '.Auth::user()->full_name.'. File ID-'.$data->file_id); |
141
|
2 |
|
} |
142
|
|
|
|
143
|
|
|
$request->session()->forget('newLinkFile'); |
144
|
2 |
|
} |
145
|
|
|
|
146
|
|
|
return response()->json(['link' => $linkID, 'name' => Str::slug($request->name)]); |
147
|
8 |
|
} |
148
|
|
|
|
149
|
|
|
// Create the new file link |
150
|
|
|
private function createLink($data) |
151
|
|
|
{ |
152
|
|
|
// Generate a random hash to use as the file link and make sure it is not already in use |
153
|
|
|
do |
154
|
8 |
|
{ |
155
|
|
|
$hash = strtolower(Str::random(15)); |
156
|
8 |
|
$dup = FileLinks::where('link_hash', $hash)->get()->count(); |
157
|
|
|
Log::debug('New possible link hash - '.$hash.'. Is duplicate -> '.$dup == 0 ? 'false' : 'true'); |
158
|
|
|
} while($dup != 0); |
159
|
|
|
|
160
|
|
|
// Create the new file link |
161
|
8 |
|
$link = FileLinks::create([ |
162
|
8 |
|
'user_id' => Auth::user()->user_id, |
163
|
8 |
|
'cust_id' => $data->customerID, |
164
|
|
|
'link_hash' => $hash, |
165
|
|
|
'link_name' => $data->name, |
166
|
8 |
|
'expire' => $data->expire, |
167
|
8 |
|
'allow_upload' => isset($data->allowUp) && $data->allowUp ? true : false, |
168
|
8 |
|
'note' => $data->instructions |
169
|
8 |
|
]); |
170
|
8 |
|
|
171
|
8 |
|
Log::info('File Link Created for '.Auth::user()->full_name.'. Link Data - ', $link->toArray()); |
172
|
8 |
|
|
173
|
8 |
|
return $link->link_id; |
174
|
|
|
} |
175
|
|
|
|
176
|
8 |
|
// Save a file attached to the link |
177
|
|
|
private function saveFile(UploadedFile $file) |
178
|
8 |
|
{ |
179
|
|
|
Log::debug('Preparing to save file. File Data - ', array($file)); |
180
|
|
|
$filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.'_tmp'; |
181
|
|
|
Log:debug('File will be saved to '.$filePath); |
182
|
2 |
|
|
183
|
|
|
// Clean the file and store it |
184
|
2 |
|
$fileName = Files::cleanFilename($filePath, $file->getClientOriginalName()); |
185
|
|
|
$file->storeAs($filePath, $fileName); |
186
|
2 |
|
|
187
|
|
|
// Place file in Files table of DB |
188
|
|
|
$newFile = Files::create([ |
189
|
2 |
|
'file_name' => $fileName, |
190
|
2 |
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
191
|
|
|
]); |
192
|
|
|
$fileID = $newFile->file_id; |
193
|
2 |
|
|
194
|
2 |
|
// Save the file ID in the session array |
195
|
2 |
|
$fileArr = session('newLinkFile') != null ? session('newLinkFile') : []; |
196
|
|
|
$fileArr[] = $fileID; |
197
|
2 |
|
session(['newLinkFile' => $fileArr]); |
198
|
|
|
|
199
|
|
|
// Log stored file |
200
|
2 |
|
Log::info('File Stored by '.Auth::user()->full_name, ['file_id' => $fileID, 'file_path' => $filePath.DIRECTORY_SEPARATOR, 'file_name' => $fileName]); |
201
|
2 |
|
return $fileID; |
202
|
2 |
|
} |
203
|
|
|
|
204
|
|
|
// Show details about a file link |
205
|
2 |
|
public function details($id, /** @scrutinizer ignore-unused */ $name) |
206
|
2 |
|
{ |
207
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name); |
208
|
|
|
|
209
|
|
|
// Verify that the link is a valid link |
210
|
4 |
|
$linkData = FileLinks::find($id); |
211
|
|
|
$fileTypes = new CustomerFileTypesCollection(CustomerFileTypes::all()); |
212
|
4 |
|
|
213
|
|
|
// If the link is invalid, return an error page |
214
|
|
|
if(empty($linkData)) |
215
|
4 |
|
{ |
216
|
4 |
|
Log::warning('User '.Auth::user()->full_name.' tried to view invalid file link', ['user_id' => Auth::user()->user_id, 'link_id' => $id]); |
217
|
|
|
return view('links.badLink'); |
218
|
|
|
} |
219
|
4 |
|
|
220
|
|
|
Log::debug('Link Data Gathered - ', $linkData->toArray()); |
221
|
2 |
|
return view('links.details', [ |
222
|
2 |
|
'link_id' => $linkData->link_id, |
223
|
|
|
'cust_id' => $linkData->cust_id, |
224
|
|
|
'file_types' => $fileTypes |
225
|
2 |
|
]); |
226
|
2 |
|
} |
227
|
2 |
|
|
228
|
2 |
|
// Ajax call te get JSON details of the link |
229
|
|
|
public function show($id) |
230
|
|
|
{ |
231
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name); |
232
|
|
|
$linkData = new FileLinksResource(FileLinks::find($id)); |
233
|
2 |
|
Log::debug('Link Data Gathered - ', array($linkData)); |
234
|
|
|
return $linkData; |
235
|
2 |
|
} |
236
|
|
|
|
237
|
2 |
|
// Update the link's details |
238
|
2 |
|
public function update(Request $request, $id) |
239
|
|
|
{ |
240
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name.'. Submitted Data - ', $request->toArray()); |
241
|
|
|
|
242
|
6 |
|
$request->validate([ |
243
|
|
|
'name' => 'required', |
244
|
6 |
|
'expire' => 'required', |
245
|
|
|
'customerTag' => 'exists:customers,cust_id|nullable' |
246
|
6 |
|
]); |
247
|
6 |
|
|
248
|
|
|
FileLinks::find($id)->update([ |
249
|
|
|
'link_name' => $request->name, |
250
|
|
|
'expire' => $request->expire, |
251
|
|
|
'allow_upload' => $request->allowUp, |
252
|
2 |
|
'cust_id' => $request->customerID, |
253
|
2 |
|
]); |
254
|
2 |
|
|
255
|
2 |
|
Log::info('File Link Updated by '.Auth::user()->full_name, ['link_id' => $id]); |
256
|
2 |
|
return response()->json(['success' => true]); |
257
|
2 |
|
} |
258
|
|
|
|
259
|
|
|
// Retrieve the instructions attached to a link |
260
|
2 |
|
public function getInstructions($linkID) |
261
|
|
|
{ |
262
|
2 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name); |
263
|
|
|
|
264
|
|
|
$linkData = FileLinks::select('note')->where('link_id', $linkID)->first(); |
265
|
|
|
Log::debug('Link Instructions Gathered - ', array($linkData)); |
266
|
2 |
|
|
267
|
|
|
return $linkData; |
268
|
|
|
} |
269
|
2 |
|
|
270
|
2 |
|
// Update the instructions attached to the link |
271
|
|
|
public function submitInstructions(Request $request, $linkID) |
272
|
|
|
{ |
273
|
2 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name . '. Submitted Data - ', $request->toArray()); |
274
|
2 |
|
|
275
|
2 |
|
FileLinks::find($linkID)->update([ |
276
|
|
|
'note' => $request->instructions, |
277
|
|
|
]); |
278
|
|
|
|
279
|
2 |
|
return response()->json(['success' => true]); |
280
|
|
|
} |
281
|
|
|
|
282
|
2 |
|
// Disable a file linke, but do not remove it (set the expire date to a previous date) |
283
|
2 |
|
public function disableLink($id) |
284
|
|
|
{ |
285
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name); |
286
|
|
|
|
287
|
2 |
|
// update the expire date for the link |
288
|
2 |
|
FileLinks::find($id)->update([ |
289
|
|
|
'expire' => Carbon::yesterday() |
290
|
|
|
]); |
291
|
2 |
|
|
292
|
|
|
Log::info('User '.Auth::user()->full_name.' disabled link ID - '.$id); |
293
|
|
|
return response()->json(['success' => true]); |
294
|
|
|
} |
295
|
2 |
|
|
296
|
|
|
// Delete a file link |
297
|
2 |
|
public function destroy($id) |
298
|
2 |
|
{ |
299
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name); |
300
|
2 |
|
|
301
|
2 |
|
// Remove any files from database |
302
|
|
|
$data = FileLinkFiles::where('link_id', $id)->get(); |
303
|
|
|
if(!$data->isEmpty()) |
304
|
|
|
{ |
305
|
|
|
foreach($data as $file) |
306
|
|
|
{ |
307
|
|
|
$fileID = $file->file_id; |
308
|
|
|
$file->delete(); |
309
|
|
|
Log::debug('File link file ID '.$file->file_id.' deleted from Link ID '.$id); |
310
|
|
|
|
311
|
|
|
// Delete the file if it is no longer in use |
312
|
|
|
Files::deleteFile($fileID); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
FileLinks::find($id)->delete(); |
317
|
|
|
|
318
|
|
|
Log::info('File link ID - '.$id.' deleted by '.Auth::user()->full_name); |
319
|
|
|
|
320
|
|
|
return response()->json([ |
321
|
|
|
'success' => true |
322
|
|
|
]); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|