mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #13501 from appwrite/feat/storage-auto-gravity
Add automatic image crop gravity
This commit is contained in:
15 files changed
+515
-9
No files matched your search
@@ -76,6 +76,7 @@ _APP_EMBEDDING_TIMEOUT=30000
|
||||
_APP_STORAGE_DEVICE=Local
|
||||
_APP_STORAGE_LIMIT=30000000
|
||||
_APP_STORAGE_PREVIEW_LIMIT=20000000
|
||||
_APP_AUTOGRAVITY_HOST=http://appwrite-autogravity:8080
|
||||
_APP_STORAGE_ANTIVIRUS=disabled
|
||||
_APP_STORAGE_ANTIVIRUS_HOST=clamav
|
||||
_APP_STORAGE_ANTIVIRUS_PORT=3310
|
||||
|
||||
@@ -904,6 +904,16 @@ return [
|
||||
'question' => '',
|
||||
'filter' => ''
|
||||
],
|
||||
[
|
||||
'name' => '_APP_AUTOGRAVITY_HOST',
|
||||
'description' => 'The host used by Appwrite to determine automatic image crop gravity.',
|
||||
'introduction' => '2.0.0',
|
||||
'default' => '',
|
||||
'required' => false,
|
||||
'overwrite' => true,
|
||||
'question' => '',
|
||||
'filter' => ''
|
||||
],
|
||||
[
|
||||
'name' => '_APP_STORAGE_ANTIVIRUS',
|
||||
'description' => 'This variable allows you to disable the internal anti-virus scans. This value is set to \'disabled\' by default, to enable the scans set the value to \'enabled\'. Before enabling, you must add the ClamAV service and depend on it on main Appwrite service.',
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Appwrite\Autogravity\Client as AutogravityClient;
|
||||
use Appwrite\Autogravity\Detector as AutogravityDetector;
|
||||
use Appwrite\Certificates\Certificates;
|
||||
use Appwrite\Database\Factory as DatabaseFactory;
|
||||
use Appwrite\Event\Event;
|
||||
@@ -102,6 +104,17 @@ $container->set('screenshots', function () {
|
||||
return new ScreenshotsClient($client);
|
||||
}, []);
|
||||
|
||||
$container->set('autogravity', function (Cache $cache) {
|
||||
$host = System::getEnv('_APP_AUTOGRAVITY_HOST', '');
|
||||
$client = $host === ''
|
||||
? null
|
||||
: (new Client(new SwooleClientAdapter()))
|
||||
->withBaseUri($host)
|
||||
->withTimeout(30);
|
||||
|
||||
return new AutogravityDetector($client === null ? null : new AutogravityClient($client), $cache);
|
||||
}, ['cache']);
|
||||
|
||||
$container->set('telemetry', fn () => new NoTelemetry(), []);
|
||||
|
||||
$container->set('authorization', fn () => new Authorization(), []);
|
||||
|
||||
Generated
+6
-6
@@ -4108,16 +4108,16 @@
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/image",
|
||||
"version": "0.8.9",
|
||||
"version": "0.8.12",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/utopia-php/image.git",
|
||||
"reference": "6647714267e43bae25c8e8ae7021264eca4a1e84"
|
||||
"reference": "cab2779faf1e02f3bed9bc5cddd6350a69056948"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/utopia-php/image/zipball/6647714267e43bae25c8e8ae7021264eca4a1e84",
|
||||
"reference": "6647714267e43bae25c8e8ae7021264eca4a1e84",
|
||||
"url": "https://api.github.com/repos/utopia-php/image/zipball/cab2779faf1e02f3bed9bc5cddd6350a69056948",
|
||||
"reference": "cab2779faf1e02f3bed9bc5cddd6350a69056948",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -4148,9 +4148,9 @@
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/utopia-php/image/issues",
|
||||
"source": "https://github.com/utopia-php/image/tree/0.8.9"
|
||||
"source": "https://github.com/utopia-php/image/tree/0.8.12"
|
||||
},
|
||||
"time": "2026-07-20T18:04:09+00:00"
|
||||
"time": "2026-09-07T10:00:40+00:00"
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/locale",
|
||||
|
||||
@@ -76,6 +76,7 @@ services:
|
||||
- redis
|
||||
- ${_APP_DB_HOST:-postgresql}
|
||||
- appwrite-geo
|
||||
- appwrite-autogravity
|
||||
environment:
|
||||
- _APP_ENV
|
||||
- _APP_USAGE_STATS
|
||||
@@ -163,6 +164,7 @@ services:
|
||||
- _APP_SMTP_PASSWORD
|
||||
- _APP_STORAGE_LIMIT
|
||||
- _APP_STORAGE_PREVIEW_LIMIT
|
||||
- _APP_AUTOGRAVITY_HOST
|
||||
- _APP_STORAGE_ANTIVIRUS
|
||||
- _APP_STORAGE_ANTIVIRUS_HOST
|
||||
- _APP_STORAGE_ANTIVIRUS_PORT
|
||||
@@ -1578,6 +1580,13 @@ services:
|
||||
networks:
|
||||
- appwrite
|
||||
|
||||
appwrite-autogravity:
|
||||
container_name: appwrite-autogravity
|
||||
image: ghcr.io/appwrite/autogravity:0.0.6
|
||||
restart: unless-stopped
|
||||
networks:
|
||||
- appwrite
|
||||
|
||||
clickhouse:
|
||||
image: clickhouse/clickhouse-server:26.4.3-alpine
|
||||
restart: unless-stopped
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Autogravity;
|
||||
|
||||
use Psr\Http\Client\ClientExceptionInterface;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Utopia\Psr7\Method;
|
||||
use Utopia\Psr7\Request\Factory;
|
||||
|
||||
class Client
|
||||
{
|
||||
private Factory $factory;
|
||||
|
||||
public function __construct(private readonly ClientInterface $client)
|
||||
{
|
||||
$this->factory = new Factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the image's focal point.
|
||||
*
|
||||
* @throws Exception on an error response or malformed result
|
||||
* @throws ClientExceptionInterface on transport failure
|
||||
*/
|
||||
public function analyze(string $image): Gravity
|
||||
{
|
||||
$request = $this->factory->body(
|
||||
Method::POST,
|
||||
'analyze',
|
||||
$image,
|
||||
'application/octet-stream'
|
||||
);
|
||||
$response = $this->client->sendRequest($request);
|
||||
$status = $response->getStatusCode();
|
||||
$body = (string) $response->getBody();
|
||||
$result = \json_decode($body, true);
|
||||
|
||||
if ($status >= 400) {
|
||||
$error = \is_array($result) ? ($result['error'] ?? null) : null;
|
||||
throw new Exception(\is_string($error) ? $error : 'Autogravity failed with status ' . $status, $status);
|
||||
}
|
||||
|
||||
$x = \is_array($result) ? ($result['gravity']['x'] ?? null) : null;
|
||||
$y = \is_array($result) ? ($result['gravity']['y'] ?? null) : null;
|
||||
if (!\is_numeric($x) || !\is_numeric($y)) {
|
||||
throw new Exception('Autogravity returned an invalid gravity', $status);
|
||||
}
|
||||
|
||||
return new Gravity((float) $x, (float) $y);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Autogravity;
|
||||
|
||||
use Utopia\Cache\Cache;
|
||||
|
||||
class Detector
|
||||
{
|
||||
private const CACHE_TTL = 15_552_000;
|
||||
|
||||
public function __construct(
|
||||
private readonly ?Client $client,
|
||||
private readonly Cache $cache
|
||||
) {
|
||||
}
|
||||
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
return $this->client !== null;
|
||||
}
|
||||
|
||||
public function get(string $source): Gravity
|
||||
{
|
||||
if ($this->client === null) {
|
||||
throw new Exception('Autogravity needs to be configured with _APP_AUTOGRAVITY_HOST to use automatic gravity');
|
||||
}
|
||||
|
||||
$key = 'autogravity-' . \hash('sha256', $source);
|
||||
$cached = $this->cache->load($key, self::CACHE_TTL);
|
||||
if (
|
||||
\is_array($cached)
|
||||
&& isset($cached['x'], $cached['y'])
|
||||
&& \is_numeric($cached['x'])
|
||||
&& \is_numeric($cached['y'])
|
||||
&& $cached['x'] >= 0
|
||||
&& $cached['x'] <= 1
|
||||
&& $cached['y'] >= 0
|
||||
&& $cached['y'] <= 1
|
||||
) {
|
||||
return new Gravity((float) $cached['x'], (float) $cached['y']);
|
||||
}
|
||||
|
||||
$gravity = $this->client->analyze($source);
|
||||
$this->cache->save($key, $gravity->getArrayCopy());
|
||||
|
||||
return $gravity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Autogravity;
|
||||
|
||||
class Exception extends \Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Autogravity;
|
||||
|
||||
class Gravity
|
||||
{
|
||||
public function __construct(
|
||||
public readonly float $x,
|
||||
public readonly float $y
|
||||
) {
|
||||
if ($x < 0 || $x > 1 || $y < 0 || $y > 1) {
|
||||
throw new Exception('Autogravity returned an invalid gravity');
|
||||
}
|
||||
}
|
||||
|
||||
public function unrotate(int $rotation): self
|
||||
{
|
||||
return match (($rotation % 360 + 360) % 360) {
|
||||
0 => $this,
|
||||
90 => new self($this->y, 1 - $this->x),
|
||||
180 => new self(1 - $this->x, 1 - $this->y),
|
||||
270 => new self(1 - $this->y, $this->x),
|
||||
default => throw new Exception('Autogravity cannot reverse an unsupported rotation'),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{x: float, y: float}
|
||||
*/
|
||||
public function getArrayCopy(): array
|
||||
{
|
||||
return ['x' => $this->x, 'y' => $this->y];
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Appwrite\Platform\Modules\Storage\Http\Buckets\Files\Preview;
|
||||
|
||||
use Appwrite\Autogravity\Detector as AutogravityDetector;
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\OpenSSL\OpenSSL;
|
||||
use Appwrite\Platform\Action;
|
||||
@@ -40,6 +41,8 @@ class Get extends Action
|
||||
{
|
||||
use HTTP;
|
||||
|
||||
private const GRAVITY_AUTO = 'auto';
|
||||
|
||||
public static function getName()
|
||||
{
|
||||
return 'getFilePreview';
|
||||
@@ -79,7 +82,7 @@ class Get extends Action
|
||||
->param('fileId', '', new UID(), 'File ID')
|
||||
->param('width', 0, new Range(0, 4000), 'Resize preview image width, Pass an integer between 0 to 4000.', true)
|
||||
->param('height', 0, new Range(0, 4000), 'Resize preview image height, Pass an integer between 0 to 4000.', true)
|
||||
->param('gravity', Image::GRAVITY_CENTER, new WhiteList(Image::getGravityTypes()), 'Image crop gravity. Can be one of ' . implode(",", Image::getGravityTypes()), true, enum: new Enum(name: 'ImageGravity'))
|
||||
->param('gravity', Image::GRAVITY_CENTER, new WhiteList([self::GRAVITY_AUTO, ...Image::getGravityTypes()]), 'Image crop gravity. Can be one of ' . implode(",", [self::GRAVITY_AUTO, ...Image::getGravityTypes()]), true, enum: new Enum(name: 'ImageGravity'))
|
||||
->param('quality', -1, new Range(-1, 100), 'Preview image quality. Pass an integer between 0 to 100. Defaults to keep existing image quality.', true)
|
||||
->param('borderWidth', 0, new Range(0, 100), 'Preview image border in pixels. Pass an integer between 0 to 100. Defaults to 0.', true)
|
||||
->param('borderColor', '', new HexColor(), 'Preview image border color. Use a valid HEX color, no # is needed for prefix.', true)
|
||||
@@ -100,6 +103,7 @@ class Get extends Action
|
||||
->inject('authorization')
|
||||
->inject('user')
|
||||
->inject('cacheControlForStorage')
|
||||
->inject('autogravity')
|
||||
->callback($this->action(...));
|
||||
}
|
||||
|
||||
@@ -127,7 +131,8 @@ class Get extends Action
|
||||
Document $project,
|
||||
Authorization $authorization,
|
||||
User $user,
|
||||
callable $cacheControlForStorage
|
||||
callable $cacheControlForStorage,
|
||||
AutogravityDetector $autogravity
|
||||
) {
|
||||
|
||||
if (!\extension_loaded('imagick')) {
|
||||
@@ -264,11 +269,34 @@ class Get extends Action
|
||||
throw new Exception(Exception::STORAGE_FILE_TYPE_UNSUPPORTED, $e->getMessage());
|
||||
}
|
||||
|
||||
if ($gravity === self::GRAVITY_AUTO && !$autogravity->isEnabled()) {
|
||||
throw new Exception(
|
||||
Exception::GENERAL_ARGUMENT_INVALID,
|
||||
'Autogravity needs to be configured with _APP_AUTOGRAVITY_HOST to use automatic gravity'
|
||||
);
|
||||
}
|
||||
|
||||
$focalPoint = null;
|
||||
if (
|
||||
$gravity === self::GRAVITY_AUTO
|
||||
&& $width > 0
|
||||
&& $height > 0
|
||||
&& isset($sourceWidth, $sourceHeight)
|
||||
&& \abs($width / $height - $sourceWidth / $sourceHeight) > 0.000001
|
||||
) {
|
||||
$focalPoint = $autogravity
|
||||
->get($source)
|
||||
->unrotate($this->getAutogravityRotation($source));
|
||||
$gravity = Image::GRAVITY_CENTER;
|
||||
} elseif ($gravity === self::GRAVITY_AUTO) {
|
||||
$gravity = Image::GRAVITY_CENTER;
|
||||
}
|
||||
|
||||
if ($width > 0 || $height > 0 || $gravity !== Image::GRAVITY_CENTER) {
|
||||
Span::add('storage.transform.crop.width', $width);
|
||||
Span::add('storage.transform.crop.height', $height);
|
||||
Span::add('storage.transform.crop.gravity', $gravity);
|
||||
$image->crop($width, $height, $gravity);
|
||||
$image->crop($width, $height, $gravity, x: $focalPoint?->x, y: $focalPoint?->y);
|
||||
}
|
||||
|
||||
if ($opacity !== 1.0) {
|
||||
@@ -351,4 +379,18 @@ class Get extends Action
|
||||
|
||||
unset($image);
|
||||
}
|
||||
|
||||
private function getAutogravityRotation(string $source): int
|
||||
{
|
||||
$metadata = new \Imagick();
|
||||
$metadata->pingImageBlob($source);
|
||||
$orientation = $metadata->getImageProperties()['exif:Orientation'] ?? null;
|
||||
|
||||
return match ($orientation) {
|
||||
'3' => 180,
|
||||
'6' => 90,
|
||||
'8' => -90,
|
||||
default => 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Tests\E2E\Services\Storage;
|
||||
|
||||
use Appwrite\Extend\Exception;
|
||||
use CURLFile;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\E2E\Client;
|
||||
@@ -55,6 +56,85 @@ final class StorageCustomServerTest extends Scope
|
||||
return self::$cachedBucket[$cacheKey];
|
||||
}
|
||||
|
||||
public function testGetFilePreviewWithAutomaticGravity(): void
|
||||
{
|
||||
$bucketId = $this->setupBucket()['bucketId'];
|
||||
$source = \tempnam(\sys_get_temp_dir(), 'appwrite-autogravity-');
|
||||
if ($source === false) {
|
||||
$this->fail('Failed to create a temporary image');
|
||||
}
|
||||
$subject = new \Imagick(__DIR__ . '/../../../resources/disk-a/kitten-1.jpg');
|
||||
$subject->resizeImage(900, 0, \Imagick::FILTER_LANCZOS, 1);
|
||||
$canvas = new \Imagick();
|
||||
$canvas->newImage(2400, 1920, 'white', 'png');
|
||||
$canvas->compositeImage($subject, \Imagick::COMPOSITE_OVER, 1450, 0);
|
||||
$canvas->writeImage($source);
|
||||
|
||||
$file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge([
|
||||
'content-type' => 'multipart/form-data',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders()), [
|
||||
'fileId' => ID::unique(),
|
||||
'file' => new CURLFile($source, 'image/png', 'autogravity.png'),
|
||||
]);
|
||||
\unlink($source);
|
||||
|
||||
$this->assertEquals(201, $file['headers']['status-code']);
|
||||
|
||||
$headers = array_merge([
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
], $this->getHeaders());
|
||||
$params = [
|
||||
'width' => 120,
|
||||
'height' => 320,
|
||||
'gravity' => 'auto',
|
||||
'output' => 'png',
|
||||
];
|
||||
$path = '/storage/buckets/' . $bucketId . '/files/' . $file['body']['$id'] . '/preview';
|
||||
|
||||
$preview = $this->client->call(Client::METHOD_GET, $path, $headers, $params);
|
||||
|
||||
if (System::getEnv('_APP_AUTOGRAVITY_HOST', '') === '') {
|
||||
$this->assertEquals(400, $preview['headers']['status-code']);
|
||||
$this->assertSame(Exception::GENERAL_ARGUMENT_INVALID, $preview['body']['type']);
|
||||
$this->assertSame(
|
||||
'Autogravity needs to be configured with _APP_AUTOGRAVITY_HOST to use automatic gravity',
|
||||
$preview['body']['message']
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->assertEquals(200, $preview['headers']['status-code']);
|
||||
$this->assertEquals('image/png', $preview['headers']['content-type']);
|
||||
$this->assertEquals('miss', $preview['headers']['x-appwrite-cache']);
|
||||
$this->assertNotEmpty($preview['body']);
|
||||
|
||||
$image = new \Imagick();
|
||||
$image->readImageBlob($preview['body']);
|
||||
$this->assertSame(120, $image->getImageWidth());
|
||||
$this->assertSame(320, $image->getImageHeight());
|
||||
|
||||
$center = $this->client->call(Client::METHOD_GET, $path, $headers, [...$params, 'gravity' => 'center']);
|
||||
$centerImage = new \Imagick();
|
||||
$centerImage->readImageBlob($center['body']);
|
||||
$this->assertNotSame($centerImage->getImageSignature(), $image->getImageSignature());
|
||||
|
||||
$cached = [];
|
||||
$this->assertEventually(function () use (&$cached, $path, $headers, $params, $image): void {
|
||||
$cached = $this->client->call(Client::METHOD_GET, $path, $headers, $params);
|
||||
|
||||
$this->assertSame('hit', $cached['headers']['x-appwrite-cache']);
|
||||
$cachedImage = new \Imagick();
|
||||
$cachedImage->readImageBlob($cached['body']);
|
||||
$this->assertSame($image->getImageSignature(), $cachedImage->getImageSignature());
|
||||
});
|
||||
|
||||
$this->assertEquals(200, $cached['headers']['status-code']);
|
||||
$this->assertEquals('image/png', $cached['headers']['content-type']);
|
||||
}
|
||||
|
||||
public function testCreateBucket(): void
|
||||
{
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Autogravity;
|
||||
|
||||
use Appwrite\Autogravity\Client;
|
||||
use Appwrite\Autogravity\Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Utopia\Psr7\Response;
|
||||
use Utopia\Psr7\Stream;
|
||||
|
||||
final class ClientTest extends TestCase
|
||||
{
|
||||
private ?RequestInterface $request = null;
|
||||
|
||||
private function client(ResponseInterface $response): Client
|
||||
{
|
||||
return new Client(new class ($response, $this) implements ClientInterface {
|
||||
public function __construct(private ResponseInterface $response, private ClientTest $test)
|
||||
{
|
||||
}
|
||||
|
||||
public function sendRequest(RequestInterface $request): ResponseInterface
|
||||
{
|
||||
$this->test->setRequest($request);
|
||||
return $this->response;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function setRequest(RequestInterface $request): void
|
||||
{
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function testAnalyze(): void
|
||||
{
|
||||
$response = (new Response(200, body: new Stream('{"gravity":{"x":0.7,"y":0.4},"confidence":0.9}')))
|
||||
->withHeader('Content-Type', 'application/json');
|
||||
|
||||
$gravity = $this->client($response)->analyze('image-bytes');
|
||||
|
||||
$this->assertEqualsWithDelta(0.7, $gravity->x, PHP_FLOAT_EPSILON);
|
||||
$this->assertEqualsWithDelta(0.4, $gravity->y, PHP_FLOAT_EPSILON);
|
||||
$this->assertInstanceOf(RequestInterface::class, $this->request);
|
||||
$this->assertSame('POST', $this->request->getMethod());
|
||||
$this->assertSame('analyze', (string) $this->request->getUri());
|
||||
$this->assertSame('application/octet-stream', $this->request->getHeaderLine('Content-Type'));
|
||||
$this->assertSame('image-bytes', (string) $this->request->getBody());
|
||||
}
|
||||
|
||||
public function testAnalyzeError(): void
|
||||
{
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage('invalid image');
|
||||
$this->expectExceptionCode(400);
|
||||
|
||||
$this->client(new Response(400, body: new Stream('{"error":"invalid image"}')))->analyze('invalid');
|
||||
}
|
||||
|
||||
public function testAnalyzeInvalidResponse(): void
|
||||
{
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage('Autogravity returned an invalid gravity');
|
||||
|
||||
$this->client(new Response(200, body: new Stream('{"gravity":{"x":2,"y":0.5}}')))->analyze('image');
|
||||
}
|
||||
|
||||
public function testAnalyzeErrorWithoutBody(): void
|
||||
{
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage('Autogravity failed with status 502');
|
||||
|
||||
$this->client(new Response(502, body: new Stream('<html>')))->analyze('image');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Autogravity;
|
||||
|
||||
use Appwrite\Autogravity\Client;
|
||||
use Appwrite\Autogravity\Detector;
|
||||
use Appwrite\Autogravity\Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Utopia\Cache\Adapter\Memory;
|
||||
use Utopia\Cache\Cache;
|
||||
use Utopia\Psr7\Response;
|
||||
use Utopia\Psr7\Stream;
|
||||
|
||||
final class DetectorTest extends TestCase
|
||||
{
|
||||
public function testRejectsDetectionWhenDisabled(): void
|
||||
{
|
||||
$detector = new Detector(null, new Cache(new Memory()));
|
||||
|
||||
$this->assertFalse($detector->isEnabled());
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage('Autogravity needs to be configured with _APP_AUTOGRAVITY_HOST to use automatic gravity');
|
||||
|
||||
$detector->get('source-image');
|
||||
}
|
||||
|
||||
public function testCachesDetectionBySource(): void
|
||||
{
|
||||
$http = new CountingClient(new Response(
|
||||
200,
|
||||
body: new Stream('{"gravity":{"x":0.7,"y":0.4},"confidence":0.9}')
|
||||
));
|
||||
$detector = new Detector(new Client($http), new Cache(new Memory()));
|
||||
|
||||
$this->assertTrue($detector->isEnabled());
|
||||
|
||||
$first = $detector->get('source-image');
|
||||
$second = $detector->get('source-image');
|
||||
|
||||
$this->assertEqualsWithDelta(0.7, $first->x, PHP_FLOAT_EPSILON);
|
||||
$this->assertEqualsWithDelta(0.4, $first->y, PHP_FLOAT_EPSILON);
|
||||
$this->assertSame($first->getArrayCopy(), $second->getArrayCopy());
|
||||
$this->assertSame(1, $http->requests);
|
||||
$this->assertSame('source-image', $http->lastBody);
|
||||
}
|
||||
|
||||
public function testDifferentSourcesAreDetectedSeparately(): void
|
||||
{
|
||||
$http = new CountingClient(new Response(
|
||||
200,
|
||||
body: new Stream('{"gravity":{"x":0.5,"y":0.5},"confidence":0.9}')
|
||||
));
|
||||
$detector = new Detector(new Client($http), new Cache(new Memory()));
|
||||
|
||||
$detector->get('first-source');
|
||||
$detector->get('second-source');
|
||||
|
||||
$this->assertSame(2, $http->requests);
|
||||
}
|
||||
}
|
||||
|
||||
final class CountingClient implements ClientInterface
|
||||
{
|
||||
public int $requests = 0;
|
||||
|
||||
public string $lastBody = '';
|
||||
|
||||
public function __construct(private readonly ResponseInterface $response)
|
||||
{
|
||||
}
|
||||
|
||||
public function sendRequest(RequestInterface $request): ResponseInterface
|
||||
{
|
||||
$this->requests++;
|
||||
$this->lastBody = (string) $request->getBody();
|
||||
|
||||
return $this->response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Autogravity;
|
||||
|
||||
use Appwrite\Autogravity\Exception;
|
||||
use Appwrite\Autogravity\Gravity;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class GravityTest extends TestCase
|
||||
{
|
||||
public function testRejectsInvalidCoordinates(): void
|
||||
{
|
||||
$this->expectException(Exception::class);
|
||||
$this->expectExceptionMessage('Autogravity returned an invalid gravity');
|
||||
|
||||
new Gravity(1.1, 0.5);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Iterator<string, array{int, float, float}>
|
||||
*/
|
||||
public static function rotationProvider(): \Iterator
|
||||
{
|
||||
yield 'clockwise' => [90, 0.8, 0.75];
|
||||
yield 'upside down' => [180, 0.75, 0.2];
|
||||
yield 'counterclockwise' => [-90, 0.2, 0.25];
|
||||
}
|
||||
|
||||
#[DataProvider('rotationProvider')]
|
||||
public function testUnrotate(int $rotation, float $expectedX, float $expectedY): void
|
||||
{
|
||||
$gravity = (new Gravity(0.25, 0.8))->unrotate($rotation);
|
||||
|
||||
$this->assertEqualsWithDelta($expectedX, $gravity->x, 0.000001);
|
||||
$this->assertEqualsWithDelta($expectedY, $gravity->y, 0.000001);
|
||||
}
|
||||
|
||||
public function testGetArrayCopy(): void
|
||||
{
|
||||
$this->assertSame(['x' => 0.2, 'y' => 0.8], (new Gravity(0.2, 0.8))->getArrayCopy());
|
||||
}
|
||||
}
|
||||
@@ -68,6 +68,8 @@ final class GeneratorTest extends TestCase
|
||||
$this->assertArrayHasKey('appwrite-task-scheduler', $compose['services']);
|
||||
$this->assertArrayHasKey('appwrite-task-interval', $compose['services']);
|
||||
$this->assertArrayHasKey('appwrite-embedding', $compose['services']);
|
||||
$this->assertArrayHasKey('appwrite-autogravity', $compose['services']);
|
||||
$this->assertSame('ghcr.io/appwrite/autogravity:0.0.6', $compose['services']['appwrite-autogravity']['image']);
|
||||
$this->assertArrayNotHasKey('profiles', $compose['services']['appwrite-worker']);
|
||||
$this->assertArrayNotHasKey('profiles', $compose['services']['appwrite-task-scheduler']);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user