Skip to content

Commit

Permalink
enable read stream
Browse files Browse the repository at this point in the history
  • Loading branch information
coolcodemy committed Aug 24, 2017
1 parent 893dffb commit 7d915e1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"require": {
"php": ">=5.5.0",
"league/flysystem": "~1.0",
"cwhite92/b2-sdk-php" : "^1.2",
"runcloudio/b2-sdk-php" : "^1.2",
"mikey179/vfsStream" : "*"
},
"require-dev": {
Expand Down
54 changes: 34 additions & 20 deletions src/BackblazeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Mhetreramesh\Flysystem;

use ChrisWhite\B2\Client;
use GuzzleHttp\Psr7\StreamWrapper;
use League\Flysystem\Adapter\AbstractAdapter;
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;
use League\Flysystem\Config;

class BackblazeAdapter extends AbstractAdapter {
class BackblazeAdapter extends AbstractAdapter
{

use NotSupportingVisibilityTrait;

Expand All @@ -17,7 +19,7 @@ class BackblazeAdapter extends AbstractAdapter {

public function __construct(Client $client, $bucketName)
{
$this->client = $client;
$this->client = $client;
$this->bucketName = $bucketName;
}

Expand All @@ -36,8 +38,8 @@ public function write($path, $contents, Config $config)
{
$file = $this->getClient()->upload([
'BucketName' => $this->bucketName,
'FileName' => $path,
'Body' => $contents
'FileName' => $path,
'Body' => $contents,
]);
return $this->getFileInfo($file);
}
Expand All @@ -49,8 +51,8 @@ public function writeStream($path, $resource, Config $config)
{
$file = $this->getClient()->upload([
'BucketName' => $this->bucketName,
'FileName' => $path,
'Body' => $resource
'FileName' => $path,
'Body' => $resource,
]);
return $this->getFileInfo($file);
}
Expand All @@ -62,8 +64,8 @@ public function update($path, $contents, Config $config)
{
$file = $this->getClient()->upload([
'BucketName' => $this->bucketName,
'FileName' => $path,
'Body' => $contents
'FileName' => $path,
'Body' => $contents,
]);
return $this->getFileInfo($file);
}
Expand All @@ -75,8 +77,8 @@ public function updateStream($path, $resource, Config $config)
{
$file = $this->getClient()->upload([
'BucketName' => $this->bucketName,
'FileName' => $path,
'Body' => $resource
'FileName' => $path,
'Body' => $resource,
]);
return $this->getFileInfo($file);
}
Expand All @@ -88,10 +90,10 @@ public function read($path)
{
$file = $this->getClient()->getFile([
'BucketName' => $this->bucketName,
'FileName' => $path
'FileName' => $path,
]);
$fileContent = $this->getClient()->download([
'FileId' => $file->getId()
'FileId' => $file->getId(),
]);
return ['contents' => $fileContent];
}
Expand All @@ -101,7 +103,19 @@ public function read($path)
*/
public function readStream($path)
{
return false;
$file = $this->getClient()->getFile([
'BucketName' => $this->bucketName,
'FileName' => $path,
]);

$fileContent = $this->getClient()->download([
'FileId' => $file->getId(),
'stream' => true,
]);

$resource = StreamWrapper::getResource($fileContent);

return ['type' => 'file', 'path' => $path, 'stream' => $resource];
}

/**
Expand All @@ -119,8 +133,8 @@ public function copy($path, $newPath)
{
return $this->getClient()->upload([
'BucketName' => $this->bucketName,
'FileName' => $newPath,
'Body' => @file_get_contents($path)
'FileName' => $newPath,
'Body' => @file_get_contents($path),
]);
}

Expand All @@ -147,8 +161,8 @@ public function createDir($path, Config $config)
{
return $this->getClient()->upload([
'BucketName' => $this->bucketName,
'FileName' => $path,
'Body' => ''
'FileName' => $path,
'Body' => '',
]);
}

Expand Down Expand Up @@ -222,10 +236,10 @@ public function listContents($directory = '', $recursive = false)
protected function getFileInfo($file)
{
$normalized = [
'type' => 'file',
'path' => $file->getName(),
'type' => 'file',
'path' => $file->getName(),
'timestamp' => substr($file->getUploadTimestamp(), 0, -3),
'size' => $file->getSize()
'size' => $file->getSize(),
];

return $normalized;
Expand Down

0 comments on commit 7d915e1

Please sign in to comment.