1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Facades\Storage; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
use Illuminate\Support\Facades\Log; |
9
|
|
|
use App\Files; |
10
|
|
|
use App\SystemTypes; |
11
|
|
|
use App\SystemFiles; |
12
|
|
|
use App\SystemFileTypes; |
13
|
|
|
|
14
|
|
|
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException; |
15
|
|
|
use Pion\Laravel\ChunkUpload\Handler\AbstractHandler; |
16
|
|
|
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory; |
17
|
|
|
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver; |
18
|
|
|
use Illuminate\Http\UploadedFile; |
19
|
|
|
|
20
|
|
|
class SystemFilesController extends Controller |
21
|
|
|
{ |
22
|
|
|
// Only authorized users have access |
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->middleware('auth'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function index() |
29
|
|
|
{ |
30
|
|
|
return view('system.form.newFile'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Ajax load to load the file for the system |
34
|
|
|
public function loadFiles($sysName, $fileType) |
35
|
|
|
{ |
36
|
|
|
$fileList = SystemFiles::whereHas('SystemTypes', function($q) use($sysName) |
37
|
|
|
{ |
38
|
|
|
$q->where('name', urldecode($sysName)); |
39
|
|
|
})->whereHas('SystemFileTypes', function($q) use($fileType) |
40
|
|
|
{ |
41
|
|
|
$q->where('description', urldecode($fileType)); |
42
|
|
|
})->with('user')->with('files')->get(); |
43
|
|
|
|
44
|
|
|
return view('system.fileList', [ |
45
|
|
|
'fileList' => $fileList |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Form to create a new system file |
50
|
|
|
public function create() |
51
|
|
|
{ |
52
|
|
|
return view('system.form.newFile'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Store the new system file |
56
|
|
|
public function store(Request $request) |
57
|
|
|
{ |
58
|
|
|
// Validate incoming data |
59
|
|
|
$request->validate([ |
60
|
|
|
'name' => 'required', |
61
|
|
|
'file' => 'required', |
62
|
|
|
'fileType' => 'required', |
63
|
|
|
'system' => 'required' |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
$receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request)); |
67
|
|
|
|
68
|
|
|
// check if the upload is success, throw exception or return response you need |
69
|
|
|
if ($receiver->isUploaded() === false) { |
70
|
|
|
throw new UploadMissingFileException(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// receive the file |
74
|
|
|
$save = $receiver->receive(); |
75
|
|
|
// check if the upload has finished (in chunk mode it will send smaller files) |
76
|
|
|
if ($save->isFinished()) |
77
|
|
|
{ |
78
|
|
|
// Get the folder location for the system |
79
|
|
|
$folder = SystemTypes::where('name', $request['system'])->first()->folder_location; |
80
|
|
|
|
81
|
|
|
// Set file location and clean filename for duplicates |
82
|
|
|
$filePath = config('filesystems.paths.systems') |
83
|
|
|
.DIRECTORY_SEPARATOR.strtolower($request['category']) |
84
|
|
|
.DIRECTORY_SEPARATOR.$folder; |
85
|
|
|
|
86
|
|
|
$fileID = $this->saveFile($save->getFile(), $filePath); |
87
|
|
|
|
88
|
|
|
$typeID = SystemFileTypes::where('description', $request['fileType'])->first()->type_id; |
89
|
|
|
$sysID = SystemTypes::where('name', $request['system'])->first()->sys_id; |
90
|
|
|
|
91
|
|
|
// Attach file to system type |
92
|
|
|
SystemFiles::create( |
93
|
|
|
[ |
94
|
|
|
'sys_id' => $sysID, |
95
|
|
|
'type_id' => $typeID, |
96
|
|
|
'file_id' => $fileID, |
97
|
|
|
'name' => $request['name'], |
98
|
|
|
'description' => $request['description'], |
99
|
|
|
'user_id' => \Auth::user()->user_id |
100
|
|
|
]); |
101
|
|
|
|
102
|
|
|
Log::info('File ID-'.$fileID.' Added For System ID-'.$sysID.' By User ID-'.Auth::user()->user_id); |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$handler = $save->handler(); |
107
|
|
|
|
108
|
|
|
return response()->json([ |
109
|
|
|
"done" => $handler->getPercentageDone(), |
110
|
|
|
'status' => true |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function saveFile(UploadedFile $file, $filePath) |
115
|
|
|
{ |
116
|
|
|
$fileName = Files::cleanFileName($filePath, $file->getClientOriginalName()); |
117
|
|
|
|
118
|
|
|
// Store the file |
119
|
|
|
$file->storeAs($filePath, $fileName); |
120
|
|
|
|
121
|
|
|
// Put the file in the database |
122
|
|
|
$file = Files::create( |
123
|
|
|
[ |
124
|
|
|
'file_name' => $fileName, |
125
|
|
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
126
|
|
|
]); |
127
|
|
|
|
128
|
|
|
return $file->file_id; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Form to show a file to edit |
132
|
|
|
public function edit($id) |
133
|
|
|
{ |
134
|
|
|
$data = SystemFiles::find($id); |
135
|
|
|
|
136
|
|
|
return view('system.form.editFile', [ |
137
|
|
|
'data' => $data |
138
|
|
|
]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Submit the edited file |
142
|
|
|
public function update(Request $request, $id) |
143
|
|
|
{ |
144
|
|
|
SystemFiles::find($id)->update([ |
145
|
|
|
'name' => $request['name'], |
146
|
|
|
'description' => $request['description'] |
147
|
|
|
]); |
148
|
|
|
|
149
|
|
|
Log::info('File Information on System File ID-'.$id.' Updated by User ID-'.Auth::user()->user_id); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// Load the replace file form |
153
|
|
|
public function replaceForm($id) |
154
|
|
|
{ |
155
|
|
|
return view('system.form.replaceFile', [ |
156
|
|
|
'fileID' => $id |
157
|
|
|
]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// Submit the replace file form |
161
|
|
|
public function replace($id, Request $request) |
162
|
|
|
{ |
163
|
|
|
$request->validate(['file' => 'required']); |
164
|
|
|
|
165
|
|
|
// Get the original file information |
166
|
|
|
$origData = SystemFiles::find($id)->with('files')->first(); |
167
|
|
|
$filePath = $origData->files->file_link; |
|
|
|
|
168
|
|
|
// Clean the filename |
169
|
|
|
$fileName = Files::cleanFileName($filePath, $request->file->getClientOriginalName()); |
170
|
|
|
|
171
|
|
|
// Store the file |
172
|
|
|
$request->file->storeAs($filePath, $fileName); |
173
|
|
|
|
174
|
|
|
// Put the file in the database |
175
|
|
|
$file = Files::create( |
176
|
|
|
[ |
177
|
|
|
'file_name' => $fileName, |
178
|
|
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
|
|
|
|
179
|
|
|
]); |
180
|
|
|
|
181
|
|
|
// Get information for system files table |
182
|
|
|
$fileID = $file->file_id; |
183
|
|
|
|
184
|
|
|
SystemFiles::find($id)->update([ |
185
|
|
|
'file_id' => $fileID |
186
|
|
|
]); |
187
|
|
|
|
188
|
|
|
Log::info('System File ID-'.$fileID.' Replaced by User ID-'.Auth::user()->user_id); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// Remove a file from the system files table |
192
|
|
|
public function destroy($id) |
193
|
|
|
{ |
194
|
|
|
// Remove the file from SystemFiles table |
195
|
|
|
$data = SystemFiles::find($id); |
196
|
|
|
$fileID = $data->file_id; |
197
|
|
|
|
198
|
|
|
Log::info('System File ID-'.$fileID.' Deleted For System ID-'.$data->sys_id.' by User ID-'.Auth::user()->user_id); |
199
|
|
|
|
200
|
|
|
$data->delete(); |
201
|
|
|
|
202
|
|
|
// Delete from system if no longer in use |
203
|
|
|
Files::deleteFile($fileID); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|