1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Customers; |
4
|
|
|
|
5
|
|
|
use App\Files; |
6
|
|
|
use App\Customers; |
7
|
|
|
use App\CustomerFiles; |
8
|
|
|
use App\CustomerFileTypes; |
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 Pion\Laravel\ChunkUpload\Receiver\FileReceiver; |
15
|
|
|
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory; |
16
|
|
|
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException; |
17
|
|
|
|
18
|
|
|
class CustomerFilesController extends Controller |
19
|
|
|
{ |
20
|
12 |
|
public function __construct() |
21
|
|
|
{ |
22
|
12 |
|
$this->middleware('auth'); |
23
|
12 |
|
} |
24
|
|
|
|
25
|
|
|
// Store a new customer file |
26
|
2 |
|
public function store(Request $request) |
27
|
|
|
{ |
28
|
|
|
// Validate the form |
29
|
2 |
|
$request->validate([ |
30
|
2 |
|
'cust_id' => 'required', |
31
|
|
|
'name' => 'required', |
32
|
|
|
'type' => 'required' |
33
|
|
|
]); |
34
|
|
|
|
35
|
2 |
|
$receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request)); |
36
|
|
|
|
37
|
|
|
// Verify that the upload is valid and being processed |
38
|
2 |
|
if($receiver->isUploaded() === false) |
39
|
|
|
{ |
40
|
|
|
Log::error('Upload File Missing - ' . |
41
|
|
|
/** @scrutinizer ignore-type */ |
42
|
|
|
$request->toArray()); |
43
|
|
|
throw new UploadMissingFileException(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Receive and process the file |
47
|
2 |
|
$save = $receiver->receive(); |
48
|
|
|
|
49
|
|
|
// See if the upload has finished |
50
|
2 |
|
if($save->isFinished()) |
51
|
|
|
{ |
52
|
2 |
|
$file = $save->getFile(); |
53
|
|
|
// Set file locationi and clean filename for duplicates |
54
|
2 |
|
$filePath = config('filesystems.paths.customers').DIRECTORY_SEPARATOR.$request->cust_id; |
55
|
2 |
|
$fileName = Files::cleanFileName($filePath, $file->getClientOriginalName()); |
56
|
|
|
|
57
|
|
|
// Store the file |
58
|
2 |
|
$file->storeAs($filePath, $fileName); |
59
|
|
|
|
60
|
|
|
// Put the file in the database |
61
|
2 |
|
$file = Files::create( |
62
|
|
|
[ |
63
|
2 |
|
'file_name' => $fileName, |
64
|
2 |
|
'file_link' => $filePath.DIRECTORY_SEPARATOR |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
// Get information for system files table |
68
|
2 |
|
$fileID = $file->file_id; |
69
|
|
|
|
70
|
|
|
// Input the file into the customer files table |
71
|
2 |
|
CustomerFiles::create([ |
72
|
2 |
|
'file_id' => $fileID, |
73
|
2 |
|
'file_type_id' => $request->type, |
74
|
2 |
|
'cust_id' => $request->cust_id, |
75
|
2 |
|
'user_id' => Auth::user()->user_id, |
76
|
2 |
|
'name' => $request->name |
77
|
|
|
]); |
78
|
|
|
|
79
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
80
|
2 |
|
Log::debug('Submitted Data - ', $request->toArray()); |
81
|
2 |
|
Log::info('File added for Customer ID-'.$request->custID.' by User ID-'.Auth::user()->user_id.'. New File ID-'.$fileID); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Get the current progress |
85
|
2 |
|
$handler = $save->handler(); |
86
|
|
|
|
87
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
88
|
2 |
|
Log::debug('File being uploaded. Percentage done - '.$handler->getPercentageDone()); |
89
|
2 |
|
return response()->json([ |
90
|
2 |
|
'done' => $handler->getPercentageDone(), |
91
|
|
|
'status' => true |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Get the files for the customer |
96
|
2 |
|
public function show($id) |
97
|
|
|
{ |
98
|
2 |
|
$files = CustomerFiles::where('cust_id', $id) |
99
|
2 |
|
->with('Files') |
100
|
2 |
|
->with('CustomerFileTypes') |
101
|
2 |
|
->with('User') |
102
|
2 |
|
->get(); |
103
|
|
|
|
104
|
2 |
|
return $files; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Update the test information of the file, but not the file itself |
108
|
|
|
public function update(Request $request, $id) |
109
|
|
|
{ |
110
|
|
|
$request->validate([ |
111
|
|
|
'cust_id' => 'required', |
112
|
|
|
'name' => 'required', |
113
|
|
|
'type' => 'required' |
114
|
|
|
]); |
115
|
|
|
|
116
|
|
|
CustomerFiles::find($id)->update([ |
117
|
|
|
'name' => $request->name, |
118
|
|
|
'file_type_id' => $request->type |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
return response()->json(['success' => true]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Remove a customer file |
125
|
2 |
|
public function destroy($id) |
126
|
|
|
{ |
127
|
|
|
// Remove the file from SystemFiles table |
128
|
2 |
|
$data = CustomerFiles::find($id); |
129
|
2 |
|
$fileID = $data->file_id; |
130
|
2 |
|
$data->delete(); |
131
|
|
|
|
132
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
133
|
2 |
|
Log::info('File Deleted For Customer ID-'.$data->custID.' by User ID-'.Auth::user()->user_id.'. File ID-'.$id); |
134
|
|
|
|
135
|
|
|
// Delete from system if no longer in use |
136
|
2 |
|
Files::deleteFile($fileID); |
137
|
|
|
|
138
|
2 |
|
return response()->json(['success' => true]); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|