Skip to content

Commit

Permalink
feat: Add support for handling blobs via arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
shahmal1yev committed Dec 9, 2024
1 parent c526b17 commit 146770c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/Contracts/DataModel/BlobContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/Contracts/DataModel/BlobHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions src/DataModel/Blob/ArrayBlobHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Atproto\DataModel\Blob;

use Atproto\Contracts\DataModel\BlobHandler;
use Atproto\Support\Arr;

class ArrayBlobHandler implements BlobHandler
{
private array $blob;

public function __construct(array $blob)
{
$this->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');
}
}
15 changes: 15 additions & 0 deletions src/DataModel/Blob/BinaryBlobHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -41,4 +51,9 @@ public function content(): string
{
return $this->binary;
}

public function __toString(): string
{
return $this->cid->__toString();
}
}
12 changes: 6 additions & 6 deletions src/DataModel/Blob/Blob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -51,7 +51,7 @@ public function __toString(): string

public function link(): string
{
return $this->cid->__toString();
return $this->handler->__toString();
}

public function jsonSerialize(): array
Expand Down
18 changes: 18 additions & 0 deletions src/DataModel/Blob/FileBlobHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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
Expand All @@ -41,4 +54,9 @@ public function content(): string
{
return $this->file->getBlob();
}

public function __toString(): string
{
return $this->cid->__toString();
}
}

0 comments on commit 146770c

Please sign in to comment.