From 44c35f1915261dae60a7b30d6b7837821f232c13 Mon Sep 17 00:00:00 2001 From: overtrue Date: Thu, 17 Feb 2022 11:46:12 +0800 Subject: [PATCH] Add getTemporaryUrl() for laravel FilesystemAdapter --- README.md | 36 ++++++++++++++++++++++-------------- src/QiniuAdapter.php | 33 +++++++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index e2fc835..be38946 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,20 @@ -Flysystem Adapter for Qiniu Cloud Storage ---- +## Flysystem Adapter for Qiniu Cloud Storage :floppy_disk: Flysystem adapter for the Qiniu cloud storage. -[![Build Status](https://travis-ci.org/overtrue/flysystem-qiniu.svg?branch=master)](https://travis-ci.org/overtrue/flysystem-qiniu) -[![Latest Stable Version](https://poser.pugx.org/overtrue/flysystem-qiniu/v/stable.svg)](https://packagist.org/packages/overtrue/flysystem-qiniu) -[![Latest Unstable Version](https://poser.pugx.org/overtrue/flysystem-qiniu/v/unstable.svg)](https://packagist.org/packages/overtrue/flysystem-qiniu) -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/overtrue/flysystem-qiniu/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/overtrue/flysystem-qiniu/?branch=master) -[![Code Coverage](https://scrutinizer-ci.com/g/overtrue/flysystem-qiniu/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/overtrue/flysystem-qiniu/?branch=master) -[![Total Downloads](https://poser.pugx.org/overtrue/flysystem-qiniu/downloads)](https://packagist.org/packages/overtrue/flysystem-qiniu) +[![Build Status](https://travis-ci.org/overtrue/flysystem-qiniu.svg?branch=master)](https://travis-ci.org/overtrue/flysystem-qiniu) +[![Latest Stable Version](https://poser.pugx.org/overtrue/flysystem-qiniu/v/stable.svg)](https://packagist.org/packages/overtrue/flysystem-qiniu) +[![Latest Unstable Version](https://poser.pugx.org/overtrue/flysystem-qiniu/v/unstable.svg)](https://packagist.org/packages/overtrue/flysystem-qiniu) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/overtrue/flysystem-qiniu/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/overtrue/flysystem-qiniu/?branch=master) +[![Code Coverage](https://scrutinizer-ci.com/g/overtrue/flysystem-qiniu/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/overtrue/flysystem-qiniu/?branch=master) +[![Total Downloads](https://poser.pugx.org/overtrue/flysystem-qiniu/downloads)](https://packagist.org/packages/overtrue/flysystem-qiniu) [![License](https://poser.pugx.org/overtrue/flysystem-qiniu/license)](https://packagist.org/packages/overtrue/flysystem-qiniu) [![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me-button-s.svg?raw=true)](https://github.com/sponsors/overtrue) # Requirement -- PHP >= 8.0.2 +- PHP >= 8.0.2 # Installation @@ -55,22 +54,31 @@ bool $flysystem->directoryExists('path/to/dir'); string|false $flysystem->read('file.md'); array $flysystem->listContents(); int $flysystem->fileSize('file.md'); -string $flysystem->getAdapter()->getUrl('file.md'); string $flysystem->mimeType('file.md'); ``` +Adapter extended methods: + +```php +string $adapter->getUrl('file.md'); +bool|array $adapter->fetch(string $path, string $url); +array $adapter->refresh(string $path); +string $adapter->getTemporaryUrl($path, int|string|\DateTimeInterface $expiration); +string $adapter->privateDownloadUrl(string $path, int $expires = 3600); +string $adapter->getUploadToken(string $key = null, int $expires = 3600, string $policy = null, string $strictPolice = null) +``` + # Integration -- Laravel 5: [overtrue/laravel-filesystem-qiniu](https://github.com/overtrue/laravel-filesystem-qiniu) -- Yii2: [krissss/yii2-filesystem-qiniu](https://github.com/krissss/yii2-filesystem-qiniu) +- Laravel: [overtrue/laravel-filesystem-qiniu](https://github.com/overtrue/laravel-filesystem-qiniu) +- Yii2: [krissss/yii2-filesystem-qiniu](https://github.com/krissss/yii2-filesystem-qiniu) -## :heart: Sponsor me +## :heart: Sponsor me [![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me.svg?raw=true)](https://github.com/sponsors/overtrue) 如果你喜欢我的项目并想支持它,[点击这里 :heart:](https://github.com/sponsors/overtrue) - ## Project supported by JetBrains Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects. diff --git a/src/QiniuAdapter.php b/src/QiniuAdapter.php index 12fdab3..60dc049 100644 --- a/src/QiniuAdapter.php +++ b/src/QiniuAdapter.php @@ -182,6 +182,14 @@ public function copy(string $source, string $destination, Config $config): void } } + protected function getMetadata($path): FileAttributes|array + { + $result = $this->getBucketManager()->stat($this->bucket, $path); + $result[0]['key'] = $path; + + return $this->normalizeFileInfo($result[0]); + } + public function getUrl(string $path): string { $segments = $this->parseUrl($path); @@ -190,13 +198,6 @@ public function getUrl(string $path): string return $this->normalizeHost($this->domain) . ltrim(implode('/', array_map('rawurlencode', explode('/', $segments['path']))), '/') . $query; } - protected function getMetadata($path): FileAttributes|array - { - $result = $this->getBucketManager()->stat($this->bucket, $path); - $result[0]['key'] = $path; - - return $this->normalizeFileInfo($result[0]); - } public function fetch(string $path, string $url): bool|array { @@ -209,12 +210,28 @@ public function fetch(string $path, string $url): bool|array return $response; } + /** + * For laravel FilesystemAdapter. + */ + public function getTemporaryUrl($path, int|string|\DateTimeInterface $expiration): string + { + if ($expiration instanceof \DateTimeInterface) { + $expiration = $expiration->getTimestamp(); + } + + if (is_string($expiration)) { + $expiration = strtotime($expiration); + } + + return $this->privateDownloadUrl($path, $expiration); + } + public function privateDownloadUrl(string $path, int $expires = 3600): string { return $this->getAuthManager()->privateDownloadUrl($this->getUrl($path), $expires); } - public function refresh($path) + public function refresh(string $path) { if (is_string($path)) { $path = [$path];