From 37bbd9986160bd0bd9759c531315b84b338768ef Mon Sep 17 00:00:00 2001 From: augushong Date: Tue, 19 Sep 2023 15:21:40 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E5=AE=9E=E7=8E=B0filesystem?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=8C=E5=8D=87=E7=BA=A7=E5=88=B0=E6=9C=80?= =?UTF-8?q?=E6=96=B0=E7=89=88=E6=9C=AC=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/Ajax.php | 1 + app/admin/view/system/config/upload.html | 6 +- app/middleware.php | 4 +- app/service.php | 4 +- composer.json | 8 +- composer.lock | 311 ++++++++++++++--------- extend/think/Filesystem.php | 89 +++++++ extend/think/facade/Filesystem.php | 33 +++ extend/think/filesystem/Driver.php | 130 ++++++++++ extend/think/filesystem/driver/Local.php | 98 +++++++ extend/think/filesystem/driver/Txcos.php | 5 +- 11 files changed, 562 insertions(+), 127 deletions(-) create mode 100644 extend/think/Filesystem.php create mode 100644 extend/think/facade/Filesystem.php create mode 100644 extend/think/filesystem/Driver.php create mode 100644 extend/think/filesystem/driver/Local.php diff --git a/app/admin/controller/Ajax.php b/app/admin/controller/Ajax.php index 9fc58e9..5645bca 100644 --- a/app/admin/controller/Ajax.php +++ b/app/admin/controller/Ajax.php @@ -69,6 +69,7 @@ class Ajax extends AdminController $result = $upload_service->save($data['file']); } catch (\Exception $e) { + throw $e; $this->error($e->getMessage()); } diff --git a/app/admin/view/system/config/upload.html b/app/admin/view/system/config/upload.html index 7f23569..0f6b1de 100644 --- a/app/admin/view/system/config/upload.html +++ b/app/admin/view/system/config/upload.html @@ -51,7 +51,7 @@
- 例子:https://oss-cn-shenzhen.aliyuncs.com + endpoint,例子:oss-cn-shenzhen.aliyuncs.com
@@ -67,7 +67,7 @@
- 例子:ulthon-admin.oss-cn-shenzhen.aliyuncs.com + 例子:http://ulthon-admin.oss-cn-shenzhen.aliyuncs.com
@@ -106,7 +106,7 @@
- 例子:ulthon-admin-1251997243 + 例子:ulthon-admin ,注意不需要拼接appid等数字
diff --git a/app/middleware.php b/app/middleware.php index 4ffeefa..8b12ac4 100644 --- a/app/middleware.php +++ b/app/middleware.php @@ -15,4 +15,6 @@ $middleware_common = include_once __DIR__ . '/common/app/middleware.php'; $middleware = array_merge($middleware_default, $middleware_common); -return ksort($middleware); +ksort($middleware); + +return $middleware; diff --git a/app/service.php b/app/service.php index ef0bcfd..7e066b8 100644 --- a/app/service.php +++ b/app/service.php @@ -11,4 +11,6 @@ $service_common = include_once __DIR__ . '/common/app/service.php'; $service = array_merge($service_default, $service_common); -return ksort($service); +ksort($service); + +return $service; diff --git a/composer.json b/composer.json index be76aa8..fbf35b0 100644 --- a/composer.json +++ b/composer.json @@ -24,14 +24,14 @@ "topthink/think-multi-app": "^1.0", "topthink/think-view": "^2.0", "topthink/think-captcha": "^3.0", - "topthink/think-filesystem": "^2.0", "topthink/think-migration": "^3.0", "guzzlehttp/guzzle": "^7.4", "phpoffice/phpspreadsheet": "^1.22", "doctrine/annotations": "^1.13", - "overtrue/flysystem-qiniu": "^2.0", - "overtrue/flysystem-cos": "^4.0", - "iidestiny/flysystem-oss": "^3.0" + "league/flysystem": "^3.0", + "overtrue/flysystem-qiniu": "^3.0", + "overtrue/flysystem-cos": "^5.0", + "iidestiny/flysystem-oss": "^4.0" }, "require-dev": { "symfony/var-dumper": "^4.2" diff --git a/composer.lock b/composer.lock index c06ff71..a237863 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ca3fcd696a06edb2905bbd88a2d905d0", + "content-hash": "07cbcad2506a74dbd4130f9597859332", "packages": [ { "name": "aliyuncs/oss-sdk-php", @@ -640,16 +640,16 @@ }, { "name": "iidestiny/flysystem-oss", - "version": "3.1", + "version": "4.3", "source": { "type": "git", "url": "https://github.com/iiDestiny/flysystem-oss.git", - "reference": "9fff308b9bcb47f7d5021c0741a8fbcf463cef9e" + "reference": "15447c3367cb8a36a64bc2881c13ee2b6ae3fa26" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/iiDestiny/flysystem-oss/zipball/9fff308b9bcb47f7d5021c0741a8fbcf463cef9e", - "reference": "9fff308b9bcb47f7d5021c0741a8fbcf463cef9e", + "url": "https://api.github.com/repos/iiDestiny/flysystem-oss/zipball/15447c3367cb8a36a64bc2881c13ee2b6ae3fa26", + "reference": "15447c3367cb8a36a64bc2881c13ee2b6ae3fa26", "shasum": "" }, "require": { @@ -657,12 +657,12 @@ "ext-curl": "*", "ext-json": "*", "ext-openssl": "*", - "league/flysystem": "^2.0", - "php": ">=7.2" + "league/flysystem": "^3.0", + "php": "^8.0.2" }, "require-dev": { - "mockery/mockery": "^1.2", - "phpunit/phpunit": "^6.5", + "mockery/mockery": "^1.5", + "phpunit/phpunit": "^9.5", "symfony/var-dumper": "^3.4" }, "type": "library", @@ -690,45 +690,53 @@ ], "support": { "issues": "https://github.com/iiDestiny/flysystem-oss/issues", - "source": "https://github.com/iiDestiny/flysystem-oss/tree/3.1" + "source": "https://github.com/iiDestiny/flysystem-oss/tree/4.3" }, - "time": "2022-04-19T05:39:46+00:00" + "time": "2023-09-09T11:59:47+00:00" }, { "name": "league/flysystem", - "version": "2.5.0", + "version": "3.16.0", "source": { "type": "git", "url": "https://github.com/thephpleague/flysystem.git", - "reference": "8aaffb653c5777781b0f7f69a5d937baf7ab6cdb" + "reference": "4fdf372ca6b63c6e281b1c01a624349ccb757729" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/8aaffb653c5777781b0f7f69a5d937baf7ab6cdb", - "reference": "8aaffb653c5777781b0f7f69a5d937baf7ab6cdb", + "url": "https://api.github.com/repos/thephpleague/flysystem/zipball/4fdf372ca6b63c6e281b1c01a624349ccb757729", + "reference": "4fdf372ca6b63c6e281b1c01a624349ccb757729", "shasum": "" }, "require": { - "ext-json": "*", + "league/flysystem-local": "^3.0.0", "league/mime-type-detection": "^1.0.0", - "php": "^7.2 || ^8.0" + "php": "^8.0.2" }, "conflict": { - "guzzlehttp/ringphp": "<1.1.1" + "async-aws/core": "<1.19.0", + "async-aws/s3": "<1.14.0", + "aws/aws-sdk-php": "3.209.31 || 3.210.0", + "guzzlehttp/guzzle": "<7.0", + "guzzlehttp/ringphp": "<1.1.1", + "phpseclib/phpseclib": "3.0.15", + "symfony/http-client": "<5.2" }, "require-dev": { "async-aws/s3": "^1.5", - "async-aws/simple-s3": "^1.0", - "aws/aws-sdk-php": "^3.132.4", + "async-aws/simple-s3": "^1.1", + "aws/aws-sdk-php": "^3.220.0", "composer/semver": "^3.0", "ext-fileinfo": "*", "ext-ftp": "*", - "friendsofphp/php-cs-fixer": "^3.2", + "ext-zip": "*", + "friendsofphp/php-cs-fixer": "^3.5", "google/cloud-storage": "^1.23", - "phpseclib/phpseclib": "^2.0", + "microsoft/azure-storage-blob": "^1.1", + "phpseclib/phpseclib": "^3.0.14", "phpstan/phpstan": "^0.12.26", - "phpunit/phpunit": "^8.5 || ^9.4", - "sabre/dav": "^4.1" + "phpunit/phpunit": "^9.5.11|^10.0", + "sabre/dav": "^4.3.1" }, "type": "library", "autoload": { @@ -762,7 +770,7 @@ ], "support": { "issues": "https://github.com/thephpleague/flysystem/issues", - "source": "https://github.com/thephpleague/flysystem/tree/2.5.0" + "source": "https://github.com/thephpleague/flysystem/tree/3.16.0" }, "funding": [ { @@ -772,13 +780,69 @@ { "url": "https://github.com/frankdejonge", "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/league/flysystem", - "type": "tidelift" } ], - "time": "2022-09-17T21:02:32+00:00" + "time": "2023-09-07T19:22:17+00:00" + }, + { + "name": "league/flysystem-local", + "version": "3.16.0", + "source": { + "type": "git", + "url": "https://github.com/thephpleague/flysystem-local.git", + "reference": "ec7383f25642e6fd4bb0c9554fc2311245391781" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thephpleague/flysystem-local/zipball/ec7383f25642e6fd4bb0c9554fc2311245391781", + "reference": "ec7383f25642e6fd4bb0c9554fc2311245391781", + "shasum": "" + }, + "require": { + "ext-fileinfo": "*", + "league/flysystem": "^3.0.0", + "league/mime-type-detection": "^1.0.0", + "php": "^8.0.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "League\\Flysystem\\Local\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Frank de Jonge", + "email": "info@frankdejonge.nl" + } + ], + "description": "Local filesystem adapter for Flysystem.", + "keywords": [ + "Flysystem", + "file", + "files", + "filesystem", + "local" + ], + "support": { + "issues": "https://github.com/thephpleague/flysystem-local/issues", + "source": "https://github.com/thephpleague/flysystem-local/tree/3.16.0" + }, + "funding": [ + { + "url": "https://ecologi.com/frankdejonge", + "type": "custom" + }, + { + "url": "https://github.com/frankdejonge", + "type": "github" + } + ], + "time": "2023-08-30T10:23:59+00:00" }, { "name": "league/mime-type-detection", @@ -1086,35 +1150,36 @@ }, { "name": "overtrue/flysystem-cos", - "version": "4.0.1", + "version": "5.1.4", "source": { "type": "git", "url": "https://github.com/overtrue/flysystem-cos.git", - "reference": "d86e1d5f3f7a881c672a698fbdfa08a767b3ea38" + "reference": "bbcfe9195e96a9d5262c79fdb1e57820d45dd664" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/overtrue/flysystem-cos/zipball/d86e1d5f3f7a881c672a698fbdfa08a767b3ea38", - "reference": "d86e1d5f3f7a881c672a698fbdfa08a767b3ea38", + "url": "https://api.github.com/repos/overtrue/flysystem-cos/zipball/bbcfe9195e96a9d5262c79fdb1e57820d45dd664", + "reference": "bbcfe9195e96a9d5262c79fdb1e57820d45dd664", "shasum": "" }, "require": { - "league/flysystem": "^2.0", - "overtrue/qcloud-cos-client": "^1.0.0", - "php": ">=7.4" + "league/flysystem": "^3.0", + "overtrue/qcloud-cos-client": "^2.0", + "php": ">=8.0.2" }, "require-dev": { - "brainmaestro/composer-git-hooks": "^2.7", - "friendsofphp/php-cs-fixer": "^2.15", - "mockery/mockery": "~1.0", - "phpunit/phpunit": "~9" + "brainmaestro/composer-git-hooks": "dev-master", + "laravel/pint": "^1.6", + "league/flysystem-adapter-test-utilities": "^3.0", + "mockery/mockery": "^1.0", + "phpunit/phpunit": "^9.5" }, "type": "library", "extra": { "hooks": { "pre-commit": [ - "composer test", - "composer check-style" + "composer fix-style", + "composer test" ], "pre-push": [ "composer test", @@ -1140,7 +1205,7 @@ "description": "Flysystem adapter for the QCloud COS storage.", "support": { "issues": "https://github.com/overtrue/flysystem-cos/issues", - "source": "https://github.com/overtrue/flysystem-cos/tree/4.0.1" + "source": "https://github.com/overtrue/flysystem-cos/tree/5.1.4" }, "funding": [ { @@ -1148,32 +1213,43 @@ "type": "github" } ], - "time": "2021-11-10T09:20:45+00:00" + "time": "2023-05-26T09:00:43+00:00" }, { "name": "overtrue/flysystem-qiniu", - "version": "2.0.1", + "version": "3.2.2", "source": { "type": "git", "url": "https://github.com/overtrue/flysystem-qiniu.git", - "reference": "8d997760cff8967d9d6861d359c404f8ec6aa316" + "reference": "cf87e0fd74ba80736c7e0e9d19e1422eee6617d2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/overtrue/flysystem-qiniu/zipball/8d997760cff8967d9d6861d359c404f8ec6aa316", - "reference": "8d997760cff8967d9d6861d359c404f8ec6aa316", + "url": "https://api.github.com/repos/overtrue/flysystem-qiniu/zipball/cf87e0fd74ba80736c7e0e9d19e1422eee6617d2", + "reference": "cf87e0fd74ba80736c7e0e9d19e1422eee6617d2", "shasum": "" }, "require": { - "league/flysystem": "^2.0", - "php": ">=7.2", + "league/flysystem": "^3.0", "qiniu/php-sdk": "^7.2" }, "require-dev": { - "mockery/mockery": "1.3.1", - "phpunit/phpunit": "~8.0" + "mockery/mockery": "^1.4", + "phpunit/phpunit": "^9.5" }, "type": "library", + "extra": { + "hooks": { + "pre-commit": [ + "composer test", + "composer check-style" + ], + "pre-push": [ + "composer test", + "composer check-style" + ] + } + }, "autoload": { "psr-4": { "Overtrue\\Flysystem\\Qiniu\\": "src" @@ -1192,7 +1268,7 @@ "description": "Flysystem adapter for the Qiniu storage.", "support": { "issues": "https://github.com/overtrue/flysystem-qiniu/issues", - "source": "https://github.com/overtrue/flysystem-qiniu/tree/2.0.1" + "source": "https://github.com/overtrue/flysystem-qiniu/tree/3.2.2" }, "funding": [ { @@ -1200,20 +1276,20 @@ "type": "github" } ], - "time": "2022-08-12T03:23:12+00:00" + "time": "2023-05-02T13:14:45+00:00" }, { "name": "overtrue/qcloud-cos-client", - "version": "1.0.4", + "version": "2.0.2", "source": { "type": "git", "url": "https://github.com/overtrue/qcloud-cos-client.git", - "reference": "64ca47881afe4fd0961958f85674dcae71421913" + "reference": "238ef94beb0f856336e3cedd93b21c1c42228540" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/overtrue/qcloud-cos-client/zipball/64ca47881afe4fd0961958f85674dcae71421913", - "reference": "64ca47881afe4fd0961958f85674dcae71421913", + "url": "https://api.github.com/repos/overtrue/qcloud-cos-client/zipball/238ef94beb0f856336e3cedd93b21c1c42228540", + "reference": "238ef94beb0f856336e3cedd93b21c1c42228540", "shasum": "" }, "require": { @@ -1221,16 +1297,18 @@ "ext-json": "*", "ext-libxml": "*", "ext-simplexml": "*", - "guzzlehttp/guzzle": "^7.2", - "php": ">=7.4", - "psr/http-message": "^1.0" + "guzzlehttp/guzzle": "^7.4", + "php": ">=8.0.2", + "psr/http-message": "^1.0|^2.0", + "thenorthmemory/xml": "^1.0" }, "require-dev": { - "brainmaestro/composer-git-hooks": "^2.7", - "friendsofphp/php-cs-fixer": "^3.4.0", + "brainmaestro/composer-git-hooks": "^2.8", + "friendsofphp/php-cs-fixer": "^3.5", + "laravel/pint": "^1.2", "mockery/mockery": "^1.0", - "monolog/monolog": "^2.1", - "phpunit/phpunit": "^9.0" + "monolog/monolog": "^2.5", + "phpunit/phpunit": "^9.5" }, "type": "library", "extra": { @@ -1263,7 +1341,7 @@ "description": "Client of QCloud.com COS", "support": { "issues": "https://github.com/overtrue/qcloud-cos-client/issues", - "source": "https://github.com/overtrue/qcloud-cos-client/tree/1.0.4" + "source": "https://github.com/overtrue/qcloud-cos-client/tree/2.0.2" }, "funding": [ { @@ -1271,7 +1349,7 @@ "type": "github" } ], - "time": "2022-11-07T00:18:25+00:00" + "time": "2023-08-16T08:56:32+00:00" }, { "name": "phpoffice/phpspreadsheet", @@ -1910,6 +1988,57 @@ ], "time": "2022-01-02T09:55:41+00:00" }, + { + "name": "thenorthmemory/xml", + "version": "1.1.1", + "source": { + "type": "git", + "url": "https://github.com/TheNorthMemory/xml.git", + "reference": "6f50c63450a0b098772423f8bdc3c4ad2c4c30bb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/TheNorthMemory/xml/zipball/6f50c63450a0b098772423f8bdc3c4ad2c4c30bb", + "reference": "6f50c63450a0b098772423f8bdc3c4ad2c4c30bb", + "shasum": "" + }, + "require": { + "ext-libxml": "*", + "ext-simplexml": "*", + "php": ">=7.1.2" + }, + "require-dev": { + "phpstan/phpstan": "^0.12.89 || ^1.0", + "phpunit/phpunit": "^7.5 || ^8.5.16 || ^9.3.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "TheNorthMemory\\Xml\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "James ZHANG", + "homepage": "https://github.com/TheNorthMemory" + } + ], + "description": "A wrapper of the XML parser and builder", + "homepage": "https://github.com/TheNorthMemory/xml", + "keywords": [ + "xml-builder", + "xml-parser" + ], + "support": { + "issues": "https://github.com/TheNorthMemory/xml/issues", + "source": "https://github.com/TheNorthMemory/xml/tree/1.1.1" + }, + "time": "2023-01-15T06:01:13+00:00" + }, { "name": "topthink/framework", "version": "v8.0.2", @@ -2028,52 +2157,6 @@ }, "time": "2023-04-27T07:18:40+00:00" }, - { - "name": "topthink/think-filesystem", - "version": "v2.0.2", - "source": { - "type": "git", - "url": "https://github.com/top-think/think-filesystem.git", - "reference": "c08503232fcae0c3c7fefae5e6b5c841ffe09f2f" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/top-think/think-filesystem/zipball/c08503232fcae0c3c7fefae5e6b5c841ffe09f2f", - "reference": "c08503232fcae0c3c7fefae5e6b5c841ffe09f2f", - "shasum": "" - }, - "require": { - "league/flysystem": "^2.0", - "topthink/framework": "^6.1|^8.0" - }, - "require-dev": { - "mikey179/vfsstream": "^1.6", - "mockery/mockery": "^1.2", - "phpunit/phpunit": "^8.0" - }, - "type": "library", - "autoload": { - "psr-4": { - "think\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "Apache-2.0" - ], - "authors": [ - { - "name": "yunwuxin", - "email": "448901948@qq.com" - } - ], - "description": "The ThinkPHP6.1 Filesystem Package", - "support": { - "issues": "https://github.com/top-think/think-filesystem/issues", - "source": "https://github.com/top-think/think-filesystem/tree/v2.0.2" - }, - "time": "2023-02-08T01:23:42+00:00" - }, { "name": "topthink/think-helper", "version": "v3.1.6", diff --git a/extend/think/Filesystem.php b/extend/think/Filesystem.php new file mode 100644 index 0000000..0aee929 --- /dev/null +++ b/extend/think/Filesystem.php @@ -0,0 +1,89 @@ + +// +---------------------------------------------------------------------- +declare (strict_types = 1); + +namespace think; + +use InvalidArgumentException; +use think\filesystem\Driver; +use think\filesystem\driver\Local; +use think\helper\Arr; + +/** + * Class Filesystem + * @package think + * @mixin Driver + * @mixin Local + */ +class Filesystem extends Manager +{ + protected $namespace = '\\think\\filesystem\\driver\\'; + + /** + * @param null|string $name + * @return Driver + */ + public function disk(string $name = null): Driver + { + return $this->driver($name); + } + + protected function resolveType(string $name) + { + return $this->getDiskConfig($name, 'type', 'local'); + } + + protected function resolveConfig(string $name) + { + return $this->getDiskConfig($name); + } + + /** + * 获取缓存配置 + * @access public + * @param null|string $name 名称 + * @param mixed $default 默认值 + * @return mixed + */ + public function getConfig(string $name = null, $default = null) + { + if (!is_null($name)) { + return $this->app->config->get('filesystem.' . $name, $default); + } + + return $this->app->config->get('filesystem'); + } + + /** + * 获取磁盘配置 + * @param string $disk + * @param null $name + * @param null $default + * @return array + */ + public function getDiskConfig($disk, $name = null, $default = null) + { + if ($config = $this->getConfig("disks.{$disk}")) { + return Arr::get($config, $name, $default); + } + + throw new InvalidArgumentException("Disk [$disk] not found."); + } + + /** + * 默认驱动 + * @return string|null + */ + public function getDefaultDriver() + { + return $this->getConfig('default'); + } +} diff --git a/extend/think/facade/Filesystem.php b/extend/think/facade/Filesystem.php new file mode 100644 index 0000000..0e32c2c --- /dev/null +++ b/extend/think/facade/Filesystem.php @@ -0,0 +1,33 @@ + +// +---------------------------------------------------------------------- +declare (strict_types = 1); + +namespace think\facade; + +use think\Facade; +use think\filesystem\Driver; + +/** + * Class Filesystem + * @package think\facade + * @mixin \think\Filesystem + * @method static Driver disk(string $name = null) , null|string + * @method static mixed getConfig(null|string $name = null, mixed $default = null) 获取缓存配置 + * @method static array getDiskConfig(string $disk, null $name = null, null $default = null) 获取磁盘配置 + * @method static string|null getDefaultDriver() 默认驱动 + */ +class Filesystem extends Facade +{ + protected static function getFacadeClass() + { + return \think\Filesystem::class; + } +} diff --git a/extend/think/filesystem/Driver.php b/extend/think/filesystem/Driver.php new file mode 100644 index 0000000..e804141 --- /dev/null +++ b/extend/think/filesystem/Driver.php @@ -0,0 +1,130 @@ + +// +---------------------------------------------------------------------- +declare (strict_types = 1); + +namespace think\filesystem; + +use League\Flysystem\Filesystem; +use League\Flysystem\FilesystemAdapter; +use League\Flysystem\UnableToSetVisibility; +use League\Flysystem\UnableToWriteFile; +use RuntimeException; +use think\Cache; +use think\File; + +/** + * Class Driver + * @package think\filesystem + * @mixin Filesystem + */ +abstract class Driver +{ + + /** @var Cache */ + protected $cache; + + /** @var Filesystem */ + protected $filesystem; + + /** + * 配置参数 + * @var array + */ + protected $config = []; + + public function __construct(Cache $cache, array $config) + { + $this->cache = $cache; + $this->config = array_merge($this->config, $config); + + $adapter = $this->createAdapter(); + $this->filesystem = $this->createFilesystem($adapter); + } + + abstract protected function createAdapter(): FilesystemAdapter; + + protected function createFilesystem(FilesystemAdapter $adapter): Filesystem + { + $config = array_intersect_key($this->config, array_flip(['visibility', 'disable_asserts', 'url'])); + + return new Filesystem($adapter, $config); + } + + /** + * 获取文件完整路径 + * @param string $path + * @return string + */ + public function path(string $path): string + { + return $path; + } + + protected function concatPathToUrl($url, $path) + { + return rtrim($url, '/') . '/' . ltrim($path, '/'); + } + + public function url(string $path): string + { + throw new RuntimeException('This driver does not support retrieving URLs.'); + } + + /** + * 保存文件 + * @param string $path 路径 + * @param File $file 文件 + * @param null|string|\Closure $rule 文件名规则 + * @param array $options 参数 + * @return bool|string + */ + public function putFile(string $path, File $file, $rule = null, array $options = []) + { + return $this->putFileAs($path, $file, $file->hashName($rule), $options); + } + + /** + * 指定文件名保存文件 + * @param string $path 路径 + * @param File $file 文件 + * @param string $name 文件名 + * @param array $options 参数 + * @return bool|string + */ + public function putFileAs(string $path, File $file, string $name, array $options = []) + { + $stream = fopen($file->getRealPath(), 'r'); + $path = trim($path . '/' . $name, '/'); + + $result = $this->put($path, $stream, $options); + + if (is_resource($stream)) { + fclose($stream); + } + + return $result ? $path : false; + } + + protected function put(string $path, $contents, array $options = []) + { + try { + $this->writeStream($path, $contents, $options); + } catch (UnableToWriteFile|UnableToSetVisibility $e) { + return false; + } + return true; + } + + public function __call($method, $parameters) + { + return $this->filesystem->$method(...$parameters); + } +} diff --git a/extend/think/filesystem/driver/Local.php b/extend/think/filesystem/driver/Local.php new file mode 100644 index 0000000..3b1b64d --- /dev/null +++ b/extend/think/filesystem/driver/Local.php @@ -0,0 +1,98 @@ + +// +---------------------------------------------------------------------- +declare (strict_types = 1); + +namespace think\filesystem\driver; + +use League\Flysystem\FilesystemAdapter; +use League\Flysystem\Local\LocalFilesystemAdapter; +use League\Flysystem\PathNormalizer; +use League\Flysystem\PathPrefixer; +use League\Flysystem\UnixVisibility\PortableVisibilityConverter; +use League\Flysystem\Visibility; +use League\Flysystem\WhitespacePathNormalizer; +use think\filesystem\Driver; + +class Local extends Driver +{ + /** + * 配置参数 + * @var array + */ + protected $config = [ + 'root' => '', + ]; + + /** + * @var PathPrefixer + */ + protected $prefixer; + + /** + * @var PathNormalizer + */ + protected $normalizer; + + protected function createAdapter(): FilesystemAdapter + { + $visibility = PortableVisibilityConverter::fromArray( + $this->config['permissions'] ?? [], + $this->config['visibility'] ?? Visibility::PRIVATE + ); + + $links = ($this->config['links'] ?? null) === 'skip' + ? LocalFilesystemAdapter::SKIP_LINKS + : LocalFilesystemAdapter::DISALLOW_LINKS; + + return new LocalFilesystemAdapter( + $this->config['root'], + $visibility, + $this->config['lock'] ?? LOCK_EX, + $links + ); + } + + protected function prefixer() + { + if (!$this->prefixer) { + $this->prefixer = new PathPrefixer($this->config['root'], DIRECTORY_SEPARATOR); + } + return $this->prefixer; + } + + protected function normalizer() + { + if (!$this->normalizer) { + $this->normalizer = new WhitespacePathNormalizer(); + } + return $this->normalizer; + } + + /** + * 获取文件访问地址 + * @param string $path 文件路径 + * @return string + */ + public function url(string $path): string + { + $path = $this->normalizer()->normalizePath($path); + + if (isset($this->config['url'])) { + return $this->concatPathToUrl($this->config['url'], $path); + } + return parent::url($path); + } + + public function path(string $path): string + { + return $this->prefixer()->prefixPath($path); + } +} diff --git a/extend/think/filesystem/driver/Txcos.php b/extend/think/filesystem/driver/Txcos.php index f75ba64..23f0a1e 100644 --- a/extend/think/filesystem/driver/Txcos.php +++ b/extend/think/filesystem/driver/Txcos.php @@ -26,10 +26,7 @@ class Txcos extends Driver 'bucket' => $bucket, // 可选,如果 bucket 为私有访问请打开此项 - 'signed_url' => false, - - // 可选,是否使用 https,默认 false - 'use_https' => true, + 'signed_url' => false ]; $adapter = new CosAdapter($config);