33namespace Mhetreramesh \Flysystem ;
44
55use ChrisWhite \B2 \Client ;
6+ use GuzzleHttp \Psr7 \StreamWrapper ;
67use League \Flysystem \Adapter \AbstractAdapter ;
78use League \Flysystem \Adapter \Polyfill \NotSupportingVisibilityTrait ;
89use League \Flysystem \Config ;
910
10- class BackblazeAdapter extends AbstractAdapter {
11+ class BackblazeAdapter extends AbstractAdapter
12+ {
1113
1214 use NotSupportingVisibilityTrait;
1315
@@ -17,7 +19,7 @@ class BackblazeAdapter extends AbstractAdapter {
1719
1820 public function __construct (Client $ client , $ bucketName )
1921 {
20- $ this ->client = $ client ;
22+ $ this ->client = $ client ;
2123 $ this ->bucketName = $ bucketName ;
2224 }
2325
@@ -36,8 +38,8 @@ public function write($path, $contents, Config $config)
3638 {
3739 $ file = $ this ->getClient ()->upload ([
3840 'BucketName ' => $ this ->bucketName ,
39- 'FileName ' => $ path ,
40- 'Body ' => $ contents
41+ 'FileName ' => $ path ,
42+ 'Body ' => $ contents,
4143 ]);
4244 return $ this ->getFileInfo ($ file );
4345 }
@@ -49,8 +51,8 @@ public function writeStream($path, $resource, Config $config)
4951 {
5052 $ file = $ this ->getClient ()->upload ([
5153 'BucketName ' => $ this ->bucketName ,
52- 'FileName ' => $ path ,
53- 'Body ' => $ resource
54+ 'FileName ' => $ path ,
55+ 'Body ' => $ resource,
5456 ]);
5557 return $ this ->getFileInfo ($ file );
5658 }
@@ -62,8 +64,8 @@ public function update($path, $contents, Config $config)
6264 {
6365 $ file = $ this ->getClient ()->upload ([
6466 'BucketName ' => $ this ->bucketName ,
65- 'FileName ' => $ path ,
66- 'Body ' => $ contents
67+ 'FileName ' => $ path ,
68+ 'Body ' => $ contents,
6769 ]);
6870 return $ this ->getFileInfo ($ file );
6971 }
@@ -75,8 +77,8 @@ public function updateStream($path, $resource, Config $config)
7577 {
7678 $ file = $ this ->getClient ()->upload ([
7779 'BucketName ' => $ this ->bucketName ,
78- 'FileName ' => $ path ,
79- 'Body ' => $ resource
80+ 'FileName ' => $ path ,
81+ 'Body ' => $ resource,
8082 ]);
8183 return $ this ->getFileInfo ($ file );
8284 }
@@ -88,10 +90,10 @@ public function read($path)
8890 {
8991 $ file = $ this ->getClient ()->getFile ([
9092 'BucketName ' => $ this ->bucketName ,
91- 'FileName ' => $ path
93+ 'FileName ' => $ path,
9294 ]);
9395 $ fileContent = $ this ->getClient ()->download ([
94- 'FileId ' => $ file ->getId ()
96+ 'FileId ' => $ file ->getId (),
9597 ]);
9698 return ['contents ' => $ fileContent ];
9799 }
@@ -101,7 +103,19 @@ public function read($path)
101103 */
102104 public function readStream ($ path )
103105 {
104- return false ;
106+ $ file = $ this ->getClient ()->getFile ([
107+ 'BucketName ' => $ this ->bucketName ,
108+ 'FileName ' => $ path ,
109+ ]);
110+
111+ $ fileContent = $ this ->getClient ()->download ([
112+ 'FileId ' => $ file ->getId (),
113+ 'stream ' => true ,
114+ ]);
115+
116+ $ resource = StreamWrapper::getResource ($ fileContent );
117+
118+ return ['type ' => 'file ' , 'path ' => $ path , 'stream ' => $ resource ];
105119 }
106120
107121 /**
@@ -119,8 +133,8 @@ public function copy($path, $newPath)
119133 {
120134 return $ this ->getClient ()->upload ([
121135 'BucketName ' => $ this ->bucketName ,
122- 'FileName ' => $ newPath ,
123- 'Body ' => @file_get_contents ($ path )
136+ 'FileName ' => $ newPath ,
137+ 'Body ' => @file_get_contents ($ path ),
124138 ]);
125139 }
126140
@@ -147,8 +161,8 @@ public function createDir($path, Config $config)
147161 {
148162 return $ this ->getClient ()->upload ([
149163 'BucketName ' => $ this ->bucketName ,
150- 'FileName ' => $ path ,
151- 'Body ' => ''
164+ 'FileName ' => $ path ,
165+ 'Body ' => '' ,
152166 ]);
153167 }
154168
@@ -222,10 +236,10 @@ public function listContents($directory = '', $recursive = false)
222236 protected function getFileInfo ($ file )
223237 {
224238 $ normalized = [
225- 'type ' => 'file ' ,
226- 'path ' => $ file ->getName (),
239+ 'type ' => 'file ' ,
240+ 'path ' => $ file ->getName (),
227241 'timestamp ' => substr ($ file ->getUploadTimestamp (), 0 , -3 ),
228- 'size ' => $ file ->getSize ()
242+ 'size ' => $ file ->getSize (),
229243 ];
230244
231245 return $ normalized ;
0 commit comments