作者 李忠强

更新

... ... @@ -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;
... ...
... ... @@ -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);
... ...