Skip to content

Commit

Permalink
Merge pull request #35 from shahmal1yev/bss-94
Browse files Browse the repository at this point in the history
[BSS-94] Add support for handling blobs via array
  • Loading branch information
shahmal1yev authored Dec 9, 2024
2 parents 7f0b9d6 + 1724b48 commit 8e5d6aa
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fabric.properties

# PHPUnit
/app/phpunit.xml

phpunit.dist.xml
# Build data
/build/

Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
<directory suffix=".php">src</directory>
</include>
</coverage>

</phpunit>
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
46 changes: 46 additions & 0 deletions src/DataModel/Blob/ArrayBlobHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Atproto\DataModel\Blob;

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

class ArrayBlobHandler implements BlobHandler
{
private array $blob;

/**
* @throws InvalidArgumentException
*/
public function __construct(array $blob)
{
$this->blob = $blob;

if (! isset($this->blob['size'], $this->blob['mimeType'], $this->blob['ref']['$link'])) {
throw new InvalidArgumentException(
"ArrayBlobHandler requires 'ref.\$link', 'mimeType', and 'size' fields."
);
}
}

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();
}
}
51 changes: 51 additions & 0 deletions tests/Unit/DataModel/Blob/BlobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,55 @@ public function createSchema(string $link, string $mimeType = null): array
'size' => 7,
];
}

public function testViaArrayConstructorWorkingCorrectly(): void
{
$blobContent = [
'ref' => ['$link' => 'mock-link'],
'mimeType' => 'application/json',
'size' => 1024,
];

$blob = Blob::viaArray($blobContent);

$this->assertSame(1024, $blob->size());
$this->assertSame('application/json', $blob->mimeType());
$this->assertSame('mock-link', $blob->link());
}

public function testViaArrayConstructorThrowsExceptionForMissingFields(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
"ArrayBlobHandler requires 'ref.\$link', 'mimeType', and 'size' fields."
);

Blob::viaArray([
'mimeType' => 'application/json',
'size' => 1024,
// 'ref.$link' missing
]);
}

public function testJsonSerializeForArrayBlob(): void
{
$blobContent = [
'ref' => ['$link' => 'mock-link'],
'mimeType' => 'application/json',
'size' => 1024,
];

$blob = Blob::viaArray($blobContent);

$expectedSerialization = [
'$type' => 'blob',
'ref' => [
'$link' => 'mock-link',
],
'mimeType' => 'application/json',
'size' => 1024,
];

$this->assertSame($expectedSerialization, $blob->jsonSerialize());
}
}

0 comments on commit 8e5d6aa

Please sign in to comment.