1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\FileLinks; |
4
|
|
|
|
5
|
|
|
use App\Files; |
6
|
|
|
use App\FileLinks; |
7
|
|
|
use App\FileLinkFiles; |
8
|
|
|
use App\CustomerFiles; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Support\Facades\Log; |
11
|
|
|
use App\Http\Controllers\Controller; |
12
|
|
|
use Illuminate\Support\Facades\Auth; |
13
|
|
|
use Illuminate\Support\Facades\Route; |
14
|
|
|
use Illuminate\Support\Facades\Storage; |
15
|
|
|
use App\Http\Resources\FileLinkFilesCollection; |
16
|
|
|
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver; |
17
|
|
|
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory; |
18
|
|
|
|
19
|
|
|
class LinkFilesController extends Controller |
20
|
|
|
{ |
21
|
|
|
private $user; |
22
|
|
|
|
23
|
22 |
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
// Verify the user is logged in and has permissions for this page |
26
|
22 |
|
$this->middleware('auth'); |
27
|
|
|
$this->middleware(function($request, $next) { |
28
|
16 |
|
$this->user = auth()->user(); |
29
|
16 |
|
$this->authorize('hasAccess', 'Use File Links'); |
30
|
10 |
|
return $next($request); |
31
|
22 |
|
}); |
32
|
22 |
|
} |
33
|
|
|
|
34
|
|
|
// Add a file to the file link |
35
|
2 |
|
public function store(Request $request) |
36
|
|
|
{ |
37
|
2 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name.'. Submitted Data - ', $request->toArray()); |
38
|
|
|
|
39
|
2 |
|
$request->validate([ |
40
|
2 |
|
'linkID' => 'required|exists:file_links,link_id' |
41
|
|
|
]); |
42
|
|
|
|
43
|
2 |
|
$receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request)); |
44
|
|
|
|
45
|
|
|
// Recieve and process the file |
46
|
2 |
|
$save = $receiver->receive(); |
47
|
|
|
|
48
|
|
|
// See if the uploade has finished |
49
|
2 |
|
if($save->isFinished()) |
50
|
|
|
{ |
51
|
2 |
|
$filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$request->linkID; |
52
|
2 |
|
$file = $save->getFile(); |
53
|
|
|
|
54
|
|
|
// Clean the file and store it |
55
|
2 |
|
$fileName = Files::cleanFilename($filePath, $file->getClientOriginalName()); |
56
|
2 |
|
$file->storeAs($filePath, $fileName); |
57
|
|
|
|
58
|
|
|
// Place file in Files table of DB |
59
|
2 |
|
$newFile = Files::create([ |
60
|
2 |
|
'file_name' => $fileName, |
61
|
2 |
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
62
|
|
|
]); |
63
|
2 |
|
$fileID = $newFile->file_id; |
64
|
|
|
|
65
|
|
|
// Place the file in the file link files table of DB |
66
|
2 |
|
FileLinkFiles::create([ |
67
|
2 |
|
'link_id' => $request->linkID, |
68
|
2 |
|
'file_id' => $fileID, |
69
|
2 |
|
'user_id' => Auth::user()->user_id, |
70
|
2 |
|
'upload' => 0 |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
// Log stored file |
74
|
2 |
|
Log::info('File Stored', ['file_id' => $fileID, 'file_path' => $filePath.DIRECTORY_SEPARATOR.$fileName]); |
75
|
|
|
|
76
|
2 |
|
return response()->json(['success' => true]); ; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Get the current progress |
80
|
|
|
$handler = $save->handler(); |
81
|
|
|
|
82
|
|
|
Log::debug('File being uploaded. Percentage done - '.$handler->getPercentageDone()); |
83
|
|
|
return response()->json([ |
84
|
|
|
'done' => $handler->getPercentageDone(), |
85
|
|
|
'status' => true |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Show the files attached to a link |
90
|
2 |
|
public function show($id) |
91
|
|
|
{ |
92
|
2 |
|
$files = new FileLinkFilesCollection( |
93
|
2 |
|
FileLinkFiles::where('link_id', $id) |
94
|
2 |
|
->orderBy('user_id', 'ASC') |
95
|
2 |
|
->orderBy('created_at', 'ASC') |
96
|
2 |
|
->with('Files') |
97
|
2 |
|
->with('User') |
98
|
2 |
|
->get() |
99
|
|
|
); |
100
|
|
|
|
101
|
2 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name); |
102
|
2 |
|
return $files; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Move a file to a customer file |
106
|
4 |
|
public function update(Request $request, $id) |
107
|
|
|
{ |
108
|
4 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name.'. Submitted Data - ', $request->toArray()); |
109
|
|
|
|
110
|
4 |
|
$request->validate([ |
111
|
4 |
|
'fileID' => 'required', |
112
|
|
|
'fileName' => 'required', |
113
|
|
|
'fileType' => 'required' |
114
|
|
|
]); |
115
|
|
|
|
116
|
4 |
|
$linkData = FileLinks::find($id); |
117
|
|
|
|
118
|
4 |
|
$newPath = config('filesystems.paths.customers').DIRECTORY_SEPARATOR.$linkData->cust_id.DIRECTORY_SEPARATOR; |
119
|
4 |
|
$fileData = Files::find($request->fileID); |
120
|
|
|
|
121
|
|
|
// Verify that the file does not already exist in the customerdata or file |
122
|
4 |
|
$dup = CustomerFiles::where('file_id', $request->fileID)->where('cust_id', $linkData->cust_id)->count(); |
123
|
4 |
|
if($dup || Storage::exists($newPath.$fileData->file_name)) |
124
|
|
|
{ |
125
|
2 |
|
return response()->json(['success' => 'false', 'reason' => 'This File Already Exists in Customer Files']); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Move the file to the customrs file folder |
129
|
|
|
try |
130
|
|
|
{ |
131
|
4 |
|
Storage::move($fileData->file_link.$fileData->file_name, $newPath.$fileData->file_name); |
132
|
|
|
} |
133
|
2 |
|
catch(\Exception $e) |
134
|
|
|
{ |
135
|
2 |
|
report($e); |
136
|
2 |
|
return response()->json(['success' => false, 'reason' => 'Cannot Find File']); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// Update the file path in the database |
140
|
2 |
|
$fileData->update([ |
141
|
2 |
|
'file_link' => $newPath |
142
|
|
|
]); |
143
|
|
|
|
144
|
|
|
// Place the file in the customer database |
145
|
2 |
|
CustomerFiles::create([ |
146
|
2 |
|
'file_id' => $request->fileID, |
147
|
2 |
|
'file_type_id' => $request->fileType, |
148
|
2 |
|
'cust_id' => $linkData->cust_id, |
149
|
2 |
|
'user_id' => Auth::user()->user_id, |
150
|
2 |
|
'name' => $request->fileName |
151
|
|
|
]); |
152
|
|
|
|
153
|
2 |
|
Log::debug('File Data - ', $request->toArray()); |
154
|
2 |
|
Log::info('File ID-'.$request->fileId.' moved to customer ID-'.$linkData->cust_id.' for link ID-'.$id); |
155
|
2 |
|
return response()->json(['success' => true]); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// Delete a file attached to a link |
159
|
2 |
|
public function destroy($id) |
160
|
|
|
{ |
161
|
|
|
// Get the necessary file information and delete it from the database |
162
|
2 |
|
$fileData = FileLinkFiles::find($id); |
163
|
2 |
|
$fileID = $fileData->file_id; |
164
|
2 |
|
$fileData->delete(); |
165
|
|
|
|
166
|
|
|
// Delete the file from the folder (not, will not delete if in use elsewhere) |
167
|
2 |
|
Files::deleteFile($fileID); |
168
|
|
|
|
169
|
2 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name); |
170
|
2 |
|
Log::info('File ID-'.$fileData->file_id.' deleted for Link ID-'.$fileData->link_id.' by '.Auth::user()->user_id); |
171
|
2 |
|
return response()->json(['success' => true]); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|