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

Commit 57cb9cd

Browse files
authored
Merge pull request overtrue#4 from JellyBool/qiniu-support
Add Qiniu support
2 parents c59c297 + dd0b854 commit 57cb9cd

File tree

5 files changed

+121
-11
lines changed

5 files changed

+121
-11
lines changed

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,53 @@ $ composer require "overtrue/laravel-ueditor:~1.0"
4949

5050
# 说明
5151

52-
上传路径在:`public/uploads/` 下,确认该目录存在并可写。
52+
在 `config/ueditor.php` 配置 `disk` 为 `'local'` 情况下,上传路径在:`public/uploads/` 下,确认该目录存在并可写。
5353

5454
如果要修改上传路径,请在 `config/ueditor.php` 里各种类型的上传路径,但是都在 public 下。
5555

56+
# 七牛支持
57+
58+
如果你想使用七牛云储存,需要进行下面几个简单的操作:
59+
60+
1.配置 `config/ueditor.php` 的 `disk` 为 `qiniu`:
61+
62+
```php
63+
'disk' => 'qiniu'
64+
```
65+
66+
2.在 `config/filesystems.php` 添加下面的配置:
67+
68+
```php
69+
's3' => [
70+
'driver' => 's3',
71+
'key' => env('AWS_KEY'),
72+
'secret' => env('AWS_SECRET'),
73+
'region' => env('AWS_REGION'),
74+
'bucket' => env('AWS_BUCKET'),
75+
],
76+
77+
// 下面是添加的配置
78+
79+
'qiniu' => [
80+
'protocol' => 'http', // 域名对于的协议 http 或 https,默认 http
81+
'bucket' => env('QINIU_BUCKET_NAME'), // 七牛存储空间名字(bucket name),推荐使用公开空间
82+
'domain' => env('QINIU_BUCKET_DOMAIN'), // 七牛分配的域名
83+
'key' => env('QINIU_ACCESS_KEY'),
84+
'secret' => env('QINIU_SECRET_KEY'),
85+
],
86+
```
87+
88+
3.在 `.env` 文件添加配置:
89+
90+
```php
91+
QINIU_BUCKET_NAME=
92+
QINIU_BUCKET_DOMAIN=
93+
QINIU_ACCESS_KEY=
94+
QINIU_SECRET_KEY=
95+
```
96+
> 七牛的 `access_key``secret_Key` 可以在这里找到:https://portal.qiniu.com/user/key ,在创建 `bucket`
97+
(空间)的时候,推荐大家都使用公开的空间。
98+
5699
# License
57100

58101
MIT

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"Overtrue\\LaravelUEditor\\":"src/"
1414
}
1515
},
16-
"require": {},
16+
"require": {
17+
"qiniu/php-sdk": "7.*"
18+
},
1719
"require-dev": {
1820
"fabpot/php-cs-fixer": "^1.10"
1921
}

src/QiNiuStorage.php

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
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;
1418
use Symfony\Component\HttpFoundation\File\UploadedFile;
1519

1620
/**
@@ -22,28 +26,56 @@ class QiNiuStorage implements StorageInterface
2226
* Store file.
2327
*
2428
* @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
25-
* @param string $filename
29+
* @param string $filename
2630
*
2731
* @return mixed
2832
*/
2933
public function store(UploadedFile $file, $filename)
3034
{
31-
// TODO: Implement store() method.
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+
}
3248
}
3349

3450
/**
3551
* List files of path.
3652
*
3753
* @param string $path
38-
* @param int $start
39-
* @param int $size
40-
* @param array $allowFiles
54+
* @param int $start
55+
* @param int $size
56+
* @param array $allowFiles
4157
*
4258
* @return array
4359
*/
4460
public function lists($path, $start, $size = 20, array $allowFiles = [])
4561
{
46-
// TODO: Implement lists() method.
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+
}
76+
77+
return $files;
78+
}
4779
}
4880

4981
/**
@@ -55,6 +87,39 @@ public function lists($path, $start, $size = 20, array $allowFiles = [])
5587
*/
5688
public function getUrl($filename)
5789
{
58-
// TODO: Implement getUrl() method.
90+
return $this->getQiNiuUrl(basename($filename));
5991
}
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
108+
* @return string
109+
*/
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()
121+
{
122+
return new Operation(config('filesystems.disks.qiniu.domain'));
123+
}
124+
60125
}

src/StorageManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function listFiles($path, $start, $size = 20, array $allowFiles = [])
9090
*/
9191
public function getDefaultDriver()
9292
{
93-
return 'local';
93+
return $this->app['config']['ueditor.disk'];
9494
}
9595

9696
/**

src/config/ueditor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
return [
4-
// 存储引擎:filesystem.php 中 disks
4+
// 存储引擎:filesystem.php 中 disks,local 或 qiniu
55
'disk' => 'local',
66

77
'route' => [

0 commit comments

Comments
 (0)