|
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\FileLinkNotes; |
|
9
|
|
|
use App\FileLinkFiles; |
|
10
|
|
|
use Illuminate\Http\Request; |
|
11
|
|
|
use Illuminate\Http\UploadedFile; |
|
12
|
|
|
use Illuminate\Support\Facades\Log; |
|
13
|
|
|
// use Illuminate\Support\Facades\Auth; |
|
14
|
|
|
use App\Notifications\NewFileUpload; |
|
15
|
|
|
use App\Http\Controllers\Controller; |
|
16
|
|
|
use Illuminate\Support\Facades\Route; |
|
17
|
|
|
// use Illuminate\Support\Facades\Storage; |
|
18
|
|
|
use Illuminate\Support\Facades\Notification; |
|
19
|
|
|
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver; |
|
20
|
|
|
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory; |
|
21
|
|
|
// use Pion\Laravel\ChunkUpload\Handler\AbstractHandler; |
|
22
|
|
|
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException; |
|
23
|
|
|
|
|
24
|
|
|
use App\http\Resources\FileLinkFilesCollection; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class UserLinksController extends Controller |
|
28
|
|
|
{ |
|
29
|
|
|
// Landing page if no link is sent |
|
30
|
|
|
public function index() |
|
31
|
|
|
{ |
|
32
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip()); |
|
33
|
|
|
return view('links.userIndex'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// Show the link details for the user |
|
37
|
|
|
public function show($id) |
|
38
|
|
|
{ |
|
39
|
|
|
$details = FileLinks::where('link_hash', $id)->first(); |
|
40
|
|
|
|
|
41
|
|
|
// Verify that the link is valid |
|
42
|
|
|
if(empty($details)) |
|
43
|
|
|
{ |
|
44
|
|
|
Log::warning('Visitor '.\Request::ip().' visited bad link Hash - '.$id); |
|
45
|
|
|
return view('links.userBadLink'); |
|
46
|
|
|
} |
|
47
|
|
|
// Verify that the link has not expired |
|
48
|
|
|
else if($details->expire <= date('Y-m-d')) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
Log::warning('Visitor '.\Request::ip().' visited expired link Hash - '.$id); |
|
51
|
|
|
return view('links.userExpiredLink'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Link is valid - determine if the link has files that can be downloaded |
|
55
|
|
|
$files = FileLinkFiles::where('link_id', $details->link_id) |
|
|
|
|
|
|
56
|
|
|
->where('upload', false) |
|
57
|
|
|
->count(); |
|
58
|
|
|
|
|
59
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip()); |
|
60
|
|
|
Log::debug('Link Hash-'.$id); |
|
61
|
|
|
return view('links.userDetails', [ |
|
62
|
|
|
'hash' => $id, |
|
63
|
|
|
'details' => $details, |
|
64
|
|
|
'hasFiles' => $files > 0 ? true : false, |
|
65
|
|
|
'allowUp' => $details->allow_upload === 'Yes' ? true : false, |
|
66
|
|
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// Get the guest available files for the link |
|
70
|
|
|
public function getFiles($id) |
|
71
|
|
|
{ |
|
72
|
|
|
$linkID = FileLinks::where('link_hash', $id)->first()->link_id; |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
$files = new FileLinkFilesCollection( |
|
75
|
|
|
FileLinkFiles::where('link_id', $linkID) |
|
76
|
|
|
->where('upload', 0) |
|
77
|
|
|
->orderBy('created_at', 'ASC') |
|
78
|
|
|
->with('Files') |
|
79
|
|
|
->get() |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
return $files; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
// Upload new file |
|
94
|
|
|
public function update(Request $request, $id) |
|
95
|
|
|
{ |
|
96
|
|
|
$request->validate(['name' => 'required', 'file' => 'required']); |
|
97
|
|
|
|
|
98
|
|
|
$receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request)); |
|
99
|
|
|
|
|
100
|
|
|
// Verify that the upload is valid and being processed |
|
101
|
|
|
if($receiver->isUploaded() === false) |
|
102
|
|
|
{ |
|
103
|
|
|
Log::error('Upload File Missing - '.$request->toArray()); |
|
|
|
|
|
|
104
|
|
|
throw new UploadMissingFileException(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// Recieve and process the file |
|
108
|
|
|
$save = $receiver->receive(); |
|
109
|
|
|
|
|
110
|
|
|
// See if the uploade has finished |
|
111
|
|
|
if($save->isFinished()) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->saveFile($save->getFile(), $id, $request); |
|
114
|
|
|
|
|
115
|
|
|
return 'uploaded successfully'; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// Get the current progress |
|
119
|
|
|
$handler = $save->handler(); |
|
120
|
|
|
|
|
121
|
|
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip()); |
|
122
|
|
|
Log::debug('File being uploaded. Percentage done - '.$handler->getPercentageDone()); |
|
123
|
|
|
return response()->json([ |
|
124
|
|
|
'done' => $handler->getPercentageDone(), |
|
125
|
|
|
'status' => true |
|
126
|
|
|
]); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// Save the file in the database |
|
130
|
|
|
private function saveFile(UploadedFile $file, $id, $request) |
|
131
|
|
|
{ |
|
132
|
|
|
$details = FileLinks::where('link_hash', $id)->first(); |
|
133
|
|
|
$filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$details->link_id; |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
$fileName = Files::cleanFilename($filePath, $file->getClientOriginalName()); |
|
136
|
|
|
$file->storeAs($filePath, $fileName); |
|
137
|
|
|
|
|
138
|
|
|
// Place file in Files table of DB |
|
139
|
|
|
$newFile = Files::create([ |
|
140
|
|
|
'file_name' => $fileName, |
|
141
|
|
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
|
142
|
|
|
]); |
|
143
|
|
|
$fileID = $newFile->file_id; |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
// Place the file in the file link files table of DB |
|
146
|
|
|
FileLinkFiles::create([ |
|
147
|
|
|
'link_id' => $details->link_id, |
|
148
|
|
|
'file_id' => $fileID, |
|
149
|
|
|
'added_by' => $request->name, |
|
150
|
|
|
'upload' => 1 |
|
151
|
|
|
]); |
|
152
|
|
|
|
|
153
|
|
|
if(!empty($request->note)) |
|
154
|
|
|
{ |
|
155
|
|
|
FileLinkNotes::create([ |
|
156
|
|
|
'link_id' => $details->link_id, |
|
157
|
|
|
'file_id' => $fileID, |
|
158
|
|
|
'note' => $request->note |
|
159
|
|
|
]); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// Send email and notification to the creator of the link |
|
163
|
|
|
$user = User::find($details->user_id); |
|
|
|
|
|
|
164
|
|
|
Notification::send($user, new NewFileUpload($details)); |
|
165
|
|
|
|
|
166
|
|
|
Log::info('File uploaded by guest '.\Request::ip().' for file link -'.$details->link_id); |
|
167
|
|
|
Log::debug('File Data -', $request->toArray()); |
|
168
|
|
|
|
|
169
|
|
|
return response()->json(['success' => true]); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.