/** * @operationName("Upload Encoded Media") * @operationDescription("Upload Base64 encoded media (from Browser drag and drop)") */ public function encodeAction() { if (!$this->request->isPost()) { $this->response->setStatusCode('400', 'No Upload Files'); return $this->response->setJsonContent(array('errors' => array(array('code' => 400, 'message' => 'ERR_FILE_NO_UPLOAD')))); } $upload = new Models\Upload(); $fileinfo = array(); try { $file = $upload->uploadByEncodedData($this->request->getPost('file'), $this->request->getPost('name'), $this->request->getPost('type')); if ($file) { $fileinfo = $file->toArray(); $fileinfo['localUrl'] = $file->getLocalUrl(); } } catch (\Exception $e) { return $this->showExceptionAsJson($e, $upload->getMessages()); } return $this->response->setJsonContent($fileinfo); }
/** * * @SWG\Api( * path="/admin/media", * description="Media related api", * produces="['application/json']", * @SWG\Operations( * @SWG\Operation( * method="POST", * summary="Create new media", * notes="Returns a media based on ID", * @SWG\Parameters( * @SWG\Parameter( * name="upload", * description="Media info", * paramType="body", * required=true, * type="file" * ) * ) * ) * ) * ) * @operationName("上传文件") * @operationDescription("上传文件") */ public function postAction() { if (!$this->request->isPost() || !$this->request->hasFiles()) { throw new Exception\InvalidArgumentException('No data input'); } $upload = new Models\Upload(); try { $files = $this->request->getUploadedFiles(); //Only allow upload the first file by force $file = $files[0]; $file = $upload->upload($file); if ($file) { $data = $file->dump(Models\FileManager::$defaultDump); return $this->response->setJsonContent($data); } } catch (\Exception $e) { return $this->showExceptionAsJson($e, $upload->getMessages()); } }