[更新]修改文件存储配置

This commit is contained in:
Anyon 2020-12-10 16:36:05 +08:00
parent fee35392e9
commit cdc5baa3dd
10 changed files with 61 additions and 46 deletions

View File

@ -4,7 +4,7 @@
</label>
<div class="layui-input-block">
{if !sysconf('storage.link_type')}{php}sysconf('storage.link_type','none');{/php}{/if}
{foreach ['none'=>'简洁链接','full'=>'完整链接'] as $k=>$v}
{foreach ['none'=>'简洁链接','full'=>'完整链接','none+compress'=>'简洁并压缩图片','full+compress'=>'完整并压缩图片'] as $k=>$v}
<label class="think-radio notselect">
{if sysconf('storage.link_type') eq $k}
<input checked type="radio" name="storage.link_type" value="{$k}" lay-ignore> {$v}

View File

@ -3,7 +3,7 @@
<div class="color-text margin-left-40 margin-bottom-20 layui-code text-center layui-bg-gray" style="border-left-width:1px">
<p class="margin-bottom-5 font-w7">文件将存储在本地服务器,默认保存在 public/upload 目录,文件以 HASH 命名。</p>
<p>文件存储的目录需要有读写权限,有足够的存储空间</p>
<p>文件存储的目录需要有读写权限,有足够的存储空间<span class="color-red">特别注意,本地存储暂不支持图片压缩!</span></p>
</div>
{include file='config/storage-0'}

View File

@ -35,11 +35,10 @@
{foreach ['华东','华北','华南','北美'] as $area}
<label class="think-radio">
{if sysconf('storage.qiniu_region') eq $area}
<input checked type="radio" name="storage.qiniu_region" value="{$area}" lay-ignore>
<input checked type="radio" name="storage.qiniu_region" value="{$area}" lay-ignore> {$area}
{else}
<input type="radio" name="storage.qiniu_region" value="{$area}" lay-ignore>
<input type="radio" name="storage.qiniu_region" value="{$area}" lay-ignore> {$area}
{/if}
{$area}
</label>
{/foreach}
<p class="help-block">七牛云存储空间所在区域,需要严格对应储存所在区域才能上传文件</p>

View File

@ -937,12 +937,12 @@
"source": {
"type": "git",
"url": "https://github.com/zoujingli/ThinkLibrary.git",
"reference": "a8a7d6568b7ce7fe43c4ee7920a0b2d352e58b4c"
"reference": "6095c8e362118fd00d00f924a7ec342859e88104"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/a8a7d6568b7ce7fe43c4ee7920a0b2d352e58b4c",
"reference": "a8a7d6568b7ce7fe43c4ee7920a0b2d352e58b4c",
"url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/6095c8e362118fd00d00f924a7ec342859e88104",
"reference": "6095c8e362118fd00d00f924a7ec342859e88104",
"shasum": "",
"mirrors": [
{
@ -959,7 +959,7 @@
"ext-mbstring": "*",
"topthink/framework": "^6.0"
},
"time": "2020-12-10T03:45:59+00:00",
"time": "2020-12-10T08:26:28+00:00",
"type": "library",
"extra": {
"think": {

2
vendor/services.php vendored
View File

@ -1,5 +1,5 @@
<?php
// This file is automatically generated at:2020-12-10 11:50:32
// This file is automatically generated at:2020-12-10 16:31:45
declare (strict_types = 1);
return array (
0 => 'think\\admin\\Library',

View File

@ -43,11 +43,17 @@ abstract class Storage
protected $app;
/**
* 链接类型
* 存储类型
* @var string
*/
protected $type;
/**
* 链接类型
* @var string
*/
protected $link;
/**
* 链接前缀
* @var string
@ -64,7 +70,7 @@ abstract class Storage
public function __construct(App $app)
{
$this->app = $app;
$this->type = sysconf('storage.link_type');
$this->link = sysconf('storage.link_type');
$this->initialize();
}
@ -180,7 +186,7 @@ abstract class Storage
* @param string $url 资源地址
* @return string
*/
public static function curlGet(string $url)
public static function curlGet(string $url): string
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
@ -190,22 +196,33 @@ abstract class Storage
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
[$content] = [curl_exec($ch), curl_close($ch)];
return $content;
return $content ?: '';
}
/**
* 获取下载链接后缀
* @param null|string $attname 下载名称
* @param null|string $filename 文件名称
* @return string
*/
protected function getSuffix(?string $attname = null): string
protected function getSuffix(?string $attname = null, ?string $filename = null): string
{
if ($this->type === 'full') {
if (is_string($attname) && strlen($attname) > 0) {
return "?attname=" . urlencode($attname);
}
$suffix = '';
if (is_string($filename) && stripos($this->link, 'compress') !== false) {
$compress = [
'LocalStorage' => '',
'QiniuStorage' => '?imageslim',
'TxcosStorage' => '?imageMogr2/format/webp',
'AliossStorage' => '?x-oss-process=image/format,webp',
];
$class = basename(get_class($this));
$extens = strtolower(pathinfo($this->delSuffix($filename), PATHINFO_EXTENSION));
$suffix = in_array($extens, ['png', 'jpg', 'jpeg']) ? ($compress[$class] ?? '') : '';
}
return '';
if (is_string($attname) && strlen($attname) > 0 && stripos($this->link, 'full') !== false) {
$suffix .= ($suffix ? '&' : '?') . 'attname=' . urlencode($attname);
}
return $suffix;
}
/**

View File

@ -96,7 +96,7 @@ class AliossStorage extends Storage
* @param null|string $attname 下载名称
* @return array
*/
public function set(string $name, string $file, bool $safe = false, ?string $attname = null)
public function set(string $name, string $file, bool $safe = false, ?string $attname = null): array
{
$token = $this->buildUploadToken($name);
$data = ['key' => $name];
@ -132,7 +132,7 @@ class AliossStorage extends Storage
* @param boolean $safe 安全模式
* @return boolean
*/
public function del(string $name, bool $safe = false)
public function del(string $name, bool $safe = false): bool
{
[$file] = explode('?', $name);
$result = HttpExtend::request('DELETE', "http://{$this->bucket}.{$this->point}/{$file}", [
@ -147,7 +147,7 @@ class AliossStorage extends Storage
* @param boolean $safe 安全模式
* @return boolean
*/
public function has(string $name, bool $safe = false)
public function has(string $name, bool $safe = false): bool
{
$file = $this->delSuffix($name);
$result = HttpExtend::request('HEAD', "http://{$this->bucket}.{$this->point}/{$file}", [
@ -165,7 +165,7 @@ class AliossStorage extends Storage
*/
public function url(string $name, bool $safe = false, ?string $attname = null): string
{
return "{$this->prefix}/{$this->delSuffix($name)}{$this->getSuffix($attname)}";
return "{$this->prefix}/{$this->delSuffix($name)}{$this->getSuffix($attname,$name)}";
}
/**
@ -257,7 +257,7 @@ class AliossStorage extends Storage
* 阿里云OSS存储区域
* @return array
*/
public static function region()
public static function region(): array
{
return [
'oss-cn-hangzhou.aliyuncs.com' => '华东 1杭州',

View File

@ -55,7 +55,7 @@ class LocalStorage extends Storage
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public static function instance(?string $name = null): LocalStorage
public static function instance(?string $name = null)
{
return parent::instance('local');
}
@ -68,7 +68,7 @@ class LocalStorage extends Storage
* @param null|string $attname 下载名称
* @return array
*/
public function set(string $name, string $file, bool $safe = false, ?string $attname = null)
public function set(string $name, string $file, bool $safe = false, ?string $attname = null): array
{
try {
$path = $this->path($name, $safe);
@ -87,7 +87,7 @@ class LocalStorage extends Storage
* @param boolean $safe 安全模式
* @return string
*/
public function get(string $name, bool $safe = false)
public function get(string $name, bool $safe = false): string
{
if (!$this->has($name, $safe)) return '';
return static::curlGet($this->path($name, $safe));
@ -99,7 +99,7 @@ class LocalStorage extends Storage
* @param boolean $safe 安全模式
* @return boolean
*/
public function del(string $name, bool $safe = false)
public function del(string $name, bool $safe = false): bool
{
if ($this->has($name, $safe)) {
return @unlink($this->path($name, $safe));
@ -128,7 +128,7 @@ class LocalStorage extends Storage
*/
public function url(string $name, bool $safe = false, ?string $attname = null): string
{
return $safe ? $name : "{$this->prefix}/upload/{$this->delSuffix($name)}{$this->getSuffix($attname)}";
return $safe ? $name : "{$this->prefix}/upload/{$this->delSuffix($name)}{$this->getSuffix($attname,$name)}";
}
/**

View File

@ -27,6 +27,7 @@ use think\admin\Storage;
*/
class QiniuStorage extends Storage
{
private $bucket;
private $accessKey;
private $secretKey;
@ -79,7 +80,7 @@ class QiniuStorage extends Storage
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function set(string $name, string $file, bool $safe = false, ?string $attname = null)
public function set(string $name, string $file, bool $safe = false, ?string $attname = null): array
{
$token = $this->buildUploadToken($name, 3600, $attname);
$data = ['key' => $name, 'token' => $token, 'fileName' => $name];
@ -95,7 +96,7 @@ class QiniuStorage extends Storage
* @param boolean $safe 安全模式
* @return string
*/
public function get(string $name, bool $safe = false)
public function get(string $name, bool $safe = false): string
{
$url = $this->url($name, $safe) . "?e=" . time();
$token = "{$this->accessKey}:{$this->safeBase64(hash_hmac('sha1', $url, $this->secretKey, true))}";
@ -106,9 +107,9 @@ class QiniuStorage extends Storage
* 删除存储的文件
* @param string $name 文件名称
* @param boolean $safe 安全模式
* @return boolean|null
* @return boolean
*/
public function del(string $name, bool $safe = false)
public function del(string $name, bool $safe = false): bool
{
[$EncodedEntryURI, $AccessToken] = $this->getAccessToken($name, 'delete');
$data = json_decode(HttpExtend::post("http://rs.qiniu.com/delete/{$EncodedEntryURI}", [], [
@ -137,7 +138,7 @@ class QiniuStorage extends Storage
*/
public function url(string $name, bool $safe = false, ?string $attname = null): string
{
return "{$this->prefix}/{$this->delSuffix($name)}{$this->getSuffix($attname)}";
return "{$this->prefix}/{$this->delSuffix($name)}{$this->getSuffix($attname,$name)}";
}
/**
@ -203,9 +204,7 @@ class QiniuStorage extends Storage
{
$policy = $this->safeBase64(json_encode([
"deadline" => time() + $expires, "scope" => is_null($name) ? $this->bucket : "{$this->bucket}:{$name}",
'returnBody' => json_encode([
'uploaded' => true, 'filename' => '$(key)', 'url' => "{$this->prefix}/$(key){$this->getSuffix($attname)}", 'key' => $name, 'file' => $name,
], JSON_UNESCAPED_UNICODE),
'returnBody' => json_encode(['uploaded' => true, 'filename' => '$(key)', 'url' => "{$this->prefix}/$(key){$this->getSuffix($attname,$name)}", 'key' => $name, 'file' => $name], JSON_UNESCAPED_UNICODE),
]));
return "{$this->accessKey}:{$this->safeBase64(hash_hmac('sha1', $policy, $this->secretKey, true))}:{$policy}";
}
@ -237,7 +236,7 @@ class QiniuStorage extends Storage
* 七牛云对象存储区域
* @return array
*/
public static function region()
public static function region(): array
{
return [
'up.qiniup.com' => '华东',

View File

@ -64,7 +64,7 @@ class TxcosStorage extends Storage
/**
* 获取当前实例对象
* @param null|string $name
* @return static
* @return TxcosStorage
* @throws \think\admin\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
@ -83,11 +83,11 @@ class TxcosStorage extends Storage
* @param null|string $attname 下载名称
* @return array
*/
public function set(string $name, string $file, bool $safe = false, ?string $attname = null)
public function set(string $name, string $file, bool $safe = false, ?string $attname = null): array
{
$data = $this->buildUploadToken($name) + ['key' => $name];
if (is_string($attname) && strlen($attname) > 0) {
$data['Content-Disposition'] = '' . urlencode($attname);
$data['Content-Disposition'] = urlencode($attname);
}
$data['success_action_status'] = '200';
$file = ['field' => 'file', 'name' => $name, 'content' => $file];
@ -115,7 +115,7 @@ class TxcosStorage extends Storage
* @param boolean $safe 安全模式
* @return boolean
*/
public function del(string $name, bool $safe = false)
public function del(string $name, bool $safe = false): bool
{
[$file] = explode('?', $name);
$result = HttpExtend::request('DELETE', "http://{$this->bucket}.{$this->point}/{$file}", [
@ -130,7 +130,7 @@ class TxcosStorage extends Storage
* @param boolean $safe 安全模式
* @return boolean
*/
public function has(string $name, bool $safe = false)
public function has(string $name, bool $safe = false): bool
{
$file = $this->delSuffix($name);
$result = HttpExtend::request('HEAD', "http://{$this->bucket}.{$this->point}/{$file}", [
@ -148,7 +148,7 @@ class TxcosStorage extends Storage
*/
public function url(string $name, bool $safe = false, ?string $attname = null): string
{
return "{$this->prefix}/{$this->delSuffix($name)}{$this->getSuffix($attname)}";
return "{$this->prefix}/{$this->delSuffix($name)}{$this->getSuffix($attname,$name)}";
}
/**
@ -267,7 +267,7 @@ class TxcosStorage extends Storage
* 腾讯云COS存储区域
* @return array
*/
public static function region()
public static function region(): array
{
return [
'cos.ap-beijing-1.myqcloud.com' => '中国大陆 公有云地域 北京一区',