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