ComposerUpdate & 配置阿里云OSS下载原文件名

This commit is contained in:
Anyon 2020-01-13 15:51:35 +08:00
parent 584eba4b62
commit 163890af2d
7 changed files with 85 additions and 46 deletions

8
composer.lock generated
View File

@ -909,12 +909,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/zoujingli/ThinkLibrary.git", "url": "https://github.com/zoujingli/ThinkLibrary.git",
"reference": "73ce73f61a90994e23b7616c73655440f50790a0" "reference": "65ff43be42fcf43679c8d05a416d8b25d70b7dbf"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/73ce73f61a90994e23b7616c73655440f50790a0", "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/65ff43be42fcf43679c8d05a416d8b25d70b7dbf",
"reference": "73ce73f61a90994e23b7616c73655440f50790a0", "reference": "65ff43be42fcf43679c8d05a416d8b25d70b7dbf",
"shasum": "", "shasum": "",
"mirrors": [ "mirrors": [
{ {
@ -958,7 +958,7 @@
], ],
"description": "ThinkPHP v6.0 Development Library", "description": "ThinkPHP v6.0 Development Library",
"homepage": "http://framework.thinkadmin.top", "homepage": "http://framework.thinkadmin.top",
"time": "2020-01-13T06:39:14+00:00" "time": "2020-01-13T07:49:16+00:00"
}, },
{ {
"name": "zoujingli/wechat-developer", "name": "zoujingli/wechat-developer",

View File

@ -935,12 +935,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/zoujingli/ThinkLibrary.git", "url": "https://github.com/zoujingli/ThinkLibrary.git",
"reference": "73ce73f61a90994e23b7616c73655440f50790a0" "reference": "65ff43be42fcf43679c8d05a416d8b25d70b7dbf"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/73ce73f61a90994e23b7616c73655440f50790a0", "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/65ff43be42fcf43679c8d05a416d8b25d70b7dbf",
"reference": "73ce73f61a90994e23b7616c73655440f50790a0", "reference": "65ff43be42fcf43679c8d05a416d8b25d70b7dbf",
"shasum": "", "shasum": "",
"mirrors": [ "mirrors": [
{ {
@ -956,7 +956,7 @@
"ext-json": "*", "ext-json": "*",
"topthink/framework": "^6.0" "topthink/framework": "^6.0"
}, },
"time": "2020-01-13T06:39:14+00:00", "time": "2020-01-13T07:49:16+00:00",
"type": "library", "type": "library",
"extra": { "extra": {
"think": { "think": {

2
vendor/services.php vendored
View File

@ -1,5 +1,5 @@
<?php <?php
// This file is automatically generated at:2020-01-13 14:48:33 // This file is automatically generated at:2020-01-13 15:51:21
declare (strict_types = 1); declare (strict_types = 1);
return array ( return array (
0 => 'think\\app\\Service', 0 => 'think\\app\\Service',

View File

@ -23,10 +23,10 @@ use think\Container;
* 文件存储引擎管理 * 文件存储引擎管理
* Class Storage * Class Storage
* @package think\admin * @package think\admin
* @method array info($name, $safe = false) static 文件存储信息 * @method array info($name, $safe = false, $attname = null) static 文件存储信息
* @method array set($name, $file, $safe = false) static 储存文件 * @method array set($name, $file, $safe = false, $attname = null) static 储存文件
* @method string url($name, $safe = false, $attname = null) static 获取文件链接
* @method string get($name, $safe = false) static 读取文件内容 * @method string get($name, $safe = false) static 读取文件内容
* @method string url($name, $safe = false) static 获取文件链接
* @method string path($name, $safe = false) static 文件存储路径 * @method string path($name, $safe = false) static 文件存储路径
* @method boolean del($name, $safe = false) static 删除存储文件 * @method boolean del($name, $safe = false) static 删除存储文件
* @method boolean has($name, $safe = false) static 检查是否存在 * @method boolean has($name, $safe = false) static 检查是否存在
@ -167,4 +167,32 @@ abstract class Storage
return $mimes = include __DIR__ . '/storage/bin/mimes.php'; return $mimes = include __DIR__ . '/storage/bin/mimes.php';
} }
/**
* 获取文件基础名称
* @param string $name 文件名称
* @return string
*/
protected function delSuffix($name)
{
if (strpos($name, '?') !== false) {
list($name) = explode('?', $name);
}
return $name;
}
/**
* 获取下载链接后缀
* @param string $attname 下载名称
* @return string
*/
protected function getSuffix($attname = null)
{
if (is_string($attname) && strlen($attname) > 0) {
$attname = "?attname=" . urlencode($attname);
} else {
$attname = '';
}
return $attname;
}
} }

View File

@ -99,10 +99,10 @@ class AliossStorage extends Storage
* @param string $name 文件名称 * @param string $name 文件名称
* @param string $file 文件内容 * @param string $file 文件内容
* @param boolean $safe 安全模式 * @param boolean $safe 安全模式
* @param string $attachment 下载名称 * @param string $attname 下载名称
* @return array * @return array
*/ */
public function set($name, $file, $safe = false, $attachment = null) public function set($name, $file, $safe = false, $attname = null)
{ {
$token = $this->buildUploadToken($name); $token = $this->buildUploadToken($name);
$data = ['key' => $name]; $data = ['key' => $name];
@ -110,12 +110,13 @@ class AliossStorage extends Storage
$data['Signature'] = $token['signature']; $data['Signature'] = $token['signature'];
$data['OSSAccessKeyId'] = $this->accessKey; $data['OSSAccessKeyId'] = $this->accessKey;
$data['success_action_status'] = '200'; $data['success_action_status'] = '200';
if (is_string($attachment) && strlen($attachment) > 0) { if (is_string($attname) && strlen($attname) > 0) {
$data['Content-Disposition'] = "attachment;filename=" . urlencode($attachment); $filename = urlencode($attname);
$data['Content-Disposition'] = "attachment;filename={$filename}";
} }
$file = ['field' => 'file', 'name' => $name, 'content' => $file]; $file = ['field' => 'file', 'name' => $name, 'content' => $file];
if (is_numeric(stripos(HttpExtend::submit($this->upload(), $data, $file), '200 OK'))) { if (is_numeric(stripos(HttpExtend::submit($this->upload(), $data, $file), '200 OK'))) {
return ['file' => $this->path($name, $safe), 'url' => $this->url($name, $safe), 'key' => $name]; return ['file' => $this->path($name, $safe), 'url' => $this->url($name, $safe, $attname), 'key' => $name];
} else { } else {
return []; return [];
} }
@ -129,7 +130,7 @@ class AliossStorage extends Storage
*/ */
public function get($name, $safe = false) public function get($name, $safe = false)
{ {
return file_get_contents($this->url($name, $safe) . "?e=" . time()); return file_get_contents($this->url($name, $safe));
} }
/** /**
@ -140,8 +141,9 @@ class AliossStorage extends Storage
*/ */
public function del($name, $safe = false) public function del($name, $safe = false)
{ {
$result = HttpExtend::request('DELETE', "http://{$this->bucket}.{$this->point}/{$name}", [ list($file) = explode('?', $name);
'returnHeader' => true, 'headers' => $this->headerSign('DELETE', $name), $result = HttpExtend::request('DELETE', "http://{$this->bucket}.{$this->point}/{$file}", [
'returnHeader' => true, 'headers' => $this->headerSign('DELETE', $file),
]); ]);
return is_numeric(stripos($result, '204 No Content')); return is_numeric(stripos($result, '204 No Content'));
} }
@ -154,8 +156,9 @@ class AliossStorage extends Storage
*/ */
public function has($name, $safe = false) public function has($name, $safe = false)
{ {
$result = HttpExtend::request('HEAD', "http://{$this->bucket}.{$this->point}/{$name}", [ $file = $this->delSuffix($name);
'returnHeader' => true, 'headers' => $this->headerSign('HEAD', $name), $result = HttpExtend::request('HEAD', "http://{$this->bucket}.{$this->point}/{$file}", [
'returnHeader' => true, 'headers' => $this->headerSign('HEAD', $file),
]); ]);
return is_numeric(stripos($result, 'HTTP/1.1 200 OK')); return is_numeric(stripos($result, 'HTTP/1.1 200 OK'));
} }
@ -164,11 +167,12 @@ class AliossStorage extends Storage
* 获取文件当前URL地址 * 获取文件当前URL地址
* @param string $name 文件名称 * @param string $name 文件名称
* @param boolean $safe 安全模式 * @param boolean $safe 安全模式
* @param string $attname 下载名称
* @return string * @return string
*/ */
public function url($name, $safe = false) public function url($name, $safe = false, $attname = null)
{ {
return "{$this->prefix}/{$name}"; return "{$this->prefix}/{$this->delSuffix($name)}{$this->getSuffix($attname)}";
} }
/** /**
@ -186,15 +190,15 @@ class AliossStorage extends Storage
* 获取文件存储信息 * 获取文件存储信息
* @param string $name 文件名称 * @param string $name 文件名称
* @param boolean $safe 安全模式 * @param boolean $safe 安全模式
* @param string $attname 下载名称
* @return array * @return array
*/ */
public function info($name, $safe = false) public function info($name, $safe = false, $attname = null)
{ {
if ($this->has($name, $safe)) { return $this->has($name, $safe) ? [
return ['file' => $this->path($name, $safe), 'url' => $this->url($name, $safe), 'key' => $name]; 'url' => $this->url($name, $safe, $attname),
} else { 'key' => $name, 'file' => $this->path($name, $safe),
return []; ] : [];
}
} }
/** /**

View File

@ -68,15 +68,16 @@ class LocalStorage extends Storage
* @param string $name 文件名称 * @param string $name 文件名称
* @param string $file 文件内容 * @param string $file 文件内容
* @param boolean $safe 安全模式 * @param boolean $safe 安全模式
* @param string $attname 下载名称
* @return array * @return array
*/ */
public function set($name, $file, $safe = false) public function set($name, $file, $safe = false, $attname = null)
{ {
try { try {
$path = $this->path($name, $safe); $path = $this->path($name, $safe);
file_exists(dirname($path)) || mkdir(dirname($path), 0755, true); file_exists(dirname($path)) || mkdir(dirname($path), 0755, true);
if (file_put_contents($path, $file)) { if (file_put_contents($path, $file)) {
return $this->info($name, $safe); return $this->info($name, $safe, $attname);
} }
} catch (\Exception $e) { } catch (\Exception $e) {
return []; return [];
@ -125,12 +126,12 @@ class LocalStorage extends Storage
* 获取文件当前URL地址 * 获取文件当前URL地址
* @param string $name 文件名称 * @param string $name 文件名称
* @param boolean $safe 安全模式 * @param boolean $safe 安全模式
* @param string $attname 下载名称
* @return string|null * @return string|null
*/ */
public function url($name, $safe = false) public function url($name, $safe = false, $attname = null)
{ {
if ($safe) return $name; return $safe ? $name : "{$this->prefix}/upload/{$this->delSuffix($name)}{$this->getSuffix($attname)}";
return "{$this->prefix}/upload/{$name}";
} }
/** /**
@ -143,19 +144,21 @@ class LocalStorage extends Storage
{ {
$root = $this->app->getRootPath(); $root = $this->app->getRootPath();
$path = $safe ? 'safefile' : 'public/upload'; $path = $safe ? 'safefile' : 'public/upload';
return strtr("{$root}{$path}/{$name}", '\\', '/'); return strtr("{$root}{$path}/{$this->delSuffix($name)}", '\\', '/');
} }
/** /**
* 获取文件存储信息 * 获取文件存储信息
* @param string $name 文件名称 * @param string $name 文件名称
* @param boolean $safe 安全模式 * @param boolean $safe 安全模式
* @param string $attname 下载名称
* @return array * @return array
*/ */
public function info($name, $safe = false) public function info($name, $safe = false, $attname = null)
{ {
return $this->has($name, $safe) ? [ return $this->has($name, $safe) ? [
'file' => $this->path($name, $safe), 'url' => $this->url($name, $safe), 'key' => "upload/{$name}", 'url' => $this->url($name, $safe, $attname),
'key' => "upload/{$name}", 'file' => $this->path($name, $safe),
] : []; ] : [];
} }

View File

@ -73,15 +73,16 @@ class QiniuStorage extends Storage
* @param string $name 文件名称 * @param string $name 文件名称
* @param string $file 文件内容 * @param string $file 文件内容
* @param boolean $safe 安全模式 * @param boolean $safe 安全模式
* @param string $attname 下载名称
* @return array * @return array
* @throws \think\Exception * @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function set($name, $file, $safe = false) public function set($name, $file, $safe = false, $attname = null)
{ {
$token = $this->buildUploadToken($name); $token = $this->buildUploadToken($name, 3600, $attname);
$data = ['key' => $name, 'token' => $token, 'fileName' => $name]; $data = ['key' => $name, 'token' => $token, 'fileName' => $name];
$file = ['field' => "file", 'name' => $name, 'content' => $file]; $file = ['field' => "file", 'name' => $name, 'content' => $file];
$result = HttpExtend::submit($this->upload(), $data, $file, [], 'POST', false); $result = HttpExtend::submit($this->upload(), $data, $file, [], 'POST', false);
@ -132,11 +133,12 @@ class QiniuStorage extends Storage
* 获取文件当前URL地址 * 获取文件当前URL地址
* @param string $name 文件名称 * @param string $name 文件名称
* @param boolean $safe 安全模式 * @param boolean $safe 安全模式
* @param string $attname 下载名称
* @return string * @return string
*/ */
public function url($name, $safe = false) public function url($name, $safe = false, $attname = null)
{ {
return "{$this->prefix}/{$name}"; return "{$this->prefix}/{$this->delSuffix($name)}{$this->getSuffix($attname)}";
} }
/** /**
@ -154,13 +156,14 @@ class QiniuStorage extends Storage
* 获取文件存储信息 * 获取文件存储信息
* @param string $name 文件名称 * @param string $name 文件名称
* @param boolean $safe 安全模式 * @param boolean $safe 安全模式
* @param string $attname 下载名称
* @return array * @return array
*/ */
public function info($name, $safe = false) public function info($name, $safe = false, $attname = null)
{ {
list($entry, $token) = $this->getAccessToken($name); list($entry, $token) = $this->getAccessToken($name);
$data = json_decode(HttpExtend::get("http://rs.qiniu.com/stat/{$entry}", [], ['headers' => ["Authorization: QBox {$token}"]]), true); $data = json_decode(HttpExtend::get("http://rs.qiniu.com/stat/{$entry}", [], ['headers' => ["Authorization: QBox {$token}"]]), true);
return isset($data['md5']) ? ['file' => $name, 'url' => $this->url($name, $safe), 'key' => $name] : []; return isset($data['md5']) ? ['file' => $name, 'url' => $this->url($name, $safe, $attname), 'key' => $name] : [];
} }
/** /**
@ -194,14 +197,15 @@ class QiniuStorage extends Storage
* 获取文件上传令牌 * 获取文件上传令牌
* @param string $name 文件名称 * @param string $name 文件名称
* @param integer $expires 有效时间 * @param integer $expires 有效时间
* @param string $attname 下载名称
* @return string * @return string
*/ */
public function buildUploadToken($name = null, $expires = 3600) public function buildUploadToken($name = null, $expires = 3600, $attname = null)
{ {
$policy = $this->safeBase64(json_encode([ $policy = $this->safeBase64(json_encode([
"deadline" => time() + $expires, "scope" => is_null($name) ? $this->bucket : "{$this->bucket}:{$name}", "deadline" => time() + $expires, "scope" => is_null($name) ? $this->bucket : "{$this->bucket}:{$name}",
'returnBody' => json_encode([ 'returnBody' => json_encode([
'uploaded' => true, 'filename' => '$(key)', 'file' => $name, 'url' => "{$this->prefix}/$(key)", 'key' => $name, 'uploaded' => true, 'filename' => '$(key)', 'url' => "{$this->prefix}/$(key){$this->getSuffix($attname)}", 'key' => $name, 'file' => $name,
], JSON_UNESCAPED_UNICODE), ], JSON_UNESCAPED_UNICODE),
])); ]));
return "{$this->accessKey}:{$this->safeBase64(hash_hmac('sha1', $policy, $this->secretKey, true))}:{$policy}"; return "{$this->accessKey}:{$this->safeBase64(hash_hmac('sha1', $policy, $this->secretKey, true))}:{$policy}";