1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\FileLinks; |
4
|
|
|
|
5
|
|
|
use App\User; |
6
|
|
|
use App\Files; |
7
|
|
|
use App\FileLinks; |
8
|
|
|
use App\FileLinkFiles; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Http\UploadedFile; |
11
|
|
|
use Illuminate\Support\Facades\Log; |
12
|
|
|
use App\Notifications\NewFileUpload; |
13
|
|
|
use App\Http\Controllers\Controller; |
14
|
|
|
use Illuminate\Support\Facades\Route; |
15
|
|
|
use Illuminate\Support\Facades\Notification; |
16
|
|
|
use App\Http\Resources\FileLinkFilesCollection; |
17
|
|
|
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver; |
18
|
|
|
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory; |
19
|
|
|
|
20
|
|
|
class GuestLinksController extends Controller |
21
|
|
|
{ |
22
|
|
|
// Landing page if no link is sent |
23
|
2 |
|
public function index() |
24
|
|
|
{ |
25
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by IP Address '.\Request::ip()); |
26
|
2 |
|
return view('links.guestIndex'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// Show the link details for the user |
30
|
12 |
|
public function show($id) |
31
|
|
|
{ |
32
|
12 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by IP Address ' . \Request::ip()); |
33
|
|
|
|
34
|
12 |
|
$details = FileLinks::where('link_hash', $id)->first(); |
35
|
|
|
|
36
|
|
|
// Verify that the link is valid |
37
|
12 |
|
if(empty($details)) |
38
|
|
|
{ |
39
|
2 |
|
Log::warning('Visitor '.\Request::ip().' visited bad link Hash - '.$id); |
40
|
2 |
|
return view('links.guestBadLink'); |
41
|
|
|
} |
42
|
|
|
// Verify that the link has not expired |
43
|
10 |
View Code Duplication |
else if($details->expire <= date('Y-m-d')) |
|
|
|
|
44
|
|
|
{ |
45
|
2 |
|
Log::warning('Visitor '.\Request::ip().' visited expired link Hash - '.$id); |
46
|
2 |
|
return view('links.guestExpiredLink'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Link is valid - determine if the link has files that can be downloaded |
50
|
8 |
|
$files = FileLinkFiles::where('link_id', $details->link_id) |
51
|
8 |
|
->where('upload', false) |
52
|
8 |
|
->count(); |
53
|
|
|
|
54
|
8 |
View Code Duplication |
if($files == 0 && $details->allow_upload === 'No') |
|
|
|
|
55
|
|
|
{ |
56
|
2 |
|
Log::warning('Visitor '.\Request::ip().' visited a link that they cannot do anything with. Hash - '.$id); |
57
|
2 |
|
return view('links.guestDeadLink'); |
58
|
|
|
} |
59
|
|
|
|
60
|
6 |
|
return view('links.guestDetails', [ |
61
|
6 |
|
'hash' => $id, |
62
|
6 |
|
'details' => $details, |
63
|
6 |
|
'hasFiles' => $files > 0 ? true : false, |
64
|
6 |
|
'allowUp' => $details->allow_upload === 'Yes' ? true : false, |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Get the guest available files for the link |
69
|
6 |
|
public function getFiles($id) |
70
|
|
|
{ |
71
|
6 |
|
Log::debug('Route '.Route::currentRouteName().' visited by IP Address '.\Request::ip()); |
72
|
6 |
|
$linkID = FileLinks::where('link_hash', $id)->first()->link_id; |
73
|
|
|
|
74
|
6 |
|
$files = new FileLinkFilesCollection( |
75
|
6 |
|
FileLinkFiles::where('link_id', $linkID) |
76
|
6 |
|
->where('upload', 0) |
77
|
6 |
|
->orderBy('created_at', 'ASC') |
78
|
6 |
|
->with('Files') |
79
|
6 |
|
->get() |
80
|
|
|
); |
81
|
|
|
|
82
|
6 |
|
Log::debug('Files gathered for link ID '.$linkID, array($files)); |
83
|
6 |
|
return $files; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Upload new file |
87
|
6 |
|
public function update(Request $request, $id) |
88
|
|
|
{ |
89
|
6 |
|
Log::debug('Route '.Route::currentRouteName().' visited by IP Address '.\Request::ip().'. Submitted Data - ', $request->toArray()); |
90
|
6 |
|
$request->validate([ |
|
|
|
|
91
|
6 |
|
'name' => 'required', |
92
|
|
|
'file' => 'required' |
93
|
|
|
]); |
94
|
|
|
|
95
|
6 |
|
$receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request)); |
96
|
|
|
|
97
|
|
|
// Recieve and process the file |
98
|
6 |
|
$save = $receiver->receive(); |
99
|
|
|
|
100
|
|
|
// See if the uploade has finished |
101
|
6 |
|
if($save->isFinished()) |
102
|
|
|
{ |
103
|
6 |
|
$this->saveFile($save->getFile(), $id, $request); |
104
|
6 |
|
return response()->json(['success' => true]); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Get the current progress |
108
|
|
|
$handler = $save->handler(); |
109
|
|
|
|
110
|
|
|
Log::debug('File being uploaded. Percentage done - '.$handler->getPercentageDone()); |
111
|
|
|
return response()->json([ |
112
|
|
|
'done' => $handler->getPercentageDone(), |
113
|
|
|
'status' => true |
114
|
|
|
]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Save the file in the database |
118
|
6 |
|
private function saveFile(UploadedFile $file, $id, $request) |
119
|
|
|
{ |
120
|
6 |
|
$details = FileLinks::where('link_hash', $id)->first(); |
121
|
6 |
|
$filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$details->link_id; |
122
|
|
|
|
123
|
6 |
|
$fileName = Files::cleanFilename($filePath, $file->getClientOriginalName()); |
124
|
6 |
|
$file->storeAs($filePath, $fileName); |
125
|
|
|
|
126
|
|
|
// Place file in Files table of DB |
127
|
6 |
|
$newFile = Files::create([ |
|
|
|
|
128
|
6 |
|
'file_name' => $fileName, |
129
|
6 |
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
130
|
|
|
]); |
131
|
6 |
|
$fileID = $newFile->file_id; |
132
|
|
|
|
133
|
|
|
// Place the file in the file link files table of DB |
134
|
6 |
|
FileLinkFiles::create([ |
|
|
|
|
135
|
6 |
|
'link_id' => $details->link_id, |
136
|
6 |
|
'file_id' => $fileID, |
137
|
6 |
|
'added_by' => $request->name, |
138
|
6 |
|
'upload' => 1, |
139
|
6 |
|
'note' => $request->comments |
140
|
|
|
]); |
141
|
|
|
|
142
|
6 |
|
Log::info('File uploaded by guest '.\Request::ip().' for file link -'.$details->link_id); |
143
|
6 |
|
return response()->json(['success' => true]); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// Notify the owner of the link that files were uploaded |
147
|
2 |
|
public function notify(Request $request, $id) |
148
|
|
|
{ |
149
|
2 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by IP Address ' . \Request::ip() . '. Submitted Data - ', $request->toArray()); |
150
|
|
|
|
151
|
2 |
|
$request->validate([ |
|
|
|
|
152
|
2 |
|
'_complete' => 'required', |
153
|
|
|
'count' => 'required|integer' |
154
|
|
|
]); |
155
|
|
|
|
156
|
2 |
|
$details = FileLinks::where('link_hash', $id)->first(); |
157
|
2 |
|
$user = User::find($details->user_id); |
158
|
2 |
|
Notification::send($user, new NewFileUpload($details)); |
159
|
|
|
|
160
|
2 |
|
Log::info('Notification of file upload sent to '.$user->full_name); |
161
|
2 |
|
} |
162
|
|
|
} |
163
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.