From a6f9b3ae30de0145a6bfbeefd81e089fbd82409e Mon Sep 17 00:00:00 2001 From: 李忠强 <1354905998@qq.com> Date: Wed, 19 Jan 2022 10:18:36 +0800 Subject: [PATCH] 更新 --- application/common/library/Upload.php | 31 ++++++++++++++++++++++++++++++- thinkphp/base.php | 2 +- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/application/common/library/Upload.php b/application/common/library/Upload.php index 0a31f69..e868e1e 100644 --- a/application/common/library/Upload.php +++ b/application/common/library/Upload.php @@ -6,6 +6,8 @@ use app\common\exception\UploadException; use app\common\model\Attachment; use fast\Random; use FilesystemIterator; +use Qiniu\Auth; +use Qiniu\Storage\UploadManager; use think\Config; use think\File; use think\Hook; @@ -358,7 +360,7 @@ class Upload $this->file = $file; $category = request()->post('category'); $category = array_key_exists($category, config('site.attachmentcategory') ?? []) ? $category : ''; - $auth = Auth::instance(); + $auth = \app\common\library\Auth::instance(); $params = array( 'admin_id' => (int)session('admin.id'), 'user_id' => (int)$auth->id, @@ -376,6 +378,8 @@ class Upload 'sha1' => $sha1, 'extparam' => '', ); + $token = $this->getQiniuToken(); + $this->qiniu(PUBLIC_PATH.$params['url'],$params['url'],$token); $attachment = new Attachment(); $attachment->data(array_filter($params)); $attachment->save(); @@ -384,6 +388,31 @@ class Upload return $attachment; } + /** + * @param $filename (文件路径) + * @param $key (文件名) + * @param $token (上传token) + * @return (null|jsonstring) + * @throws \Exception + */ + protected function qiniu($filename,$key,$token) + { + $upload = new UploadManager(); + list($ret,$err) = $upload->putFile($token,$key,$filename); + return $err; + } + + /** + * 获取七牛云上传token 一小时有效期 + * @return string + */ + protected function getQiniuToken() + { + $config = get_addon_config('qiniu'); + $auth = new Auth($config['accessKey'],$config['secretKey']); + $token = $auth->uploadToken($config['bucket']); + return $token; + } public function setError($msg) { $this->error = $msg; diff --git a/thinkphp/base.php b/thinkphp/base.php index 92c4fa5..4d18d8f 100644 --- a/thinkphp/base.php +++ b/thinkphp/base.php @@ -29,7 +29,7 @@ defined('TEMP_PATH') or define('TEMP_PATH', RUNTIME_PATH . 'temp' . DS); defined('CONF_PATH') or define('CONF_PATH', APP_PATH); // 配置文件目录 defined('CONF_EXT') or define('CONF_EXT', EXT); // 配置文件后缀 defined('ENV_PREFIX') or define('ENV_PREFIX', 'PHP_'); // 环境变量的配置前缀 - +define('PUBLIC_PATH', ROOT_PATH . 'public'); // 环境常量 define('IS_CLI', PHP_SAPI == 'cli' ? true : false); define('IS_WIN', strpos(PHP_OS, 'WIN') !== false); -- libgit2 0.24.0