|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gewaer\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Phalcon\Http\Response; |
|
8
|
|
|
use Phalcon\Validation; |
|
9
|
|
|
use Phalcon\Validation\Validator\File as FileValidator; |
|
10
|
|
|
use Gewaer\Exception\UnprocessableEntityHttpException; |
|
11
|
|
|
use Gewaer\Models\FileSystem; |
|
12
|
|
|
use Gewaer\Filesystem\Helper; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Trait ResponseTrait |
|
16
|
|
|
* |
|
17
|
|
|
* @package Gewaer\Traits |
|
18
|
|
|
* |
|
19
|
|
|
* @property Users $user |
|
20
|
|
|
* @property AppsPlans $appPlan |
|
21
|
|
|
* @property CompanyBranches $branches |
|
22
|
|
|
* @property Companies $company |
|
23
|
|
|
* @property UserCompanyApps $app |
|
24
|
|
|
* @property \Phalcon\Di $di |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
trait FileManagementTrait |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Get item. |
|
31
|
|
|
* |
|
32
|
|
|
* @method GET |
|
33
|
|
|
* url /v1/filesystem/{id} |
|
34
|
|
|
* |
|
35
|
|
|
* @param mixed $id |
|
36
|
|
|
* |
|
37
|
|
|
* @return \Phalcon\Http\Response |
|
38
|
|
|
* @throws Exception |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getById($id) : Response |
|
41
|
|
|
{ |
|
42
|
|
|
//find the info |
|
43
|
|
|
$records = $this->model->findFirst([ |
|
44
|
|
|
'conditions' => 'entity_id = ?0 and companies_id = ?1 and apps_id = ?2', |
|
45
|
|
|
'bind' => [$id, $this->userData->currentCompanyId(), $this->app->getId()] |
|
46
|
|
|
]); |
|
47
|
|
|
|
|
48
|
|
|
if (!is_object($records)) { |
|
49
|
|
|
throw new UnprocessableEntityHttpException('Records not found'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $this->response($records); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Add a new item. |
|
57
|
|
|
* |
|
58
|
|
|
* @method POST |
|
59
|
|
|
* url /v1/filesystem |
|
60
|
|
|
* |
|
61
|
|
|
* @return \Phalcon\Http\Response |
|
62
|
|
|
* @throws Exception |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function create() : Response |
|
65
|
|
|
{ |
|
66
|
1 |
|
if (!$this->request->hasFiles()) { |
|
67
|
|
|
//@todo handle base64 images |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
return $this->response($this->processFiles()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Update an item. |
|
75
|
|
|
* |
|
76
|
|
|
* @method PUT |
|
77
|
|
|
* url /v1/filesystem/{id} |
|
78
|
|
|
* |
|
79
|
|
|
* @param mixed $id |
|
80
|
|
|
* |
|
81
|
|
|
* @return \Phalcon\Http\Response |
|
82
|
|
|
* @throws Exception |
|
83
|
|
|
*/ |
|
84
|
|
|
public function edit($id) : Response |
|
85
|
|
|
{ |
|
86
|
|
|
$file = $this->model->findFirst([ |
|
87
|
|
|
'conditions' => 'id = ?0 and companies_id = ?1 and apps_id = ?2', |
|
88
|
|
|
'bind' => [$id, $this->userData->currentCompanyId(), $this->app->getId()] |
|
89
|
|
|
]); |
|
90
|
|
|
|
|
91
|
|
|
if (!is_object($file)) { |
|
92
|
|
|
throw new UnprocessableEntityHttpException('Record not found'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$request = $this->request->getPut(); |
|
96
|
|
|
|
|
97
|
|
|
if (empty($request)) { |
|
98
|
|
|
$request = $this->request->getJsonRawBody(true); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$systemModule = $request['system_modules_id'] ?? 0; |
|
102
|
|
|
$entityId = $request['entity_id'] ?? 0; |
|
103
|
|
|
|
|
104
|
|
|
$file->system_modules_id = $systemModule; |
|
105
|
|
|
$file->entity_id = $entityId; |
|
106
|
|
|
|
|
107
|
|
|
if (!$file->update()) { |
|
108
|
|
|
throw new UnprocessableEntityHttpException((string)current($file->getMessages())); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this->response($file); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Set the validation for the files |
|
116
|
|
|
* |
|
117
|
|
|
* @return Validation |
|
118
|
|
|
*/ |
|
119
|
1 |
|
protected function validation(): Validation |
|
120
|
|
|
{ |
|
121
|
1 |
|
$validator = new Validation(); |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @todo add validation for other file types, but we need to |
|
125
|
|
|
* look for a scalable way |
|
126
|
|
|
*/ |
|
127
|
|
|
$uploadConfig = [ |
|
128
|
1 |
|
'maxSize' => '10M', |
|
129
|
|
|
'messageSize' => ':field exceeds the max filesize (:max)', |
|
130
|
|
|
'allowedTypes' => [ |
|
131
|
|
|
'image/jpeg', |
|
132
|
|
|
'image/png', |
|
133
|
|
|
], |
|
134
|
|
|
'messageType' => 'Allowed file types are :types', |
|
135
|
|
|
]; |
|
136
|
|
|
|
|
137
|
1 |
|
$validator->add( |
|
138
|
1 |
|
'file', |
|
139
|
1 |
|
new FileValidator($uploadConfig) |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
1 |
|
return $validator; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Upload the document and save them to the filesystem |
|
147
|
|
|
* |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
1 |
|
protected function processFiles(): array |
|
151
|
|
|
{ |
|
152
|
|
|
//@todo validate entity id |
|
153
|
1 |
|
$systemModule = $this->request->getPost('system_modules_id', 'int', '0'); |
|
154
|
1 |
|
$entityId = $this->request->getPost('entity_id', 'int', '0'); |
|
155
|
|
|
|
|
156
|
1 |
|
$validator = $this->validation(); |
|
157
|
|
|
|
|
158
|
1 |
|
$files = []; |
|
159
|
1 |
|
foreach ($this->request->getUploadedFiles() as $file) { |
|
160
|
|
|
//validate this current file |
|
161
|
1 |
|
$errors = $validator->validate(['file' => [ |
|
162
|
1 |
|
'name' => $file->getName(), |
|
163
|
1 |
|
'type' => $file->getType(), |
|
164
|
1 |
|
'tmp_name' => $file->getTempName(), |
|
165
|
1 |
|
'error' => $file->getError(), |
|
166
|
1 |
|
'size' => $file->getSize(), |
|
167
|
|
|
]]); |
|
168
|
|
|
|
|
169
|
1 |
|
if (!defined('API_TESTS')) { |
|
170
|
|
|
if (count($errors)) { |
|
171
|
|
|
foreach ($errors as $error) { |
|
172
|
|
|
throw new UnprocessableEntityHttpException((string)$error); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
1 |
|
$filePath = Helper::generateUniqueName($file, $this->config->filesystem->local->path); |
|
178
|
|
|
$compleFilePath = $this->config->filesystem->local->path . $filePath; |
|
179
|
|
|
|
|
180
|
|
|
$this->di->get('filesystem', 'local')->writeStream($filePath, fopen($file->getTempName(), 'r')); |
|
181
|
|
|
|
|
182
|
|
|
$fileSystem = new FileSystem(); |
|
183
|
|
|
$fileSystem->name = $file->getName(); |
|
184
|
|
|
$fileSystem->system_modules_id = $systemModule; |
|
185
|
|
|
$fileSystem->entity_id = $entityId; |
|
186
|
|
|
$fileSystem->companies_id = $this->userData->currentCompanyId(); |
|
187
|
|
|
$fileSystem->apps_id = $this->app->getId(); |
|
188
|
|
|
$fileSystem->users_id = $this->userData->getId(); |
|
189
|
|
|
$fileSystem->path = $compleFilePath; |
|
190
|
|
|
$fileSystem->url = $compleFilePath; |
|
191
|
|
|
$fileSystem->size = $file->getSize(); |
|
192
|
|
|
|
|
193
|
|
|
if (!$fileSystem->save()) { |
|
194
|
|
|
throw new UnprocessableEntityHttpException((string)current($fileSystem->getMessages())); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$files[] = $fileSystem; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
return $files; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|