| Total Complexity | 14 |
| Total Lines | 145 |
| Duplicated Lines | 0 % |
| Coverage | 90.41% |
| Changes | 0 | ||
| 1 | <?php |
||
| 21 | class GuestLinksController extends Controller |
||
| 22 | { |
||
| 23 | // Landing page if no link is sent |
||
| 24 | 2 | public function index() |
|
| 25 | { |
||
| 26 | 2 | Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip()); |
|
| 27 | 2 | return view('links.guestIndex'); |
|
| 28 | } |
||
| 29 | |||
| 30 | // Show the link details for the user |
||
| 31 | 12 | public function show($id) |
|
| 32 | { |
||
| 33 | 12 | $details = FileLinks::where('link_hash', $id)->first(); |
|
| 34 | |||
| 35 | // Verify that the link is valid |
||
| 36 | 12 | if(empty($details)) |
|
| 37 | { |
||
| 38 | 2 | Log::warning('Visitor '.\Request::ip().' visited bad link Hash - '.$id); |
|
| 39 | 2 | return view('links.guestBadLink'); |
|
| 40 | } |
||
| 41 | // Verify that the link has not expired |
||
| 42 | 10 | else if($details->expire <= date('Y-m-d')) |
|
|
1 ignored issue
–
show
|
|||
| 43 | { |
||
| 44 | 2 | Log::warning('Visitor '.\Request::ip().' visited expired link Hash - '.$id); |
|
| 45 | 2 | return view('links.guestExpiredLink'); |
|
| 46 | } |
||
| 47 | |||
| 48 | // Link is valid - determine if the link has files that can be downloaded |
||
| 49 | 8 | $files = FileLinkFiles::where('link_id', $details->link_id) |
|
|
1 ignored issue
–
show
|
|||
| 50 | 8 | ->where('upload', false) |
|
| 51 | 8 | ->count(); |
|
| 52 | |||
| 53 | 8 | if($files == 0 && $details->allow_upload === 'No') |
|
| 54 | { |
||
| 55 | 2 | Log::warning('Visitor ' . \Request::ip() . ' visited a link that they cannot do anything with. Hash - ' . $id); |
|
| 56 | 2 | return view('links.guestDeadLink'); |
|
| 57 | } |
||
| 58 | |||
| 59 | 6 | Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip()); |
|
| 60 | 6 | Log::debug('Link Hash-'.$id); |
|
| 61 | 6 | return view('links.guestDetails', [ |
|
| 62 | 6 | 'hash' => $id, |
|
| 63 | 6 | 'details' => $details, |
|
| 64 | 6 | 'hasFiles' => $files > 0 ? true : false, |
|
| 65 | 6 | 'allowUp' => $details->allow_upload === 'Yes' ? true : false, |
|
| 66 | ]); |
||
| 67 | } |
||
| 68 | |||
| 69 | // Get the guest available files for the link |
||
| 70 | 6 | public function getFiles($id) |
|
| 83 | } |
||
| 84 | |||
| 85 | // Upload new file |
||
| 86 | 6 | public function update(Request $request, $id) |
|
| 87 | { |
||
| 88 | 6 | $request->validate(['name' => 'required', 'file' => 'required']); |
|
| 89 | |||
| 90 | 6 | $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request)); |
|
| 91 | |||
| 92 | // Verify that the upload is valid and being processed |
||
| 93 | 6 | if($receiver->isUploaded() === false) |
|
| 94 | { |
||
| 95 | Log::error('Upload File Missing - '.$request->toArray()); |
||
| 96 | throw new UploadMissingFileException(); |
||
| 97 | } |
||
| 98 | |||
| 99 | // Recieve and process the file |
||
| 100 | 6 | $save = $receiver->receive(); |
|
| 101 | |||
| 102 | // See if the uploade has finished |
||
| 103 | 6 | if($save->isFinished()) |
|
| 104 | { |
||
| 105 | 6 | $this->saveFile($save->getFile(), $id, $request); |
|
| 106 | |||
| 107 | 6 | return 'uploaded successfully'; |
|
| 108 | } |
||
| 109 | |||
| 110 | // Get the current progress |
||
| 111 | $handler = $save->handler(); |
||
| 112 | |||
| 113 | Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip()); |
||
| 114 | Log::debug('File being uploaded. Percentage done - '.$handler->getPercentageDone()); |
||
| 115 | return response()->json([ |
||
| 116 | 'done' => $handler->getPercentageDone(), |
||
| 117 | 'status' => true |
||
| 118 | ]); |
||
| 119 | } |
||
| 120 | |||
| 121 | // Save the file in the database |
||
| 122 | 6 | private function saveFile(UploadedFile $file, $id, $request) |
|
| 123 | { |
||
| 124 | 6 | $details = FileLinks::where('link_hash', $id)->first(); |
|
| 125 | 6 | $filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$details->link_id; |
|
|
1 ignored issue
–
show
|
|||
| 126 | |||
| 127 | 6 | $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName()); |
|
| 128 | 6 | $file->storeAs($filePath, $fileName); |
|
| 129 | |||
| 130 | // Place file in Files table of DB |
||
| 131 | 6 | $newFile = Files::create([ |
|
| 132 | 6 | 'file_name' => $fileName, |
|
| 133 | 6 | 'file_link' => $filePath.DIRECTORY_SEPARATOR |
|
| 134 | ]); |
||
| 135 | 6 | $fileID = $newFile->file_id; |
|
|
1 ignored issue
–
show
|
|||
| 136 | |||
| 137 | // Place the file in the file link files table of DB |
||
| 138 | 6 | FileLinkFiles::create([ |
|
| 139 | 6 | 'link_id' => $details->link_id, |
|
| 140 | 6 | 'file_id' => $fileID, |
|
| 141 | 6 | 'added_by' => $request->name, |
|
| 142 | 6 | 'upload' => 1, |
|
| 143 | 6 | 'note' => $request->note |
|
| 144 | ]); |
||
| 145 | |||
| 146 | 6 | Log::info('File uploaded by guest '.\Request::ip().' for file link -'.$details->link_id); |
|
| 147 | 6 | Log::debug('File Data -', $request->toArray()); |
|
| 148 | |||
| 149 | 6 | return response()->json(['success' => true]); |
|
| 150 | } |
||
| 151 | |||
| 152 | // Notify the owner of the link that files were uploaded |
||
| 153 | 2 | public function notify(Request $request, $id) |
|
| 166 | 2 | } |
|
| 167 | } |
||
| 168 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.