From 146770c6f2db008cf9f7e173f36998e20a31a6ee Mon Sep 17 00:00:00 2001 From: Eldar Shahmaliyev Date: Mon, 9 Dec 2024 17:20:20 +0400 Subject: [PATCH] feat: Add support for handling blobs via arrays --- src/Contracts/DataModel/BlobContract.php | 1 + src/Contracts/DataModel/BlobHandler.php | 4 ++- src/DataModel/Blob/ArrayBlobHandler.php | 36 ++++++++++++++++++++++++ src/DataModel/Blob/BinaryBlobHandler.php | 15 ++++++++++ src/DataModel/Blob/Blob.php | 12 ++++---- src/DataModel/Blob/FileBlobHandler.php | 18 ++++++++++++ 6 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 src/DataModel/Blob/ArrayBlobHandler.php diff --git a/src/Contracts/DataModel/BlobContract.php b/src/Contracts/DataModel/BlobContract.php index e0f9afe..1ea1618 100644 --- a/src/Contracts/DataModel/BlobContract.php +++ b/src/Contracts/DataModel/BlobContract.php @@ -9,6 +9,7 @@ interface BlobContract extends Stringable, \JsonSerializable { public static function viaFile(FileSupport $file): BlobContract; public static function viaBinary(string $binary): BlobContract; + public static function viaArray(array $blobContent): BlobContract; public function size(): int; public function mimeType(): string; diff --git a/src/Contracts/DataModel/BlobHandler.php b/src/Contracts/DataModel/BlobHandler.php index 4eae7eb..ab2a63d 100644 --- a/src/Contracts/DataModel/BlobHandler.php +++ b/src/Contracts/DataModel/BlobHandler.php @@ -2,7 +2,9 @@ namespace Atproto\Contracts\DataModel; -interface BlobHandler +use Atproto\Contracts\Stringable; + +interface BlobHandler extends Stringable { public function size(): int; public function mimeType(): string; diff --git a/src/DataModel/Blob/ArrayBlobHandler.php b/src/DataModel/Blob/ArrayBlobHandler.php new file mode 100644 index 0000000..a06919c --- /dev/null +++ b/src/DataModel/Blob/ArrayBlobHandler.php @@ -0,0 +1,36 @@ +blob = $blob; + } + + public function size(): int + { + return Arr::get($this->blob, 'size'); + } + + public function mimeType(): string + { + return Arr::get($this->blob, 'mimeType'); + } + + public function content(): string + { + throw new \LogicException("Content does not exists for array blob handler."); + } + + public function __toString(): string + { + return Arr::get($this->blob, 'ref.$link'); + } +} \ No newline at end of file diff --git a/src/DataModel/Blob/BinaryBlobHandler.php b/src/DataModel/Blob/BinaryBlobHandler.php index 5f95024..b86ad8a 100644 --- a/src/DataModel/Blob/BinaryBlobHandler.php +++ b/src/DataModel/Blob/BinaryBlobHandler.php @@ -4,11 +4,15 @@ use Atproto\Contracts\DataModel\BlobHandler; use Atproto\Exceptions\InvalidArgumentException; +use Atproto\IPFS\CID\CID; +use Atproto\IPFS\MultiFormats\MultiBase\MultiBase; +use Atproto\IPFS\MultiFormats\MultiCodec; use finfo; class BinaryBlobHandler implements BlobHandler { private string $binary; + private CID $cid; /** * @throws InvalidArgumentException @@ -20,6 +24,12 @@ public function __construct(string $binary) } $this->binary = $binary; + + $this->cid = new CID( + MultiCodec::get('raw'), + MultiBase::get('base32'), + $this->binary + ); } private function isBinary(string $binary): bool @@ -41,4 +51,9 @@ public function content(): string { return $this->binary; } + + public function __toString(): string + { + return $this->cid->__toString(); + } } diff --git a/src/DataModel/Blob/Blob.php b/src/DataModel/Blob/Blob.php index 3db8967..3e6d8de 100644 --- a/src/DataModel/Blob/Blob.php +++ b/src/DataModel/Blob/Blob.php @@ -17,11 +17,6 @@ class Blob implements BlobContract private function __construct(BlobHandler $handler) { $this->handler = $handler; - $this->cid = new CID( - MultiCodec::get('raw'), - MultiBase::get('base32'), - $handler->content() - ); } public static function viaBinary(string $binary): BlobContract @@ -34,6 +29,11 @@ public static function viaFile(FileSupport $file): BlobContract return new self(new FileBlobHandler($file)); } + public static function viaArray(array $blobContent): BlobContract + { + return new self(new ArrayBlobHandler($blobContent)); + } + public function size(): int { return $this->handler->size(); @@ -51,7 +51,7 @@ public function __toString(): string public function link(): string { - return $this->cid->__toString(); + return $this->handler->__toString(); } public function jsonSerialize(): array diff --git a/src/DataModel/Blob/FileBlobHandler.php b/src/DataModel/Blob/FileBlobHandler.php index 72bd326..4c47901 100644 --- a/src/DataModel/Blob/FileBlobHandler.php +++ b/src/DataModel/Blob/FileBlobHandler.php @@ -4,12 +4,19 @@ use Atproto\Contracts\DataModel\BlobHandler; use Atproto\Exceptions\InvalidArgumentException; +use Atproto\IPFS\CID\CID; +use Atproto\IPFS\MultiFormats\MultiBase\MultiBase; +use Atproto\IPFS\MultiFormats\MultiCodec; use Atproto\Support\FileSupport; class FileBlobHandler implements BlobHandler { private FileSupport $file; + private CID $cid; + /** + * @throws InvalidArgumentException + */ public function __construct(FileSupport $file) { if (! $file->exists()) { @@ -25,6 +32,12 @@ public function __construct(FileSupport $file) } $this->file = $file; + + $this->cid = new CID( + MultiCodec::get('raw'), + MultiBase::get('base32'), + $this->file->getBlob() + ); } public function size(): int @@ -41,4 +54,9 @@ public function content(): string { return $this->file->getBlob(); } + + public function __toString(): string + { + return $this->cid->__toString(); + } }