Skip to content
This repository was archived by the owner on Aug 22, 2021. It is now read-only.

Commit cb9a9e0

Browse files
authored
Merge pull request overtrue#8 from JellyBool/qiniu-support
Add Qiniu support base on laravel-filesystem-qiniu
2 parents af72ef7 + 2b53547 commit cb9a9e0

File tree

2 files changed

+13
-97
lines changed

2 files changed

+13
-97
lines changed

README.md

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,42 +59,16 @@ $ composer require "overtrue/laravel-ueditor:~1.0"
5959

6060
如果你想使用七牛云储存,需要进行下面几个简单的操作:
6161

62-
1.配置 `config/ueditor.php` 的 `disk` 为 `qiniu`:
62+
1.安装和配置 [laravel-filesystem-qiniu](https://github.com/overtrue/laravel-filesystem-qiniu)
6363

64-
```php
65-
'disk' => 'qiniu'
66-
```
67-
68-
2.在 `config/filesystems.php` 添加下面的配置:
64+
2.配置 `config/ueditor.php` 的 `disk` 为 `qiniu`:
6965

7066
```php
71-
's3' => [
72-
'driver' => 's3',
73-
'key' => env('AWS_KEY'),
74-
'secret' => env('AWS_SECRET'),
75-
'region' => env('AWS_REGION'),
76-
'bucket' => env('AWS_BUCKET'),
77-
],
78-
79-
// 下面是添加的配置
80-
81-
'qiniu' => [
82-
'protocol' => 'http', // 域名对于的协议 http 或 https,默认 http
83-
'bucket' => env('QINIU_BUCKET_NAME'), // 七牛存储空间名字(bucket name),推荐使用公开空间
84-
'domain' => env('QINIU_BUCKET_DOMAIN'), // 七牛分配的域名
85-
'key' => env('QINIU_ACCESS_KEY'),
86-
'secret' => env('QINIU_SECRET_KEY'),
87-
],
67+
'disk' => 'qiniu'
8868
```
8969

90-
3.`.env` 文件添加配置:
70+
3.剩下时间打局 LOL,已经完事了。
9171

92-
```php
93-
QINIU_BUCKET_NAME=
94-
QINIU_BUCKET_DOMAIN=
95-
QINIU_ACCESS_KEY=
96-
QINIU_SECRET_KEY=
97-
```
9872
> 七牛的 `access_key``secret_Key` 可以在这里找到:https://portal.qiniu.com/user/key ,在创建 `bucket`
9973
(空间)的时候,推荐大家都使用公开的空间。
10074

src/QiNiuStorage.php

Lines changed: 9 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
*/
1212
namespace Overtrue\LaravelUEditor;
1313

14-
use Qiniu\Auth;
15-
use Qiniu\Processing\Operation;
16-
use Qiniu\Storage\BucketManager;
17-
use Qiniu\Storage\UploadManager;
14+
use Storage;
1815
use Symfony\Component\HttpFoundation\File\UploadedFile;
1916

2017
/**
@@ -32,19 +29,7 @@ class QiNiuStorage implements StorageInterface
3229
*/
3330
public function store(UploadedFile $file, $filename)
3431
{
35-
$uploadManager = new UploadManager();
36-
37-
list($result, $error) = $uploadManager->putFile(
38-
$this->getQiNiuAuth()->uploadToken(config('filesystems.disks.qiniu.bucket')),
39-
basename($filename),
40-
$file->getRealPath()
41-
);
42-
43-
if ($error !== null) {
44-
throw new StoreErrorException(trans('ERROR_UNKNOWN'));
45-
} else {
46-
// Anything todo here ?
47-
}
32+
Storage::disk('qiniu')->writeStream($filename, fopen($file->getRealPath(), 'r'));
4833
}
4934

5035
/**
@@ -59,67 +44,24 @@ public function store(UploadedFile $file, $filename)
5944
*/
6045
public function lists($path, $start, $size = 20, array $allowFiles = [])
6146
{
62-
$bucketManager = new BucketManager($this->getQiNiuAuth());
63-
64-
list($iterms, $marker, $error) = $bucketManager->listFiles(config('filesystems.disks.qiniu.bucket'), '', '', $size);
65-
66-
if ($error !== null) {
67-
throw new StoreErrorException(trans('ERROR_UNKNOWN'));
68-
} else {
69-
$files = [];
70-
foreach (collect($iterms)->sortBy('putTime', SORT_REGULAR, true)->toArray() as $file ) {
71-
$files[] = [
72-
'url' => $this->getQiNiuUrl($file['key']),
73-
'mtime' => $file['putTime'],
74-
];
75-
}
47+
$contents = Storage::disk('qiniu')->listContents($path,true);
7648

49+
return collect($contents)->map(function ($file) {
50+
$files['url'] = $this->getUrl('/'.$file['path']);
51+
$files['mtime'] = $file['timestamp'];
7752
return $files;
78-
}
53+
});
7954
}
8055

8156
/**
8257
* Make the url of file.
8358
*
8459
* @param $filename
8560
*
86-
* @return mixed
87-
*/
88-
public function getUrl($filename)
89-
{
90-
return $this->getQiNiuUrl(basename($filename));
91-
}
92-
93-
/**
94-
* Get QiNiu auth object.
95-
*
96-
* @return string
97-
*/
98-
protected function getQiNiuAuth()
99-
{
100-
return new Auth(config('filesystems.disks.qiniu.key'), config('filesystems.disks.qiniu.secret'));
101-
}
102-
103-
104-
/**
105-
* Get QiNiu url base on file key.
106-
*
107-
* @param $key
10861
* @return string
10962
*/
110-
protected function getQiNiuUrl($key)
111-
{
112-
return $this->getQiNiuOperation()->buildUrl($key, [], config('filesystems.disks.qiniu.protocol'));
113-
}
114-
115-
/**
116-
* Get QiNiu operation object.
117-
*
118-
* @return Operation
119-
*/
120-
protected function getQiNiuOperation()
63+
public function getUrl($filename)
12164
{
122-
return new Operation(config('filesystems.disks.qiniu.domain'));
65+
return 'http://' . config('filesystems.disks.qiniu.domain') . $filename;
12366
}
124-
12567
}

0 commit comments

Comments
 (0)