/** * @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')); } }
public function testValidateAttribute() { $this->tester->haveInCollection(Number::collectionName(), ['number' => '9101234567']); $this->specify("Test validate", function ($number, $expected) { $this->model['number'] = $number; $this->model->validate(['number']); $this->tester->assertEquals($expected, $this->model->hasErrors()); }, ['examples' => [['9101234567', false], ['1111111111', true], ['9121111111', true]]]); }
public function testValidateWithAddRule() { $email = 'invalid'; $name = 'long name'; $age = ''; $model = new DynamicModel(compact('name', 'email', 'age')); $model->addRule(['email', 'name', 'age'], 'required')->addRule('email', 'email')->addRule('name', 'string', ['max' => 3])->validate(); $this->assertTrue($model->hasErrors()); $this->assertTrue($model->hasErrors('email')); $this->assertTrue($model->hasErrors('name')); $this->assertTrue($model->hasErrors('age')); }
/** * @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')); } }
/** * @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; }
/** * Save photo * * @param \app\models\UserProfile $profile * @param string $photo * @return void */ private function savePhoto($profile, $photo) { $file = $this->makeUploadedFile($photo); $model = new DynamicModel(compact('file')); $model->addRule('file', 'image', $profile->fileRules('photo', true))->validate(); if (!$model->hasErrors()) { $profile->createFile('photo', $file->tempName, $model->file->name); } }
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); } }
/** * 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; }
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); }
/** * @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 hasError() { return $this->model->hasErrors(); }
/** * @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'); } }
/** * @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'); } }