1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Canvas\Traits; |
6
|
|
|
|
7
|
|
|
use Phalcon\Http\Response; |
8
|
|
|
use Phalcon\Validation; |
9
|
|
|
use Phalcon\Validation\Validator\File as FileValidator; |
|
|
|
|
10
|
|
|
use Canvas\Exception\UnprocessableEntityHttpException; |
11
|
|
|
use Canvas\Models\FileSystem; |
12
|
|
|
use Canvas\Filesystem\Helper; |
13
|
|
|
use Baka\Http\QueryParser; |
|
|
|
|
14
|
|
|
use Canvas\Models\FileSystemSettings; |
15
|
|
|
use Canvas\Models\SystemModules; |
16
|
|
|
use Canvas\Models\FileSystemEntities; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Trait ResponseTrait. |
20
|
|
|
* |
21
|
|
|
* @package Canvas\Traits |
22
|
|
|
* |
23
|
|
|
* @property Users $user |
24
|
|
|
* @property AppsPlans $appPlan |
25
|
|
|
* @property CompanyBranches $branches |
26
|
|
|
* @property Companies $company |
27
|
|
|
* @property UserCompanyApps $app |
28
|
|
|
* @property \Phalcon\Di $di |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
trait FileManagementTrait |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Get item. |
35
|
|
|
* |
36
|
|
|
* @method GET |
37
|
|
|
* url /v1/filesystem/{id} |
38
|
|
|
* |
39
|
|
|
* @param mixed $id |
40
|
|
|
* |
41
|
|
|
* @return \Phalcon\Http\Response |
42
|
|
|
* @throws Exception |
43
|
|
|
*/ |
44
|
|
|
public function getById($id) : Response |
45
|
|
|
{ |
46
|
|
|
$records = FileSystem::findFirstOrFail($id); |
47
|
|
|
|
48
|
|
|
//get relationship |
49
|
|
|
if ($this->request->hasQuery('relationships')) { |
50
|
|
|
$relationships = $this->request->getQuery('relationships', 'string'); |
51
|
|
|
$records = QueryParser::parseRelationShips($relationships, $records); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->response($records); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Add a new item. |
59
|
|
|
* |
60
|
|
|
* @method POST |
61
|
|
|
* url /v1/filesystem |
62
|
|
|
* |
63
|
|
|
* @return \Phalcon\Http\Response |
64
|
|
|
* @throws Exception |
65
|
|
|
*/ |
66
|
|
|
public function create() : Response |
67
|
|
|
{ |
68
|
|
|
if (!$this->request->hasFiles()) { |
69
|
|
|
/** |
70
|
|
|
* @todo handle file hash to avoid uploading same files again |
71
|
|
|
*/ |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->response($this->processFiles()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Update an item. |
79
|
|
|
* |
80
|
|
|
* @method PUT |
81
|
|
|
* url /v1/filesystem/{id} |
82
|
|
|
* |
83
|
|
|
* @param mixed $id |
84
|
|
|
* |
85
|
|
|
* @return \Phalcon\Http\Response |
86
|
|
|
* @throws Exception |
87
|
|
|
*/ |
88
|
|
|
public function edit($id) : Response |
89
|
|
|
{ |
90
|
|
|
/** |
91
|
|
|
* @todo update filesystem move function |
92
|
|
|
* limit the file for this app / company |
93
|
|
|
* provide a method either here or another to move the file to a new location |
94
|
|
|
* calling the method move of the same object , right now we can do it internally but for |
95
|
|
|
* exposing the api we will need this |
96
|
|
|
*/ |
97
|
|
|
$file = FileSystem::findFirstOrFail($id); |
98
|
|
|
|
99
|
|
|
$request = $this->request->getPutData(); |
100
|
|
|
|
101
|
|
|
$systemModule = $request['system_modules_id'] ?? 0; |
102
|
|
|
$entityId = $request['entity_id'] ?? 0; |
103
|
|
|
$fieldName = $request['field_name'] ?? ''; |
104
|
|
|
|
105
|
|
|
//associate |
106
|
|
|
$fileSystemEntities = new FileSystemEntities(); |
107
|
|
|
$fileSystemEntities->filesystem_id = $file->getId(); |
108
|
|
|
$fileSystemEntities->entity_id = $entityId; |
109
|
|
|
$fileSystemEntities->companies_id = $file->companies_id; |
110
|
|
|
$fileSystemEntities->system_modules_id = $systemModule; |
111
|
|
|
$fileSystemEntities->field_name = $fieldName; |
112
|
|
|
$fileSystemEntities->saveOrFail(); |
113
|
|
|
|
114
|
|
|
$file->updateOrFail($request, $this->updateFields); |
115
|
|
|
|
116
|
|
|
return $this->response($file); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Delete a file atribute. |
121
|
|
|
* |
122
|
|
|
* @param $id |
123
|
|
|
* @param string $name |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function deleteAttributes($id, string $name): Response |
127
|
|
|
{ |
128
|
|
|
$records = FileSystem::findFirstOrFail($id); |
129
|
|
|
|
130
|
|
|
$recordAttributes = FileSystemSettings::findFirstOrFail([ |
131
|
|
|
'conditions' => 'filesystem_id = ?0 and name = ?1', |
132
|
|
|
'bind' => [$records->getId(), $name] |
133
|
|
|
]); |
134
|
|
|
|
135
|
|
|
//true true delete |
136
|
|
|
$recordAttributes->delete(); |
137
|
|
|
|
138
|
|
|
return $this->response(['Delete Successfully']); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the validation for the files. |
143
|
|
|
* |
144
|
|
|
* @return Validation |
145
|
|
|
*/ |
146
|
|
|
protected function validation(): Validation |
147
|
|
|
{ |
148
|
|
|
$validator = new Validation(); |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @todo add validation for other file types, but we need to |
152
|
|
|
* look for a scalable way |
153
|
|
|
*/ |
154
|
|
|
$uploadConfig = [ |
155
|
|
|
'maxSize' => '100M', |
156
|
|
|
'messageSize' => ':field exceeds the max filesize (:max)', |
157
|
|
|
'allowedTypes' => [ |
158
|
|
|
'image/jpeg', |
159
|
|
|
'image/png', |
160
|
|
|
'image/webp', |
161
|
|
|
'audio/mpeg', |
162
|
|
|
'audio/mp3', |
163
|
|
|
'audio/mpeg', |
164
|
|
|
'application/pdf', |
165
|
|
|
'audio/mpeg3', |
166
|
|
|
'audio/x-mpeg-3', |
167
|
|
|
'application/x-zip-compressed', |
168
|
|
|
'application/octet-stream', |
169
|
|
|
], |
170
|
|
|
'messageType' => 'Allowed file types are :types', |
171
|
|
|
]; |
172
|
|
|
|
173
|
|
|
$validator->add( |
174
|
|
|
'file', |
175
|
|
|
new FileValidator($uploadConfig) |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
return $validator; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Upload the document and save them to the filesystem. |
183
|
|
|
* @todo add test |
184
|
|
|
* |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
|
|
protected function processFiles(): array |
188
|
|
|
{ |
189
|
|
|
$allFields = $this->request->getPostData(); |
190
|
|
|
|
191
|
|
|
$validator = $this->validation(); |
192
|
|
|
|
193
|
|
|
$files = []; |
194
|
|
|
foreach ($this->request->getUploadedFiles() as $file) { |
195
|
|
|
//validate this current file |
196
|
|
|
$errors = $validator->validate(['file' => [ |
197
|
|
|
'name' => $file->getName(), |
198
|
|
|
'type' => $file->getType(), |
199
|
|
|
'tmp_name' => $file->getTempName(), |
200
|
|
|
'error' => $file->getError(), |
201
|
|
|
'size' => $file->getSize(), |
202
|
|
|
]]); |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @todo figure out why this failes |
206
|
|
|
*/ |
207
|
|
|
if (!defined('API_TESTS')) { |
208
|
|
|
if (count($errors)) { |
209
|
|
|
foreach ($errors as $error) { |
210
|
|
|
throw new UnprocessableEntityHttpException((string)$error); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$fileSystem = Helper::upload($file); |
216
|
|
|
|
217
|
|
|
//add settings |
218
|
|
|
foreach ($allFields as $key => $settings) { |
219
|
|
|
$fileSystem->set($key, $settings); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$files[] = $fileSystem; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $files; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths