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

Commit 0da877b

Browse files
committed
qiniu support version 1.0
1 parent c59c297 commit 0da877b

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

src/QiNiuStorage.php

Lines changed: 61 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,51 @@ 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+
list($result, $error) = $uploadManager->putFile($this->getQiNiuAuth()->uploadToken(config('filesystems.disks.qiniu.bucket')), basename($filename), $file->getRealPath());
37+
38+
if ($error !== null) {
39+
throw new StoreErrorException(trans('ERROR_UNKNOWN'));
40+
} else {
41+
// Anything todo here ?
42+
}
3243
}
3344

3445
/**
3546
* List files of path.
3647
*
3748
* @param string $path
38-
* @param int $start
39-
* @param int $size
40-
* @param array $allowFiles
49+
* @param int $start
50+
* @param int $size
51+
* @param array $allowFiles
4152
*
4253
* @return array
4354
*/
4455
public function lists($path, $start, $size = 20, array $allowFiles = [])
4556
{
46-
// TODO: Implement lists() method.
57+
$bucketManager = new BucketManager($this->getQiNiuAuth());
58+
59+
list($iterms, $marker, $error) = $bucketManager->listFiles(config('filesystems.disks.qiniu.bucket'), '', '', $size);
60+
61+
if ($error !== null) {
62+
throw new StoreErrorException(trans('ERROR_UNKNOWN'));
63+
} else {
64+
$files = [];
65+
foreach (collect($iterms)->sortBy('putTime', SORT_REGULAR, true)->toArray() as $file ) {
66+
$files[] = [
67+
'url' => $this->getQiNiuUrl($file['key']),
68+
'mtime' => $file['fsize'],
69+
];
70+
}
71+
72+
return $files;
73+
}
4774
}
4875

4976
/**
@@ -55,6 +82,33 @@ public function lists($path, $start, $size = 20, array $allowFiles = [])
5582
*/
5683
public function getUrl($filename)
5784
{
58-
// TODO: Implement getUrl() method.
85+
return $this->getQiNiuUrl(basename($filename));
5986
}
87+
88+
/**
89+
* @return string
90+
*/
91+
protected function getQiNiuAuth()
92+
{
93+
return new Auth(config('filesystems.disks.qiniu.key'), config('filesystems.disks.qiniu.secret'));
94+
}
95+
96+
97+
/**
98+
* @param $key
99+
* @return string
100+
*/
101+
protected function getQiNiuUrl($key)
102+
{
103+
return $this->getQiNiuOperation()->buildUrl($key, [], config('filesystems.disks.qiniu.protocol'));
104+
}
105+
106+
/**
107+
* @return Operation
108+
*/
109+
protected function getQiNiuOperation()
110+
{
111+
return new Operation(config('filesystems.disks.qiniu.domain'));
112+
}
113+
60114
}

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
@@ -2,7 +2,7 @@
22

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

77
'route' => [
88
'name' => '/ueditor/server',

0 commit comments

Comments
 (0)