From 81ec32a31d2642749b64409245d6892463355687 Mon Sep 17 00:00:00 2001 From: Anyon Date: Thu, 11 May 2017 11:52:03 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E5=AE=8C=E5=96=84AliOSS?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/controller/Plugs.php | 12 +++++++- application/admin/view/plugs.upfile.html | 38 +++++++++++++++--------- extend/service/FileService.php | 16 ++++++++++ 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/application/admin/controller/Plugs.php b/application/admin/controller/Plugs.php index 38e089dac..1ba404abb 100644 --- a/application/admin/controller/Plugs.php +++ b/application/admin/controller/Plugs.php @@ -15,7 +15,9 @@ namespace app\admin\controller; use controller\BasicAdmin; +use OSS\OssClient; use service\FileService; +use think\View; /** * 插件助手控制器 @@ -40,7 +42,7 @@ class Plugs extends BasicAdmin { /** * 文件上传 - * @return \think\response\View + * @return View */ public function upfile() { $types = $this->request->get('type', 'jpg,png'); @@ -94,6 +96,14 @@ class Plugs extends BasicAdmin { case 'local': $config['server'] = FileService::getUploadLocalUrl(); break; + case 'oss': + $time = time() + 3600; + $policyText = ['expiration' => date('Y-m-d', $time) . 'T' . date('H:i:s') . '.000Z', 'conditions' => [['content-length-range', 0, 1048576000]]]; + $config['policy'] = base64_encode(json_encode($policyText)); + $config['server'] = FileService::getUploadOssUrl(); + $config['site_url'] = FileService::getBaseUriOss() . $filename; + $config['signature'] = base64_encode(hash_hmac('sha1', $config['policy'], sysconf('storage_oss_secret'), true)); + $config['OSSAccessKeyId'] = sysconf('storage_oss_keyid'); } $this->result($config, 'NOT_FOUND'); } diff --git a/application/admin/view/plugs.upfile.html b/application/admin/view/plugs.upfile.html index 5c50e3f94..7141419da 100644 --- a/application/admin/view/plugs.upfile.html +++ b/application/admin/view/plugs.upfile.html @@ -196,8 +196,13 @@ console.log('文件秒传成功 --> ' + file.name); } else { file.md5 = md5; - file.token = ret.data.token; - file.key = ret.data.file_url; + file.token = ret.data.token || ''; + file.key = ret.data.file_url || ''; + file.site_url = ret.data.site_url || ''; + me.options.formData.OSSAccessKeyId = ret.data.OSSAccessKeyId || ''; // OSS + me.options.formData.signature = ret.data.signature; // OSS + me.options.formData.policy = ret.data.policy; // OSS + me.options.formData.success_action_status = '200'; // OSS me.options.server = ret.data.server; } deferred.resolve(); @@ -240,16 +245,31 @@ fileSingleSizeLimit: 200 * 1024 * 1024 // 200 M }); + /* 上传开始前的处理 */ + uploader.on('uploadBeforeSend', function (file, data, header) { + header['X_Requested_With'] = 'XMLHttpRequest'; + data['allowed_types'] = this.options.accept[0].extensions.split(',').join('|'); + data['md5'] = file.file.md5; + data['key'] = file.file.key; + data['token'] = file.file.token; + }); + /** * 处理上传后的结果 * @param {type} file * @returns {undefined} */ uploader.on('uploadAccept', function (fieldata, ret) { + // Qiniu or Local 上传 if (ret.code === 'SUCCESS') { uploaded.call(uploader, ret.data, fieldata.file); return true; } + // AliOSS 上传 + if (fieldata.file.site_url) { + uploaded.call(uploader, {'site_url': fieldata.file.site_url}, fieldata.file); + return true; + } return false; }); @@ -266,14 +286,7 @@ return !denied; }); - /* 上传开始前的处理 */ - uploader.on('uploadBeforeSend', function (file, data, header) { - header['X_Requested_With'] = 'XMLHttpRequest'; - data['allowed_types'] = this.options.accept[0].extensions.split(',').join('|'); - data['md5'] = file.file.md5; - data['key'] = file.file.key; - data['token'] = file.file.token; - }); + // 添加“添加文件”的按钮, uploader.addButton({ @@ -484,7 +497,6 @@ if (stats.successNum) { completed.call(this); } else { - // 没有成功的文件,重设 state = 'done'; location.reload(); } @@ -515,9 +527,7 @@ uploader.onfieldequeued = function (file) { fileCount--; fileSize -= file.size; - if (!fileCount) { - setState('pedding'); - } + !fileCount && setState('pedding'); removeFile(file); updateTotalProgress(); }; diff --git a/extend/service/FileService.php b/extend/service/FileService.php index bb5d1a428..df11ad9b4 100644 --- a/extend/service/FileService.php +++ b/extend/service/FileService.php @@ -64,6 +64,8 @@ class FileService { return self::getBaseUriLocal() . $filename; case 'qiniu': return self::getBaseUriQiniu() . $filename; + case 'oss': + return self::getBaseUriOss() . $filename; } return false; } @@ -109,6 +111,14 @@ class FileService { } } + /** + * 获取AliOSS上传地址 + * @return type + */ + public static function getUploadOssUrl() { + return (request()->isSsl() ? 'https' : 'http') . '://' . sysconf('storage_oss_domain'); + } + /** * 获取服务器URL前缀 * @return string @@ -154,6 +164,9 @@ class FileService { $bucketMgr = new BucketManager($auth); list($ret, $err) = $bucketMgr->stat(sysconf('storage_qiniu_bucket'), $filename); return $err === null; + case 'oss': + $ossClient = new OssClient(sysconf('storage_oss_keyid'), sysconf('storage_oss_secret'), self::getBaseUriOss(), true); + return $ossClient->doesObjectExist(sysconf('storage_oss_bucket'), $filename); } return false; } @@ -174,6 +187,9 @@ class FileService { case 'qiniu': $auth = new Auth(sysconf('storage_qiniu_access_key'), sysconf('storage_qiniu_secret_key')); return file_get_contents($auth->privateDownloadUrl(self::getBaseUriQiniu() . $filename)); + case 'oss': + $ossClient = new OssClient(sysconf('storage_oss_keyid'), sysconf('storage_oss_secret'), self::getBaseUriOss(), true); + return $ossClient->getObject(sysconf('storage_oss_bucket'), $filename); } Log::error("通过{$storage}读取文件{$filename}的不存在!"); return null;