Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
zaxbux committed Jul 9, 2021
1 parent d879023 commit 7533921
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 81 deletions.
3 changes: 0 additions & 3 deletions docs/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ $client->bucket(Bucket|'bucketId')->listAllFileVersions();
// Get information on a file.
$client->file()->getInfo('fileId');

// Get a file by ID.
$client->file()->getById('fileId');

// Get a file by name.
$client->file()->getByName('fileName');

Expand Down
17 changes: 7 additions & 10 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Config

/**
* Custom Guzzle handler or handler stack.
* @var callable|\GuzzleHttp\HandlerStack
* @var \GuzzleHttp\HandlerStack
*/
private $handler;

Expand Down Expand Up @@ -161,14 +161,6 @@ public function middleware(): array

public function handler(): HandlerStack
{
if (!$this->handler) {
$this->handler = HandlerStack::create();
}

if (is_callable($this->handler) && !$this->handler instanceof HandlerStack) {
$this->handler = new HandlerStack(($this->handler));
}

return $this->handler;
}

Expand Down Expand Up @@ -202,7 +194,12 @@ public static function fromArray($data): Config
private function setOptions(array $options) {
$options = array_merge(static::DEFAULTS, $options);

$this->handler = $options['handler'];
$this->handler = $options['handler'] ?? HandlerStack::create();

if (is_callable($this->handler) && !$this->handler instanceof HandlerStack) {
$this->handler = new HandlerStack(($this->handler));
}

$this->maxRetries = $options['maxRetries'];
$this->maxFileCount = $options['maxFileCount'];
$this->maxKeyCount = $options['maxKeyCount'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
namespace Zaxbux\BackblazeB2\Exceptions;

/** @package BackblazeB2\Exceptions */
class NotFoundException extends \Exception {
class NoResultsException extends \Exception {

}
12 changes: 7 additions & 5 deletions src/Helpers/DownloadHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ public function download(
$options = DownloadOptions::fromArray($options ?? []);
}

// Build query string from query parameters and download options.
$queryString = implode('&', [http_build_query($query ?? []), $options->toQueryString() ?? []]);

$response = $this->getHttpClient()->request($headersOnly ? 'HEAD' : 'GET', $downloadUrl, [
'query' => $queryString,
$response = $this->getHttpClient()->request(
$headersOnly ? 'HEAD' : 'GET',
Utils::joinPaths(
$this->client->accountAuthorization()->downloadUrl(),
$downloadUrl
), [
'query' => Utils::filterRequestOptions([], $query, $options->getDownloadQueryOptions()),
'headers' => $options->getHeaders(),
'sink' => $sink ?? null,
'stream' => Utils::isStream($sink),
Expand Down
5 changes: 0 additions & 5 deletions src/Helpers/FileOperationsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ public function getInfo(string $fileId): File
return $this->client->getFileInfo($fileId);
}

public function getById(string $fileId): File
{
return $this->client->getFileById($fileId);
}

public function getByName(string $fileName): File
{
return $this->client->getFileByName($fileName);
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/UploadHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public function uploadStream(

// Upload as regular file
return $this->client->uploadFile(
$bucketId,
$fileName,
$bucketId,
$stream,
$contentType,
$fileInfo,
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class Endpoint
public const DELETE_FILE_VERSION = 'b2_delete_file_version';
public const DELETE_KEY = 'b2_delete_key';
public const DOWNLOAD_FILE_BY_ID = 'b2_download_file_by_id';
public const DOWNLOAD_FILE_BY_NAME = 'b2_download_file_by_name';
public const DOWNLOAD_FILE_BY_NAME = 'file';
public const FINISH_LARGE_FILE = 'b2_finish_large_file';
public const GET_DOWNLOAD_AUTHORIZATION = 'b2_get_download_authorization';
public const GET_FILE_INFO = 'b2_get_file_info';
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Middleware/ApplyAuthorizationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __invoke(callable $next): callable
protected function applyToken(RequestInterface $request): RequestInterface
{
$request = Psr7Utils::modifyRequest($request, [
'uri' => new Uri(Utils::joinPaths($this->client->accountAuthorization()->apiUrl(), (string)$request->getUri())),
'uri' => new Uri(Utils::joinPaths($request->getUri()->getHost() ? '' : $this->client->accountAuthorization()->apiUrl(), (string)$request->getUri())),
'set_headers' => [
'Authorization' => $this->client->accountAuthorization()->authorizationToken(),
],
Expand Down
30 changes: 15 additions & 15 deletions src/Object/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class File implements B2ObjectInterface
* @param int $partNumber
*/
public function __construct(
string $id,
?string $id = null,
?string $name = null,
?string $bucketId = null,
?string $action = null,
Expand Down Expand Up @@ -183,7 +183,7 @@ public function __construct(
/**
* Get the file account ID.
*/
public function accountId(): string
public function accountId(): ?string
{
return $this->accountId;
}
Expand All @@ -203,7 +203,7 @@ public function setAccountId(string $accountId): File
/**
* Get the file ID.
*/
public function id(): string
public function id(): ?string
{
return $this->id;
}
Expand All @@ -223,7 +223,7 @@ public function setId(string $id): File
/**
* Get the file name (path).
*/
public function name(): string
public function name(): ?string
{
return $this->name;
}
Expand All @@ -241,7 +241,7 @@ public function setName($name): File
/**
* Get the file bucket ID.
*/
public function bucketId(): string
public function bucketId(): ?string
{
return $this->bucketId;
}
Expand All @@ -261,7 +261,7 @@ public function setBucketId($bucketId)
/**
* Get the file action (type).
*/
public function action(): FileActionType
public function action(): ?FileActionType
{
return $this->action;
}
Expand All @@ -281,7 +281,7 @@ public function setAction(string $action): File
/**
* Get the file info.
*/
public function info(): FileInfo
public function info(): ?FileInfo
{
return $this->info;
}
Expand All @@ -301,7 +301,7 @@ public function setInfo($info): File
/**
* Get the file size.
*/
public function contentLength(): int
public function contentLength(): ?int
{
return $this->contentLength;
}
Expand All @@ -321,7 +321,7 @@ public function setContentLength(int $contentLength): File
/**
* Get the file type.
*/
public function contentType(): string
public function contentType(): ?string
{
return $this->contentType;
}
Expand All @@ -341,7 +341,7 @@ public function setContentType(string $contentType): File
/**
* Get the file SHA1 checksum.
*/
public function contentSha1(): string
public function contentSha1(): ?string
{
return $this->contentSha1;
}
Expand All @@ -361,7 +361,7 @@ public function setContentSha1(string $contentSha1): File
/**
* Get the file MD5 hash.
*/
public function contentMd5(): string
public function contentMd5(): ?string
{
return $this->contentMd5;
}
Expand All @@ -381,7 +381,7 @@ public function setContentMd5(string $contentMd5): File
/**
* Get the UTC timestamp when the file was uploaded. Always `0` if the action is `folder`.
*/
public function uploadTimestamp(): int
public function uploadTimestamp(): ?int
{
return $this->action->isFolder() ? 0 : $this->uploadTimestamp;
}
Expand All @@ -399,7 +399,7 @@ public function setUploadTimestamp(int $uploadTimestamp): File
/**
* Get the value of retention.
*/
public function retention(): array
public function retention(): ?array
{
return $this->retention;
}
Expand All @@ -419,7 +419,7 @@ public function setRetention(string $retention): File
/**
* Get the value of legalHold.
*/
public function legalHold(): array
public function legalHold(): ?array
{
return $this->legalHold;
}
Expand Down Expand Up @@ -503,7 +503,7 @@ public function pathInfo(): FilePathInfo
public static function fromArray(array $data): File
{
return new File(
$data[static::ATTRIBUTE_FILE_ID],
$data[static::ATTRIBUTE_FILE_ID] ?? null,
$data[static::ATTRIBUTE_FILE_NAME] ?? null,
$data[static::ATTRIBUTE_BUCKET_ID] ?? null,
$data[static::ATTRIBUTE_ACTION] ?? null,
Expand Down
4 changes: 2 additions & 2 deletions src/Object/File/DownloadOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ public function toArray() {
*
* @return string The query string.
*/
public function toQueryString() {
return http_build_query([
public function getDownloadQueryOptions() {
return array_filter([
static::OPTION_AUTHORIZATION => $this->authorization,
static::OPTION_CONTENT_DISPOSITION => $this->contentDisposition,
static::OPTION_CONTENT_ENCODING => $this->contentEncoding,
Expand Down
4 changes: 2 additions & 2 deletions src/Object/File/ServerSideEncryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ public static function fromArray(array $data, bool $rawKeys = false): ServerSide
$customerKeyMd5 = $data[static::ATTRIBUTE_CUSTOMER_KEY_MD5] ?? null;

return new ServerSideEncryption(
$data[static::ATTRIBUTE_MODE] ?? static::MODE_CUSTOMER,
$data[static::ATTRIBUTE_ALGORITHM] ?? static::ALGORITHM_AES256,
$data[static::ATTRIBUTE_MODE] ?? null,
$data[static::ATTRIBUTE_ALGORITHM] ?? null,
$customerKey ? ($rawKeys ? base64_encode($customerKey) : $customerKey) : null,
$customerKeyMd5 ? ($rawKeys ? base64_encode($customerKeyMd5) : $customerKeyMd5) : null
);
Expand Down
10 changes: 5 additions & 5 deletions src/Operations/BucketOperationsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Zaxbux\BackblazeB2\Operations;

use Zaxbux\BackblazeB2\Exceptions\NotFoundException;
use Zaxbux\BackblazeB2\Exceptions\NoResultsException;
use Zaxbux\BackblazeB2\Http\Endpoint;
use Zaxbux\BackblazeB2\Object\AccountAuthorization;
use Zaxbux\BackblazeB2\Object\Bucket;
Expand Down Expand Up @@ -182,7 +182,7 @@ public function updateBucket(
* @param string $bucketId The ID of the bucket to fetch. Defaults to the authorized bucket, if any.
* @param array|null $bucketTypes Filter for bucket types returned in the list buckets response.
*
* @throws NotFoundException
* @throws NoResultsException
*/
public function getBucketById(
?string $bucketId = null,
Expand All @@ -193,7 +193,7 @@ public function getBucketById(
$buckets = $response->getArrayCopy();

if (count($buckets) !== 1) {
throw new NotFoundException(sprintf('Bucket "%s" not found.', $bucketId));
throw new NoResultsException(sprintf('Bucket "%s" not found.', $bucketId));
}

return $buckets[0];
Expand All @@ -205,7 +205,7 @@ public function getBucketById(
* @param string $bucketName The name of the bucket to fetch.
* @param array|null $bucketTypes Filter for bucket types returned in the list buckets response.
*
* @throws NotFoundException
* @throws NoResultsException
*/
public function getBucketByName(string $bucketName, array $bucketTypes = null): Bucket
{
Expand All @@ -214,7 +214,7 @@ public function getBucketByName(string $bucketName, array $bucketTypes = null):
//$buckets = $response->getArrayCopy();

if (!$response->valid()) {
throw new NotFoundException(sprintf('Bucket "%s" not found.', $bucketName));
throw new NoResultsException(sprintf('Bucket "%s" not found.', $bucketName));
}

return $response->current();
Expand Down
9 changes: 2 additions & 7 deletions src/Operations/DownloadOperationsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ public function downloadFileById(
?bool $headersOnly = false
): FileDownload {
return DownloadHelper::instance($this)->download(
Utils::joinPaths(
$this->accountAuthorization()->downloadUrl(),
Client::B2_API_VERSION,
Endpoint::DOWNLOAD_FILE_BY_ID
),
Client::B2_API_VERSION .Endpoint::DOWNLOAD_FILE_BY_ID,
[File::ATTRIBUTE_FILE_ID => $fileId],
$options,
$sink,
Expand Down Expand Up @@ -114,8 +110,7 @@ public function downloadFileByName(
): FileDownload {
return DownloadHelper::instance($this)->download(
Utils::joinPaths(
$this->accountAuthorization()->apiUrl(),
'file',
Endpoint::DOWNLOAD_FILE_BY_NAME,
$bucketName ?? $this->allowedBucketName(),
$fileName
),
Expand Down
Loading

0 comments on commit 7533921

Please sign in to comment.