| Conditions | 16 |
| Paths | 33 |
| Total Lines | 91 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 147 | public function postAvatar($path) { |
||
| 148 | $files = $this->request->getUploadedFile('files'); |
||
| 149 | |||
| 150 | if (isset($path)) { |
||
| 151 | $path = stripslashes($path); |
||
| 152 | $userFolder = $this->rootFolder->getUserFolder($this->userId); |
||
| 153 | /** @var File $node */ |
||
| 154 | $node = $userFolder->get($path); |
||
| 155 | if (!($node instanceof File)) { |
||
|
|
|||
| 156 | return new JSONResponse(['data' => ['message' => $this->l->t('Please select a file.')]]); |
||
| 157 | } |
||
| 158 | if ($node->getSize() > 20*1024*1024) { |
||
| 159 | return new JSONResponse( |
||
| 160 | ['data' => ['message' => $this->l->t('File is too big')]], |
||
| 161 | Http::STATUS_BAD_REQUEST |
||
| 162 | ); |
||
| 163 | } |
||
| 164 | |||
| 165 | if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') { |
||
| 166 | return new JSONResponse( |
||
| 167 | ['data' => ['message' => $this->l->t('The selected file is not an image.')]], |
||
| 168 | Http::STATUS_BAD_REQUEST |
||
| 169 | ); |
||
| 170 | } |
||
| 171 | |||
| 172 | try { |
||
| 173 | $content = $node->getContent(); |
||
| 174 | } catch (\OCP\Files\NotPermittedException $e) { |
||
| 175 | return new JSONResponse( |
||
| 176 | ['data' => ['message' => $this->l->t('The selected file cannot be read.')]], |
||
| 177 | Http::STATUS_BAD_REQUEST |
||
| 178 | ); |
||
| 179 | } |
||
| 180 | } elseif (!is_null($files)) { |
||
| 181 | if ( |
||
| 182 | $files['error'][0] === 0 && |
||
| 183 | is_uploaded_file($files['tmp_name'][0]) && |
||
| 184 | !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0]) |
||
| 185 | ) { |
||
| 186 | if ($files['size'][0] > 20*1024*1024) { |
||
| 187 | return new JSONResponse( |
||
| 188 | ['data' => ['message' => $this->l->t('File is too big')]], |
||
| 189 | Http::STATUS_BAD_REQUEST |
||
| 190 | ); |
||
| 191 | } |
||
| 192 | $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200); |
||
| 193 | $content = $this->cache->get('avatar_upload'); |
||
| 194 | unlink($files['tmp_name'][0]); |
||
| 195 | } else { |
||
| 196 | return new JSONResponse( |
||
| 197 | ['data' => ['message' => $this->l->t('Invalid file provided')]], |
||
| 198 | Http::STATUS_BAD_REQUEST |
||
| 199 | ); |
||
| 200 | } |
||
| 201 | } else { |
||
| 202 | //Add imgfile |
||
| 203 | return new JSONResponse( |
||
| 204 | ['data' => ['message' => $this->l->t('No image or file provided')]], |
||
| 205 | Http::STATUS_BAD_REQUEST |
||
| 206 | ); |
||
| 207 | } |
||
| 208 | |||
| 209 | try { |
||
| 210 | $image = new \OC_Image(); |
||
| 211 | $image->loadFromData($content); |
||
| 212 | $image->readExif($content); |
||
| 213 | $image->fixOrientation(); |
||
| 214 | |||
| 215 | if ($image->valid()) { |
||
| 216 | $mimeType = $image->mimeType(); |
||
| 217 | if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { |
||
| 218 | return new JSONResponse( |
||
| 219 | ['data' => ['message' => $this->l->t('Unknown filetype')]], |
||
| 220 | Http::STATUS_OK |
||
| 221 | ); |
||
| 222 | } |
||
| 223 | |||
| 224 | $this->cache->set('tmpAvatar', $image->data(), 7200); |
||
| 225 | return new JSONResponse( |
||
| 226 | ['data' => 'notsquare'], |
||
| 227 | Http::STATUS_OK |
||
| 228 | ); |
||
| 229 | } else { |
||
| 230 | return new JSONResponse( |
||
| 231 | ['data' => ['message' => $this->l->t('Invalid image')]], |
||
| 232 | Http::STATUS_OK |
||
| 233 | ); |
||
| 234 | } |
||
| 235 | } catch (\Exception $e) { |
||
| 236 | $this->logger->logException($e, ['app' => 'core']); |
||
| 237 | return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK); |
||
| 238 | } |
||
| 327 |