/**
  * @return string
  * @throws ServerErrorHttpException
  * @throws \yii\base\InvalidConfigException
  * @throws \yii\web\BadRequestHttpException
  */
 public function run()
 {
     $params = Yii::$app->request->getBodyParams();
     $form = new ImageUploadForm(['basePath' => $this->basePath, 'baseUrl' => $this->baseUrl]);
     $form->setAttributes($params);
     $validate = Yii::$app->request->get('validate', false);
     $validateAttributeNames = null;
     if ($validate) {
         $parts = explode(',', $validate);
         $attributeNames = array_intersect($parts, $form->attributes());
         if (!empty($attributeNames[0])) {
             $validateAttributeNames = $attributeNames;
         }
     }
     $validateResult = $form->validate($validateAttributeNames);
     if ($validate || !$validate && !$validateResult) {
         return $form;
     }
     if (!$form->save() && !$form->hasErrors()) {
         HttpError::the500('Failed to upload file for unknown reason.');
     }
     $response = Yii::$app->getResponse();
     $response->setStatusCode(201);
     $response->getHeaders()->set('Location', $form->getLink());
     return $form;
 }
Exemplo n.º 2
0
 /**
  * Геттер параметров приложения
  * @param $need
  * @return mixed
  */
 public static function get($need)
 {
     $params = Yii::$app->params;
     if (!isset($params[$need])) {
         HttpError::the500("Key not found: {$need}");
     }
     return $params[$need];
 }
 /**
  * @return string
  * @throws ServerErrorHttpException
  * @throws \yii\base\InvalidConfigException
  * @throws \yii\web\BadRequestHttpException
  */
 public function run()
 {
     $params = Yii::$app->request->getBodyParams();
     $form = new ImageUploadForm(['basePath' => $this->basePath, 'baseUrl' => $this->baseUrl]);
     $form->setAttributes($params);
     if (!$form->validate()) {
         HttpError::the400();
     }
     if (!$form->save() && !$form->hasErrors()) {
         HttpError::the500('Failed to upload file for unknown reason.');
     }
     $response = Yii::$app->getResponse();
     $response->setStatusCode(201);
     $response->getHeaders()->set('Location', $form->getLink());
     return $form;
 }
 /**
  * Init variables
  * @throws \yii\web\HttpException
  * @throws \yii\web\NotFoundHttpException
  */
 public function afterValidate()
 {
     parent::afterValidate();
     $this->_pathRootObject = $this->basePath . DIRECTORY_SEPARATOR . $this->folder;
     $this->_urlRootObject = $this->baseUrl . DIRECTORY_SEPARATOR . $this->folder;
     if (!is_dir($this->_pathRootObject)) {
         HttpError::the404();
     }
     $this->_pathObject = $this->_pathRootObject . DIRECTORY_SEPARATOR . $this->id;
     $this->_urlObject = $this->_urlRootObject . DIRECTORY_SEPARATOR . $this->id;
     if (!is_dir($this->_pathObject) && !FileHelper::createDirectory($this->_pathObject)) {
         HttpError::the500('Can not create directory: ' . $this->_pathObject);
     }
     $this->_pathObjectFile = $this->_pathObject . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->name;
     $this->_urlObjectFile = $this->_urlObject . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->name;
     $this->_pathObjectFileOrigin = $this->_pathObject . DIRECTORY_SEPARATOR . $this->name;
     $this->_pathObjectPlaceholder = $this->_pathRootObject . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->placeholder;
     $this->_urlObjectPlaceholder = $this->_urlRootObject . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->placeholder;
     $this->_pathObjectPlaceholderOrigin = $this->_pathRootObject . DIRECTORY_SEPARATOR . $this->placeholder;
     $this->_pathRootPlaceholder = $this->basePath . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->placeholder;
     $this->_urlRootPlaceholder = $this->baseUrl . DIRECTORY_SEPARATOR . $this->width . 'x' . $this->height . '_' . $this->placeholder;
     $this->_pathRootPlaceholderOrigin = $this->basePath . DIRECTORY_SEPARATOR . $this->placeholder;
 }