/**
  * @inheritdoc
  */
 public function run()
 {
     if (Yii::$app->request->isPost) {
         $file = UploadedFile::getInstanceByName($this->uploadParam);
         $model = new DynamicModel(compact($this->uploadParam));
         $model->addRule($this->uploadParam, 'file', ['maxSize' => $this->maxSize, 'tooBig' => Yii::t('upload', 'TOO_BIG_ERROR', ['size' => $this->maxSize / (1024 * 1024)]), 'extensions' => explode(', ', $this->extensions), 'checkExtensionByMimeType' => false, 'wrongExtension' => Yii::t('upload', 'EXTENSION_ERROR', ['formats' => $this->extensions])])->validate();
         if ($model->hasErrors()) {
             $result = ['error' => $model->getFirstError($this->uploadParam)];
         } else {
             $model->{$this->uploadParam}->name = uniqid() . '.' . $model->{$this->uploadParam}->extension;
             $request = Yii::$app->request;
             $image_name = $this->temp_path . $model->{$this->uploadParam}->name;
             $image = Image::crop($file->tempName . $request->post('filename'), intval($request->post('w')), intval($request->post('h')), [$request->post('x'), $request->post('y')])->resize(new Box($this->width, $this->height))->save($image_name);
             // watermark
             if ($this->watermark != '') {
                 $image = Image::watermark($image_name, $this->watermark)->save($image_name);
             }
             if ($image->save($image_name)) {
                 // create Thumbnail
                 if ($this->thumbnail && ($this->thumbnail_width > 0 && $this->thumbnail_height > 0)) {
                     Image::thumbnail($this->temp_path . $model->{$this->uploadParam}->name, $this->thumbnail_width, $this->thumbnail_height)->save($this->temp_path . '/thumbs/' . $model->{$this->uploadParam}->name);
                 }
                 $result = ['filelink' => $model->{$this->uploadParam}->name];
             } else {
                 $result = ['error' => Yii::t('upload', 'ERROR_CAN_NOT_UPLOAD_FILE')];
             }
         }
         Yii::$app->response->format = Response::FORMAT_JSON;
         return $result;
     } else {
         throw new BadRequestHttpException(Yii::t('upload', 'ONLY_POST_REQUEST'));
     }
 }
 /**
  * @inheritdoc
  */
 public function run()
 {
     $result = ['err' => 1, 'msg' => 'swfupload init'];
     if (Yii::$app->request->isPost) {
         $data = Yii::$app->request->get('data');
         $data = Json::decode(base64_decode($data));
         $url = $data['url'];
         $path = $data['path'];
         $params = $data['params'];
         $callback = $data['callback'];
         $file = UploadedFile::getInstanceByName('FileData');
         $model = new DynamicModel(compact('file'));
         $model->addRule('file', 'image', [])->validate();
         if ($model->hasErrors()) {
             $result['msg'] = $model->getFirstError('file');
         } else {
             if ($model->file->extension) {
                 $model->file->name = uniqid() . '.' . $model->file->extension;
             }
             if ($model->file->saveAs($path . $model->file->name)) {
                 $result['err'] = 0;
                 $result['msg'] = 'success';
                 $result['url'] = $model->file->name;
                 $result['params'] = $params;
                 $result['callback'] = $callback;
             } else {
                 $result['msg'] = $model->getFirstError('file save error!');
             }
         }
     }
     Yii::$app->response->format = Response::FORMAT_JSON;
     return $result;
 }
Example #3
0
 /**
  * @inheritdoc
  */
 public function run()
 {
     if (Yii::$app->request->isPost) {
         $file = UploadedFile::getInstanceByName($this->uploadParam);
         $model = new DynamicModel(compact($this->uploadParam));
         $model->addRule($this->uploadParam, 'image', ['maxSize' => $this->maxSize, 'tooBig' => Yii::t('cropper', 'TOO_BIG_ERROR', ['size' => $this->maxSize / (1024 * 1024)]), 'extensions' => explode(', ', $this->extensions), 'wrongExtension' => Yii::t('cropper', 'EXTENSION_ERROR', ['formats' => $this->extensions])])->validate();
         if ($model->hasErrors()) {
             $result = ['error' => $model->getFirstError($this->uploadParam)];
         } else {
             $model->{$this->uploadParam}->name = uniqid() . '.' . $model->{$this->uploadParam}->extension;
             $request = Yii::$app->request;
             $width = $request->post('width', $this->width);
             $height = $request->post('height', $this->height);
             $image = Image::crop($file->tempName . $request->post('filename'), intval($request->post('w')), intval($request->post('h')), [$request->post('x'), $request->post('y')])->resize(new Box($width, $height));
             if ($image->save($this->path . $model->{$this->uploadParam}->name)) {
                 $result = ['filelink' => $this->url . $model->{$this->uploadParam}->name];
             } else {
                 $result = ['error' => Yii::t('cropper', 'ERROR_CAN_NOT_UPLOAD_FILE')];
             }
         }
         Yii::$app->response->format = Response::FORMAT_JSON;
         return $result;
     } else {
         throw new BadRequestHttpException(Yii::t('cropper', 'ONLY_POST_REQUEST'));
     }
 }
 public function actionData($file = false)
 {
     \Yii::$app->response->format = Response::FORMAT_JSON;
     if (\Yii::$app->request->isPost) {
         /**@var Module $module */
         $module = Module::getInstance();
         $model = new DynamicModel(['file' => null]);
         $model->addRule('file', 'file', ['extensions' => $module->expansions, 'maxSize' => $module->maxSize]);
         $model->file = UploadedFile::getInstanceByName('image');
         if ($model->validate()) {
             if (!is_dir(\Yii::getAlias($module->uploadDir))) {
                 FileHelper::createDirectory(\Yii::getAlias($module->uploadDir));
             }
             $oldFileName = $model->file->name;
             $newFileName = $module->isFileNameUnique ? uniqid() . '.' . $model->file->extension : $oldFileName;
             $newFullFileName = \Yii::getAlias($module->uploadDir) . DIRECTORY_SEPARATOR . $newFileName;
             if ($model->file->saveAs($newFullFileName)) {
                 return ['id' => $oldFileName, 'url' => \Yii::$app->request->getHostInfo() . str_replace('@webroot', '', $module->uploadDir) . '/' . $newFileName];
             }
         } else {
             \Yii::$app->response->statusCode = 500;
             return $model->getFirstError('file');
         }
     } elseif (\Yii::$app->request->isDelete && $file) {
         return true;
     }
     throw new BadRequestHttpException();
 }
 public function run()
 {
     $file = UploadedFile::getInstanceByName($this->inputName);
     if (!$file) {
         return $this->response(['error' => Yii::t('filemanager-yii2', 'An error occured, try again later…')]);
     }
     $model = new DynamicModel(compact('file'));
     $model->addRule('file', $this->type, $this->rules)->validate();
     if ($model->hasErrors()) {
         return $this->response(['error' => $model->getFirstError('file')]);
     } else {
         return $this->upload($file);
     }
 }
Example #6
0
 /**
  * Redactor upload action.
  *
  * @param string $name Model name
  * @param string $attr Model attribute
  * @param string $type Format type
  * @return array List of files
  * @throws BadRequestHttpException
  * @throws InvalidConfigException
  */
 public function actionRedactorUpload($name, $attr, $type = 'image')
 {
     /** @var $module \vladdnepr\ycm\Module */
     $module = $this->module;
     $name = (string) $name;
     $attribute = (string) $attr;
     $uploadType = 'image';
     $validatorOptions = $module->redactorImageUploadOptions;
     if ((string) $type == 'file') {
         $uploadType = 'file';
         $validatorOptions = $module->redactorFileUploadOptions;
     }
     $attributePath = $module->getAttributePath($name, $attribute);
     if (!is_dir($attributePath)) {
         if (!FileHelper::createDirectory($attributePath, $module->uploadPermissions)) {
             throw new InvalidConfigException('Could not create folder "' . $attributePath . '". Make sure "uploads" folder is writable.');
         }
     }
     $file = UploadedFile::getInstanceByName('file');
     $model = new DynamicModel(compact('file'));
     $model->addRule('file', $uploadType, $validatorOptions)->validate();
     if ($model->hasErrors()) {
         $result = ['error' => $model->getFirstError('file')];
     } else {
         if ($model->file->extension) {
             $model->file->name = md5($attribute . time() . uniqid(rand(), true)) . '.' . $model->file->extension;
         }
         $path = $attributePath . DIRECTORY_SEPARATOR . $model->file->name;
         if ($model->file->saveAs($path)) {
             $result = ['filelink' => $module->getAttributeUrl($name, $attribute, $model->file->name)];
             if ($uploadType == 'file') {
                 $result['filename'] = $model->file->name;
             }
         } else {
             $result = ['error' => Yii::t('ycm', 'Could not upload file.')];
         }
     }
     Yii::$app->response->format = Response::FORMAT_JSON;
     return $result;
 }
Example #7
0
 public function run()
 {
     $file = UploadedFile::getInstanceByName($this->inputName);
     if (!$file) {
         return $this->response(['error' => Yii::t('filemanager-yii2', 'An error occured, try again later…')]);
     }
     $rules = $this->model->fileRules($this->attribute, true);
     $type = $this->model->fileOption($this->attribute, 'type', 'image');
     $model = new DynamicModel(compact('file'));
     $maxFiles = ArrayHelper::getValue($rules, 'maxFiles');
     if ($maxFiles !== null && $maxFiles > 1) {
         $model->file = [$model->file];
     }
     $model->addRule('file', $type, $rules)->validate();
     if ($model->hasErrors()) {
         return $this->response(['error' => $model->getFirstError('file')]);
     }
     if (is_array($model->file)) {
         $model->file = $model->file[0];
     }
     return $this->save($model->file);
 }
Example #8
0
 /**
  * @inheritdoc
  */
 public function run()
 {
     if (Yii::$app->request->isPost) {
         $file = UploadedFile::getInstanceByName($this->paramName);
         $model = new DynamicModel(compact('file'));
         $model->addRule('file', $this->_validator, $this->validatorOptions)->validate();
         if ($model->hasErrors()) {
             $result = ['error' => $model->getFirstError('file')];
         } else {
             if ($this->unique === true && $model->file->extension) {
                 $model->file->name = uniqid() . '.' . $model->file->extension;
             }
             if ($model->file->saveAs($this->path . $model->file->name)) {
                 $result = ['name' => $model->file->name];
             } else {
                 $result = ['error' => Widget::t('fileapi', 'ERROR_CAN_NOT_UPLOAD_FILE')];
             }
         }
         Yii::$app->response->format = Response::FORMAT_JSON;
         return $result;
     } else {
         throw new BadRequestHttpException('Only POST is allowed');
     }
 }
 public function getError()
 {
     return ['status' => 'error', 'message' => $this->model->getFirstError('checkField')];
 }
 /**
  * @inheritdoc
  */
 public function run()
 {
     if (Yii::$app->request->isPost) {
         $file = UploadedFile::getInstanceByName($this->uploadParam);
         $model = new DynamicModel(compact('file'));
         $model->addRule('file', $this->_validator, $this->validatorOptions)->validate();
         if ($model->hasErrors()) {
             $result = ['error' => $model->getFirstError('file')];
         } else {
             if ($this->unique === true && $model->file->extension) {
                 $model->file->name = uniqid() . '.' . $model->file->extension;
             }
             if ($model->file->saveAs($this->imagePath . $model->file->name)) {
                 //crop image
                 Image::thumbnail($this->imagePath . $model->file->name, $this->options['thumbWidth'], $this->options['thumbHeight'])->save($this->thumbPath . $model->file->name, ['quality' => 90]);
                 //resize image maintaining aspect ratio
                 Image::frame($this->imagePath . $model->file->name, 0, '666', 0)->thumbnail(new Box($this->options['imageWidth'], $this->options['imageHeight']))->save($this->imagePath . $model->file->name, ['quality' => 90]);
                 $result = ['filelink' => $this->url . $model->file->name];
             } else {
                 $result = ['error' => Yii::t('vova07/imperavi', 'ERROR_CAN_NOT_UPLOAD_FILE')];
             }
         }
         Yii::$app->response->format = Response::FORMAT_JSON;
         return $result;
     } else {
         throw new BadRequestHttpException('Only POST is allowed');
     }
 }
Example #11
0
 public function getError()
 {
     return ['status' => 'error', 'message' => $this->model->getFirstError('payAmount')];
 }
 public function actionUploadAvatar()
 {
     if (Yii::$app->user->isGuest) {
         throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
     }
     Yii::$app->response->format = Response::FORMAT_JSON;
     $model = new DynamicModel(['image']);
     $model->addRule('image', 'file', ['skipOnEmpty' => false, 'extensions' => 'png, jpg']);
     if (Yii::$app->request->isPost) {
         $model->image = UploadedFile::getInstanceByName('image');
         if ($model->validate()) {
             try {
                 return AvatarHelper::saveAvatar($model->image);
             } catch (Exception $exc) {
                 Yii::$app->response->statusCode = 400;
                 return Yii::t('yee', 'An unknown error occurred.');
             }
         } else {
             $errors = $model->getErrors();
             Yii::$app->response->statusCode = 400;
             return $model->getFirstError(key($errors));
         }
     }
     return;
 }
 public function testValidateOperatorGreaterOrEqual()
 {
     $compareErrorText = Yii::t('app', '{attribute} must be greater than or equal to "{compareValue}".', ['attribute' => 'Date From', 'compareValue' => '2016-01-02']);
     $model = new DynamicModel(['date_from' => '2016-01-01', 'date_to' => '2016-01-02']);
     $model->addRule(['date_from', 'date_to'], 'required');
     $model->addRule(['date_from'], DateTimeCompareValidator::className(), ['compareAttribute' => 'date_to', 'format' => 'Y-m-d', 'operator' => '>=']);
     $model->validate();
     $errorText = $model->getFirstError('date_from');
     $this->assertEquals($errorText, $compareErrorText);
     $model = new DynamicModel(['date_from' => '2016-01-01', 'date_to' => '2016-01-01']);
     $model->addRule(['date_from', 'date_to'], 'required');
     $model->addRule(['date_from'], DateTimeCompareValidator::className(), ['compareAttribute' => 'date_to', 'format' => 'Y-m-d', 'operator' => '>=']);
     $model->validate();
     $errorText = $model->getFirstError('date_from');
     $this->assertNull($errorText);
     $model = new DynamicModel(['date_from' => '2016-01-02', 'date_to' => '2016-01-01']);
     $model->addRule(['date_from', 'date_to'], 'required');
     $model->addRule(['date_from'], DateTimeCompareValidator::className(), ['compareAttribute' => 'date_to', 'format' => 'Y-m-d', 'operator' => '>=']);
     $model->validate();
     $errorText = $model->getFirstError('date_from');
     $this->assertNull($errorText);
 }
 /**
  * @inheritdoc
  */
 public function run()
 {
     if (Yii::$app->request->isPost) {
         $file = UploadedFile::getInstanceByName($this->uploadParam);
         $model = new DynamicModel(compact('file'));
         $model->addRule('file', $this->_validator, $this->validatorOptions)->validate();
         if ($model->hasErrors()) {
             $result = ['error' => $model->getFirstError('file')];
         } else {
             if ($this->unique === true && $model->file->extension) {
                 $md5Filename = md5($model->file->name . '_' . rand(100000, 999999) . '_' . time());
                 $filename = $md5Filename[0] . '/' . $md5Filename[1] . '/' . $md5Filename[2] . '/' . $md5Filename . '.' . $model->file->extension;
                 $model->file->name = $filename;
             }
             $uploadResult = $this->storage->uploadFile($model->file->tempName, $model->file->name);
             if ($uploadResult) {
                 if (isset($this->options['baseUrl'])) {
                     $uploadResult = Yii::getAlias($this->options['baseUrl'] . $filename);
                 }
                 $result = ['filelink' => $uploadResult];
                 if ($this->uploadOnlyImage !== true) {
                     $result['filename'] = $model->file->name;
                 }
             } else {
                 $result = ['error' => Yii::t('vova07/imperavi', 'ERROR_CAN_NOT_UPLOAD_FILE')];
             }
         }
         Yii::$app->response->format = Response::FORMAT_JSON;
         return $result;
     } else {
         throw new BadRequestHttpException('Only POST is allowed');
     }
 }