11
11
*/
12
12
namespace Overtrue \LaravelUEditor ;
13
13
14
+ use Qiniu \Auth ;
15
+ use Qiniu \Processing \Operation ;
16
+ use Qiniu \Storage \BucketManager ;
17
+ use Qiniu \Storage \UploadManager ;
14
18
use Symfony \Component \HttpFoundation \File \UploadedFile ;
15
19
16
20
/**
@@ -22,28 +26,51 @@ class QiNiuStorage implements StorageInterface
22
26
* Store file.
23
27
*
24
28
* @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
25
- * @param string $filename
29
+ * @param string $filename
26
30
*
27
31
* @return mixed
28
32
*/
29
33
public function store (UploadedFile $ file , $ filename )
30
34
{
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
+ }
32
43
}
33
44
34
45
/**
35
46
* List files of path.
36
47
*
37
48
* @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
41
52
*
42
53
* @return array
43
54
*/
44
55
public function lists ($ path , $ start , $ size = 20 , array $ allowFiles = [])
45
56
{
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
+ }
47
74
}
48
75
49
76
/**
@@ -55,6 +82,33 @@ public function lists($path, $start, $size = 20, array $allowFiles = [])
55
82
*/
56
83
public function getUrl ($ filename )
57
84
{
58
- // TODO: Implement getUrl() method.
85
+ return $ this -> getQiNiuUrl ( basename ( $ filename ));
59
86
}
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
+
60
114
}
0 commit comments