/** * Upload file to server. * * @param bool $printResponse * * @return array * @throws \yii\web\NotFoundHttpException */ public function post($printResponse = true) { if (Yii::$app->request->get('delete') && ($id = Yii::$app->request->get('id'))) { return $this->delete($id, $printResponse); } $response = []; $this->_media = new Media(); $this->_media->file = UploadedFile::getInstance($this->_media, 'file'); if ($this->_media->file !== null && $this->_media->validate(['file'])) { if ($post_id = Yii::$app->request->get('post_id')) { $post = $this->findPost($post_id); $this->_media->media_post_id = $post->id; } $this->_media->media_title = $this->_media->file->baseName; $this->_media->media_mime_type = $this->_media->file->type; $this->handleFileUpload($this->_media->file); if ($this->_media->save()) { if ($this->_media->setMeta('metadata', $this->_meta)) { $response = $this->generateResponse($this->_media); } } } else { $response[] = ['media_error' => $this->_media->getErrors('file'), 'media_filename' => isset($this->_media->file->name) ? $this->_media->file->name : null, 'media_file_size' => isset($this->_media->file->size) ? $this->_media->file->size : null]; } $this->setResponse([$this->getOption('param_name') => [$response]]); return $this->getResponse($printResponse); }