1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Domains\FileLinks; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Facades\Log; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
use Illuminate\Support\Facades\Storage; |
9
|
|
|
use Illuminate\Support\Facades\Notification; |
10
|
|
|
|
11
|
|
|
use App\User; |
12
|
|
|
use App\Files; |
13
|
|
|
use App\FileLinkFiles; |
14
|
|
|
use App\CustomerFiles; |
15
|
|
|
|
16
|
|
|
use App\Domains\FilesDomain; |
17
|
|
|
|
18
|
|
|
use App\Notifications\NewFileUpload; |
19
|
|
|
|
20
|
|
|
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver; |
21
|
|
|
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory; |
22
|
|
|
|
23
|
|
|
use App\Http\Requests\MoveFileLinkFileToCustomerRequest; |
24
|
|
|
|
25
|
|
|
class SaveFileLinkFile extends FilesDomain |
26
|
|
|
{ |
27
|
|
|
// Execute will process an uploading file |
28
|
8 |
|
public function execute(Request $request, $new = true) |
29
|
|
|
{ |
30
|
8 |
|
$this->receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request)); |
31
|
|
|
|
32
|
8 |
|
$save = $this->receiver->receive(); |
33
|
8 |
|
if($save->isFinished()) |
34
|
|
|
{ |
35
|
8 |
|
if(!$new) |
36
|
|
|
{ |
37
|
8 |
|
$this->path = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$request->linkID; |
38
|
|
|
} |
39
|
|
|
|
40
|
8 |
|
$fileID = $this->saveFile($save->getFile()); |
41
|
|
|
|
42
|
8 |
|
if($new) |
43
|
|
|
{ |
44
|
|
|
$fileArr = session('newLinkFile') != null ? session('newLinkFile') : []; |
45
|
|
|
$fileArr[] = $fileID; |
46
|
|
|
session(['newLinkFile' => $fileArr]); |
47
|
|
|
} |
48
|
|
|
else |
49
|
|
|
{ |
50
|
8 |
|
if($request->name) |
51
|
|
|
{ |
52
|
|
|
$uploadDetails = [ |
53
|
6 |
|
'name' => $request->name, |
54
|
6 |
|
'note' => $request->comments, |
55
|
|
|
]; |
56
|
|
|
|
57
|
6 |
|
$this->attachFile($request->linkID, $fileID, true, $uploadDetails); |
58
|
|
|
} |
59
|
|
|
else |
60
|
|
|
{ |
61
|
2 |
|
$this->attachFile($request->linkID, $fileID); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
8 |
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Move a file in the link to a customers files folder |
72
|
4 |
|
public function moveFileToCustomer(MoveFileLinkFileToCustomerRequest $request, $linkID) |
73
|
|
|
{ |
74
|
4 |
|
$linkCustomer = (new GetFileLinkDetails($linkID))->getLinkCustomer(); |
75
|
4 |
|
$this->path = config('filesystems.paths.customers').DIRECTORY_SEPARATOR.$linkCustomer.DIRECTORY_SEPARATOR; |
76
|
|
|
|
77
|
4 |
|
if($this->checkForDup($request->fileID, $linkCustomer)) |
78
|
|
|
{ |
79
|
2 |
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
if(!$this->moveFile($this->path, $request->fileID)) |
83
|
|
|
{ |
84
|
2 |
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
CustomerFiles::create([ |
88
|
2 |
|
'file_id' => $request->fileID, |
89
|
2 |
|
'file_type_id' => $request->fileType, |
90
|
2 |
|
'cust_id' => $linkCustomer, |
91
|
2 |
|
'user_id' => Auth::user()->user_id, |
92
|
2 |
|
'name' => $request->fileName |
93
|
|
|
]); |
94
|
|
|
|
95
|
2 |
|
Log::info('File ID '.$request->fileID.' has been attached to Customer ID '.$linkCustomer.' by '.Auth::user()->full_name); |
96
|
2 |
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Delete a file attached to file link |
100
|
2 |
|
public function deleteLinkFile($fileLinkID) |
101
|
|
|
{ |
102
|
2 |
|
$fileData = FileLinkFiles::find($fileLinkID); |
103
|
2 |
|
$fileID = $fileData->file_id; |
104
|
2 |
|
Log::notice('A file for a File Link has been deleted by '.Auth::user()->full_name.'. File Data - ', array($fileData)); |
105
|
2 |
|
$fileData->delete(); |
106
|
|
|
|
107
|
2 |
|
$this->deleteFile($fileID); |
108
|
2 |
|
} |
109
|
|
|
|
110
|
|
|
// Attach a file to a file link |
111
|
8 |
|
protected function attachFile($linkID, $fileID, $upload = false, $uploadDetails = null) |
112
|
|
|
{ |
113
|
8 |
|
$fileData = FileLinkFiles::create([ |
114
|
8 |
|
'link_id' => $linkID, |
115
|
8 |
|
'file_id' => $fileID, |
116
|
8 |
|
'user_id' => $upload ? null : Auth::user()->user_id, |
117
|
8 |
|
'added_by' => $upload ? $uploadDetails['name'] : null, |
118
|
8 |
|
'upload' => $upload, |
119
|
8 |
|
'note' => $upload ? $uploadDetails['note'] : null, |
120
|
|
|
]); |
121
|
|
|
|
122
|
8 |
|
Log::info('A new file has been attached to File Link ID '.$linkID.'. File Data - ', array($fileData)); |
123
|
8 |
|
return $fileData; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Move files from the default file location to the proper file link folder |
127
|
|
|
// public function relocateFiles($linkID) |
128
|
|
|
// { |
129
|
|
|
// $files = session('newLinkFile'); |
130
|
|
|
// $this->path = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$linkID; |
131
|
|
|
|
132
|
|
|
// foreach($files as $file) |
133
|
|
|
// { |
134
|
|
|
// $this->moveFile($this->path, $file); |
135
|
|
|
// $this->attachFile($linkID, $file); |
136
|
|
|
// } |
137
|
|
|
|
138
|
|
|
// session()->forget('newLinkFile'); |
139
|
|
|
// } |
140
|
|
|
|
141
|
|
|
// When a guest uploads a new file, the owner is notified |
142
|
2 |
|
public function notifyOwnerOfUpload($userID, $linkDetails) |
143
|
|
|
{ |
144
|
2 |
|
$userData = User::find($userID); |
145
|
2 |
|
Notification::send($userData, new NewFileUpload($linkDetails)); |
146
|
|
|
|
147
|
2 |
|
return true; |
148
|
|
|
} |
149
|
|
|
|
150
|
4 |
|
protected function checkForDup($fileID, $custID) |
151
|
|
|
{ |
152
|
4 |
|
$fileData = Files::find($fileID); |
153
|
|
|
|
154
|
4 |
|
$dup = CustomerFiles::where('file_id', $fileData->fileID)->where('cust_id', $custID)->count(); |
155
|
4 |
|
if($dup || Storage::exists($this->path.$fileData->file_name)) |
156
|
|
|
{ |
157
|
2 |
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
4 |
|
return false; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|