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); |
||
1 ignored issue
–
show
Bug
introduced
by
![]() |
|||
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 | public function create() : Response |
||
65 | { |
||
66 | if (!$this->request->hasFiles()) { |
||
67 | //@todo handle base64 images |
||
68 | } |
||
69 | |||
70 | 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 | protected function validation(): Validation |
||
120 | { |
||
121 | $validator = new Validation(); |
||
122 | |||
123 | $uploadConfig = [ |
||
124 | 'maxSize' => '10M', |
||
125 | 'messageSize' => ':field exceeds the max filesize (:max)', |
||
126 | 'allowedTypes' => [ |
||
127 | 'image/jpeg', |
||
128 | 'image/png', |
||
129 | ], |
||
130 | 'messageType' => 'Allowed file types are :types', |
||
131 | ]; |
||
132 | |||
133 | $validator->add( |
||
134 | 'file', |
||
135 | new FileValidator($uploadConfig) |
||
136 | ); |
||
137 | |||
138 | return $validator; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Upload the document and save them to the filesystem |
||
143 | * |
||
144 | * @param object? $fileObject |
||
145 | * @return array |
||
146 | */ |
||
147 | protected function processFiles(): array |
||
148 | { |
||
149 | //@todo validate entity id |
||
150 | $systemModule = $this->request->getPost('system_modules_id', 'int', '0'); |
||
151 | $entityId = $this->request->getPost('entity_id', 'int', '0'); |
||
152 | |||
153 | $validator = $this->validation(); |
||
154 | |||
155 | $files = []; |
||
156 | foreach ($this->request->getUploadedFiles() as $file) { |
||
157 | //validate this current file |
||
158 | $errors = $validator->validate(['file' => [ |
||
159 | 'name' => $file->getName(), |
||
160 | 'type' => $file->getType(), |
||
161 | 'tmp_name' => $file->getTempName(), |
||
162 | 'error' => $file->getError(), |
||
163 | 'size' => $file->getSize(), |
||
164 | ]]); |
||
165 | |||
166 | if (!defined('API_TESTS')) { |
||
167 | if (count($errors)) { |
||
168 | foreach ($errors as $error) { |
||
169 | throw new UnprocessableEntityHttpException((string)$error); |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | |||
174 | $filePath = Helper::generateUniqueName($file, $this->config->filesystem->local->path); |
||
175 | $compleFilePath = $this->config->filesystem->local->path . $filePath; |
||
176 | |||
177 | $this->di->get('filesystem', 'local')->writeStream($filePath, fopen($file->getTempName(), 'r')); |
||
178 | |||
179 | $fileSystem = new FileSystem(); |
||
180 | $fileSystem->name = $file->getName(); |
||
181 | $fileSystem->system_modules_id = $systemModule; |
||
182 | $fileSystem->entity_id = $entityId; |
||
183 | $fileSystem->companies_id = $this->userData->currentCompanyId(); |
||
184 | $fileSystem->apps_id = $this->app->getId(); |
||
185 | $fileSystem->users_id = $this->userData->getId(); |
||
186 | $fileSystem->path = $compleFilePath; |
||
187 | $fileSystem->url = $compleFilePath; |
||
188 | $fileSystem->size = $file->getSize(); |
||
189 | |||
190 | if (!$fileSystem->save()) { |
||
191 | throw new UnprocessableEntityHttpException((string)current($fileSystem->getMessages())); |
||
192 | } |
||
193 | |||
194 | $files[] = $fileSystem; |
||
195 | } |
||
196 | |||
197 | return $files; |
||
198 | } |
||
199 | } |
||
200 |