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 |
|
$details = FileLinks::where('link_hash', $id)->first(); |
33
|
|
|
|
34
|
|
|
// Verify that the link is valid |
35
|
12 |
|
if(empty($details)) |
36
|
|
|
{ |
37
|
2 |
|
Log::warning('Visitor '.\Request::ip().' visited bad link Hash - '.$id); |
38
|
2 |
|
return view('links.guestBadLink'); |
39
|
|
|
} |
40
|
|
|
// Verify that the link has not expired |
41
|
10 |
|
else if($details->expire <= date('Y-m-d')) |
42
|
|
|
{ |
43
|
2 |
|
Log::warning('Visitor '.\Request::ip().' visited expired link Hash - '.$id); |
44
|
2 |
|
return view('links.guestExpiredLink'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Link is valid - determine if the link has files that can be downloaded |
48
|
8 |
|
$files = FileLinkFiles::where('link_id', $details->link_id) |
49
|
8 |
|
->where('upload', false) |
50
|
8 |
|
->count(); |
51
|
|
|
|
52
|
8 |
|
if($files == 0 && $details->allow_upload === 'No') |
53
|
|
|
{ |
54
|
2 |
|
Log::warning('Visitor ' . \Request::ip() . ' visited a link that they cannot do anything with. Hash - ' . $id); |
55
|
2 |
|
return view('links.guestDeadLink'); |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by IP Address ' . \Request::ip()); |
59
|
6 |
|
return view('links.guestDetails', [ |
60
|
6 |
|
'hash' => $id, |
61
|
6 |
|
'details' => $details, |
62
|
6 |
|
'hasFiles' => $files > 0 ? true : false, |
63
|
6 |
|
'allowUp' => $details->allow_upload === 'Yes' ? true : false, |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Get the guest available files for the link |
68
|
6 |
|
public function getFiles($id) |
69
|
|
|
{ |
70
|
6 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by IP Address ' . \Request::ip()); |
71
|
6 |
|
$linkID = FileLinks::where('link_hash', $id)->first()->link_id; |
72
|
|
|
|
73
|
6 |
|
$files = new FileLinkFilesCollection( |
74
|
6 |
|
FileLinkFiles::where('link_id', $linkID) |
75
|
6 |
|
->where('upload', 0) |
76
|
6 |
|
->orderBy('created_at', 'ASC') |
77
|
6 |
|
->with('Files') |
78
|
6 |
|
->get() |
79
|
|
|
); |
80
|
|
|
|
81
|
6 |
|
return $files; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Upload new file |
85
|
6 |
|
public function update(Request $request, $id) |
86
|
|
|
{ |
87
|
6 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by IP Address ' . \Request::ip().'. Submitted Data - ', $request->toArray()); |
88
|
6 |
|
$request->validate(['name' => 'required', 'file' => 'required']); |
89
|
|
|
|
90
|
6 |
|
$receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request)); |
91
|
|
|
|
92
|
|
|
// Recieve and process the file |
93
|
6 |
|
$save = $receiver->receive(); |
94
|
|
|
|
95
|
|
|
// See if the uploade has finished |
96
|
6 |
|
if($save->isFinished()) |
97
|
|
|
{ |
98
|
6 |
|
$this->saveFile($save->getFile(), $id, $request); |
99
|
|
|
|
100
|
6 |
|
return 'uploaded successfully'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Get the current progress |
104
|
|
|
$handler = $save->handler(); |
105
|
|
|
|
106
|
|
|
Log::debug('File being uploaded. Percentage done - '.$handler->getPercentageDone()); |
107
|
|
|
return response()->json([ |
108
|
|
|
'done' => $handler->getPercentageDone(), |
109
|
|
|
'status' => true |
110
|
|
|
]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Save the file in the database |
114
|
6 |
|
private function saveFile(UploadedFile $file, $id, $request) |
115
|
|
|
{ |
116
|
6 |
|
$details = FileLinks::where('link_hash', $id)->first(); |
117
|
6 |
|
$filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$details->link_id; |
118
|
|
|
|
119
|
6 |
|
$fileName = Files::cleanFilename($filePath, $file->getClientOriginalName()); |
120
|
6 |
|
$file->storeAs($filePath, $fileName); |
121
|
|
|
|
122
|
|
|
// Place file in Files table of DB |
123
|
6 |
|
$newFile = Files::create([ |
124
|
6 |
|
'file_name' => $fileName, |
125
|
6 |
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
126
|
|
|
]); |
127
|
6 |
|
$fileID = $newFile->file_id; |
128
|
|
|
|
129
|
|
|
// Place the file in the file link files table of DB |
130
|
6 |
|
FileLinkFiles::create([ |
131
|
6 |
|
'link_id' => $details->link_id, |
132
|
6 |
|
'file_id' => $fileID, |
133
|
6 |
|
'added_by' => $request->name, |
134
|
6 |
|
'upload' => 1, |
135
|
6 |
|
'note' => $request->note |
136
|
|
|
]); |
137
|
|
|
|
138
|
6 |
|
Log::info('File uploaded by guest '.\Request::ip().' for file link -'.$details->link_id); |
139
|
|
|
|
140
|
6 |
|
return response()->json(['success' => true]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Notify the owner of the link that files were uploaded |
144
|
2 |
|
public function notify(Request $request, $id) |
145
|
|
|
{ |
146
|
2 |
|
$request->validate([ |
147
|
2 |
|
'_complete' => 'required', |
148
|
|
|
'count' => 'required|integer' |
149
|
|
|
]); |
150
|
|
|
|
151
|
2 |
|
$details = FileLinks::where('link_hash', $id)->first(); |
152
|
|
|
|
153
|
2 |
|
$user = User::find($details->user_id); |
154
|
2 |
|
Notification::send($user, new NewFileUpload($details)); |
155
|
|
|
|
156
|
2 |
|
Log::debug('Notification of file upload sent to User ID - '.$user->user_id); |
157
|
2 |
|
} |
158
|
|
|
} |
159
|
|
|
|