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

Commit 4ffed4b

Browse files
committed
add qiniu support base one overtrue/laravel-filesystem-qiniu
1 parent dd0b854 commit 4ffed4b

File tree

1 file changed

+9
-68
lines changed

1 file changed

+9
-68
lines changed

src/QiNiuStorage.php

Lines changed: 9 additions & 68 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,23 @@ 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-
}
76-
47+
$contents = Storage::disk('qiniu')->listContents($path,true);
48+
return collect($contents)->map(function ($file) {
49+
$files['url'] = $this->getUrl('/'.$file['path']);
50+
$files['mtime'] = $file['timestamp'];
7751
return $files;
78-
}
52+
});
7953
}
8054

8155
/**
8256
* Make the url of file.
8357
*
8458
* @param $filename
8559
*
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-
*
9660
* @return string
9761
*/
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()
62+
public function getUrl($filename)
12163
{
122-
return new Operation(config('filesystems.disks.qiniu.domain'));
64+
return 'http://' . config('filesystems.disks.qiniu.domain') . $filename;
12365
}
124-
12566
}

0 commit comments

Comments
 (0)