From 92bcd92f6077509738940ce1d4cbb4f429f74881 Mon Sep 17 00:00:00 2001 From: iron Date: Thu, 7 Nov 2019 19:08:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9Eupload=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + common/config/main.php | 3 +- kcadmin/controllers/CategoryController.php | 9 +- kcadmin/controllers/SiteController.php | 3 + kcadmin/views/category/index.php | 2 +- vendor/iron/actions/UploadAction.php | 176 +++++++++++++++++++++ 6 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 vendor/iron/actions/UploadAction.php diff --git a/.gitignore b/.gitignore index 80cb2c7..e6d67bc 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,4 @@ phpunit.phar /.vagrant vendor.zip /vagrant +/kcadmin/web/uploads diff --git a/common/config/main.php b/common/config/main.php index f8a7b85..b8b2dd0 100644 --- a/common/config/main.php +++ b/common/config/main.php @@ -4,7 +4,8 @@ return [ 'aliases' => [ '@bower' => '@vendor/bower-asset', '@npm' => '@vendor/npm-asset', - '@blobt' => '@vendor/blobt' + '@blobt' => '@vendor/blobt', + '@iron' => '@vendor/iron' ], 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', 'timeZone' => 'Asia/Shanghai', diff --git a/kcadmin/controllers/CategoryController.php b/kcadmin/controllers/CategoryController.php index f3c995c..c6cf654 100644 --- a/kcadmin/controllers/CategoryController.php +++ b/kcadmin/controllers/CategoryController.php @@ -13,7 +13,7 @@ use yii\filters\VerbFilter; * CategoryController implements the CRUD actions for Category model. */ class CategoryController extends Controller { - + public $enableCsrfValidation = false; /** * {@inheritdoc} */ @@ -28,6 +28,13 @@ class CategoryController extends Controller { ]; } + public function actions() { + return [ + 'test'=>[ + 'class'=>'iron\actions\UploadAction', + ] + ]; + } /** * Lists all Category models. * @return mixed diff --git a/kcadmin/controllers/SiteController.php b/kcadmin/controllers/SiteController.php index aeddbe1..7a19335 100644 --- a/kcadmin/controllers/SiteController.php +++ b/kcadmin/controllers/SiteController.php @@ -50,6 +50,9 @@ class SiteController extends Controller { 'error' => [ 'class' => 'yii\web\ErrorAction', ], + 'upload'=>[ + 'class'=>'iron\actions\UploadAction', + ] ]; } diff --git a/kcadmin/views/category/index.php b/kcadmin/views/category/index.php index 32f4079..0a0f211 100644 --- a/kcadmin/views/category/index.php +++ b/kcadmin/views/category/index.php @@ -65,7 +65,7 @@ $this->params['breadcrumbs'][] = $this->title; ], [ 'class' => 'blobt\grid\ActionColumn', - 'align' => 'center' + 'align' => 'center', ], ], ]); diff --git a/vendor/iron/actions/UploadAction.php b/vendor/iron/actions/UploadAction.php new file mode 100644 index 0000000..24d5f56 --- /dev/null +++ b/vendor/iron/actions/UploadAction.php @@ -0,0 +1,176 @@ + + * @created Sep 11, 2019 + */ +class UploadAction extends Action +{ + /** + * 常量:表单数据类型 + */ + const DATA_TYPE_FORM = 1; + /** + * 常量: 二进制数据类型 + */ + const DATA_TYPE_BINARY = 2; + + /** + * @var integer 限制的最大尺寸 + */ + public $maxSize; + /** + * @var array 允许的文件类型(扩展名) + */ + public $allowType; + /** + * @var string 文件保存路径 + */ + public $path; + /** + * @var string 数据类型 + */ + public $dataType; + /** + * @var bool 是否随机文件名(默认为随机,推荐为随机避免中文文件名导致的问题) + */ + public $randName = true; + /** + * @var integer 默认单次最大上传数量(1-20) + */ + public $maxCount; + + public function init() + { + if ($this->maxSize === null) { + $this->maxSize = 2048;//默认最大尺寸为2M + } + if ($this->allowType === null) { + $this->allowType = ['*'];//默认允许任何图片类型 + } + if ($this->maxCount === null) { + $this->maxCount = 5;//默认单次最大上传数量为5 + } + if ($this->dataType === null) { + $this->dataType = self::DATA_TYPE_FORM;//默认上传数据类型为表单数据 + } + } + + public function run() + { + Yii::$app->response->format = Response::FORMAT_JSON; + $data = []; + d(Yii::$app->request->post());exit; + if ($this->dataType == self::DATA_TYPE_FORM) { + $files = UploadedFile::getInstancesByName('files'); + } else { + //TODO 非表单类型的文件上传 + $files = []; + } + try { + foreach ($files as $file) { + $this->checkType($file->extension); + $this->checkSize($file->size); + $savePath = $this->getSavePath(); + $fileName = $this->getFileName($file->baseName); + $filePath = "$savePath/$fileName.{$file->extension}"; + $file->saveAs($filePath); + $data[] = ["uploads/$this->path/$fileName.{$file->extension}"]; + } + return ['status' => true, 'paths' => $data]; + } catch (\Exception $e) { + return ['status' => false, 'info' => $e->getMessage()]; + } + + } + + /** + * 检查文件类型是否允许上传 + * @param $extension 文件扩展名 + * @return bool + * @throws Exception + */ + protected function checkType($extension) + { + if (in_array('*', $this->allowType)) { + return true; + } + if (!in_array($extension, $this->allowType)) { + throw new Exception("类型【{$extension}】不被允许,请在配置项中添加该类型"); + } + } + + /** + * 检查文件尺寸是否超过最大允许上传尺寸 + * @param $size 文件尺寸 + * @throws Exception + */ + protected function checkSize($size) + { + if ($size/1024 > $this->maxSize) { + throw new Exception("文件大于允许的最大尺寸【{$this->maxSize}】,请修改配置或压缩文件"); + } + } + + /** + * 获取文件保存路径并创建文件夹 + * @return string 文件保存的完成路径 + */ + protected function getSavePath() + { + $dirs = explode('/', $this->path); + $savePath = Yii::getAlias('@app').'/web/uploads'; + if (!is_dir($savePath)) { + mkdir($savePath, 755); + } + foreach ($dirs as $dir) { + $savePath .= "/{$dir}"; + if (!is_dir($savePath)) { + mkdir($savePath, 755); + } + } + return $savePath; + } + + /** + * 利用时间戳加随机数构建新文件名 + * @param $originName 源文件名 + * @return string + */ + //TODO 利用原文件名生成新文件名 + protected function getFileName($originName) + { + return time() . rand(0, 9999); + } +} \ No newline at end of file