正在显示
2 个修改的文件
包含
31 行增加
和
2 行删除
@@ -6,6 +6,8 @@ use app\common\exception\UploadException; | @@ -6,6 +6,8 @@ use app\common\exception\UploadException; | ||
6 | use app\common\model\Attachment; | 6 | use app\common\model\Attachment; |
7 | use fast\Random; | 7 | use fast\Random; |
8 | use FilesystemIterator; | 8 | use FilesystemIterator; |
9 | +use Qiniu\Auth; | ||
10 | +use Qiniu\Storage\UploadManager; | ||
9 | use think\Config; | 11 | use think\Config; |
10 | use think\File; | 12 | use think\File; |
11 | use think\Hook; | 13 | use think\Hook; |
@@ -358,7 +360,7 @@ class Upload | @@ -358,7 +360,7 @@ class Upload | ||
358 | $this->file = $file; | 360 | $this->file = $file; |
359 | $category = request()->post('category'); | 361 | $category = request()->post('category'); |
360 | $category = array_key_exists($category, config('site.attachmentcategory') ?? []) ? $category : ''; | 362 | $category = array_key_exists($category, config('site.attachmentcategory') ?? []) ? $category : ''; |
361 | - $auth = Auth::instance(); | 363 | + $auth = \app\common\library\Auth::instance(); |
362 | $params = array( | 364 | $params = array( |
363 | 'admin_id' => (int)session('admin.id'), | 365 | 'admin_id' => (int)session('admin.id'), |
364 | 'user_id' => (int)$auth->id, | 366 | 'user_id' => (int)$auth->id, |
@@ -376,6 +378,8 @@ class Upload | @@ -376,6 +378,8 @@ class Upload | ||
376 | 'sha1' => $sha1, | 378 | 'sha1' => $sha1, |
377 | 'extparam' => '', | 379 | 'extparam' => '', |
378 | ); | 380 | ); |
381 | + $token = $this->getQiniuToken(); | ||
382 | + $this->qiniu(PUBLIC_PATH.$params['url'],$params['url'],$token); | ||
379 | $attachment = new Attachment(); | 383 | $attachment = new Attachment(); |
380 | $attachment->data(array_filter($params)); | 384 | $attachment->data(array_filter($params)); |
381 | $attachment->save(); | 385 | $attachment->save(); |
@@ -384,6 +388,31 @@ class Upload | @@ -384,6 +388,31 @@ class Upload | ||
384 | return $attachment; | 388 | return $attachment; |
385 | } | 389 | } |
386 | 390 | ||
391 | + /** | ||
392 | + * @param $filename (文件路径) | ||
393 | + * @param $key (文件名) | ||
394 | + * @param $token (上传token) | ||
395 | + * @return (null|jsonstring) | ||
396 | + * @throws \Exception | ||
397 | + */ | ||
398 | + protected function qiniu($filename,$key,$token) | ||
399 | + { | ||
400 | + $upload = new UploadManager(); | ||
401 | + list($ret,$err) = $upload->putFile($token,$key,$filename); | ||
402 | + return $err; | ||
403 | + } | ||
404 | + | ||
405 | + /** | ||
406 | + * 获取七牛云上传token 一小时有效期 | ||
407 | + * @return string | ||
408 | + */ | ||
409 | + protected function getQiniuToken() | ||
410 | + { | ||
411 | + $config = get_addon_config('qiniu'); | ||
412 | + $auth = new Auth($config['accessKey'],$config['secretKey']); | ||
413 | + $token = $auth->uploadToken($config['bucket']); | ||
414 | + return $token; | ||
415 | + } | ||
387 | public function setError($msg) | 416 | public function setError($msg) |
388 | { | 417 | { |
389 | $this->error = $msg; | 418 | $this->error = $msg; |
@@ -29,7 +29,7 @@ defined('TEMP_PATH') or define('TEMP_PATH', RUNTIME_PATH . 'temp' . DS); | @@ -29,7 +29,7 @@ defined('TEMP_PATH') or define('TEMP_PATH', RUNTIME_PATH . 'temp' . DS); | ||
29 | defined('CONF_PATH') or define('CONF_PATH', APP_PATH); // 配置文件目录 | 29 | defined('CONF_PATH') or define('CONF_PATH', APP_PATH); // 配置文件目录 |
30 | defined('CONF_EXT') or define('CONF_EXT', EXT); // 配置文件后缀 | 30 | defined('CONF_EXT') or define('CONF_EXT', EXT); // 配置文件后缀 |
31 | defined('ENV_PREFIX') or define('ENV_PREFIX', 'PHP_'); // 环境变量的配置前缀 | 31 | defined('ENV_PREFIX') or define('ENV_PREFIX', 'PHP_'); // 环境变量的配置前缀 |
32 | - | 32 | +define('PUBLIC_PATH', ROOT_PATH . 'public'); |
33 | // 环境常量 | 33 | // 环境常量 |
34 | define('IS_CLI', PHP_SAPI == 'cli' ? true : false); | 34 | define('IS_CLI', PHP_SAPI == 'cli' ? true : false); |
35 | define('IS_WIN', strpos(PHP_OS, 'WIN') !== false); | 35 | define('IS_WIN', strpos(PHP_OS, 'WIN') !== false); |
-
请 注册 或 登录 后发表评论