Merge pull request #12843 from appwrite/feat/screenshots-worker-utopia-client

Migrate screenshots worker to utopia-php/client
This commit is contained in:
Luke B. Silver authored and GitHub committed 2026-07-10 21:32:24 +01:00
commit f962b03a6e
5 files changed
+183 -66

No files matched your search

+9
View File
@@ -18,6 +18,7 @@ use Appwrite\Event\Publisher\Screenshot as ScreenshotPublisher;
use Appwrite\Event\Publisher\StatsResources as StatsResourcesPublisher;
use Appwrite\Event\Publisher\Usage as UsagePublisher;
use Appwrite\Platform\Modules\Storage\Config\StorageCacheControl;
use Appwrite\Screenshots\Client as ScreenshotsClient;
use Executor\Executor;
use OpenRuntimes\Orchestrator\Jobs;
use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimitRedis;
@@ -86,6 +87,14 @@ $container->set('jobs', function () {
return new Jobs($client);
}, []);
$container->set('screenshots', function () {
$client = (new Client(new CurlAdapter()))
->withBaseUri(System::getEnv('_APP_BROWSER_HOST', 'http://appwrite-browser:3000/v1'))
->withTimeout((int) System::getEnv('_APP_SITES_TIMEOUT', 30));
return new ScreenshotsClient($client);
}, []);
$container->set('telemetry', fn () => new NoTelemetry(), []);
$container->set('authorization', fn () => new Authorization(), []);
@@ -7,6 +7,7 @@ use Appwrite\Event\Message\Screenshot;
use Appwrite\Event\Realtime;
use Appwrite\Permission;
use Appwrite\Role;
use Appwrite\Screenshots\Client;
use Exception;
use Utopia\Compression\Compression;
use Utopia\Config\Config;
@@ -14,7 +15,6 @@ use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Query;
use Utopia\Fetch\Client as FetchClient;
use Utopia\Platform\Action;
use Utopia\Queue\Message;
use Utopia\Span\Span;
@@ -23,8 +23,6 @@ use Utopia\System\System;
use Utopia\Telemetry\Adapter as Telemetry;
use Utopia\Telemetry\Counter;
use function Swoole\Coroutine\batch;
class Screenshots extends Action
{
public static function getName(): string
@@ -47,6 +45,7 @@ class Screenshots extends Action
->inject('project')
->inject('deviceForFiles')
->inject('telemetry')
->inject('screenshots')
->callback($this->action(...));
}
@@ -57,7 +56,8 @@ class Screenshots extends Action
Database $dbForProject,
Document $project,
Device $deviceForFiles,
Telemetry $telemetry
Telemetry $telemetry,
Client $screenshots
): void {
Span::add('project.id', $project->getId());
@@ -111,8 +111,6 @@ class Screenshots extends Action
throw new \Exception("Rule for deployment not found");
}
$timeout = \intval($site->getAttribute('timeout', '15')) * 1000;
$bucket = $dbForPlatform->getDocument('buckets', 'screenshots');
if ($bucket->isEmpty()) {
@@ -125,19 +123,6 @@ class Screenshots extends Action
$routerHost = "$protocol://{$rule->getAttribute('domain')}";
}
$configs = [
'screenshotLight' => [
'headers' => [ 'x-appwrite-hostname' => $rule->getAttribute('domain') ],
'url' => $routerHost . '/?appwrite-preview=1&appwrite-theme=light',
'theme' => 'light'
],
'screenshotDark' => [
'headers' => [ 'x-appwrite-hostname' => $rule->getAttribute('domain') ],
'url' => $routerHost . '/?appwrite-preview=1&appwrite-theme=dark',
'theme' => 'dark'
],
];
$jwtObj = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', 900, 0);
$apiKey = $jwtObj->encode([
'hostnameOverride' => true,
@@ -161,61 +146,30 @@ class Screenshots extends Action
'deploymentStatusIgnored' => true
]);
$screenshotError = null;
$screenshots = batch(\array_map(function ($key) use ($configs, $apiKey, $site, $timeout, &$screenshotError) {
return function () use ($key, $configs, $apiKey, $site, $timeout, &$screenshotError) {
try {
$config = $configs[$key];
$headers = [
'x-appwrite-hostname' => $rule->getAttribute('domain'),
'x-appwrite-key' => API_KEY_EPHEMERAL . '_' . $apiKey,
];
$config['headers'] = \array_merge($config['headers'], [
'x-appwrite-key' => API_KEY_EPHEMERAL . '_' . $apiKey
]);
$config['sleep'] = 3000;
$framework = Config::getParam('frameworks', [])[$site->getAttribute('framework', '')] ?? null;
$sleep = $framework['screenshotSleep'] ?? 3000;
$frameworks = Config::getParam('frameworks', []);
$framework = $frameworks[$site->getAttribute('framework', '')] ?? null;
if (!is_null($framework)) {
$config['sleep'] = $framework['screenshotSleep'];
}
$browserEndpoint = System::getEnv('_APP_BROWSER_HOST', 'http://appwrite-browser:3000/v1');
$client = new FetchClient();
$client->setTimeout($timeout);
$client->addHeader('content-type', FetchClient::CONTENT_TYPE_APPLICATION_JSON);
$fetchResponse = $client->fetch(
url: $browserEndpoint . '/screenshots',
method: 'POST',
body: $config
);
if ($fetchResponse->getStatusCode() >= 400) {
throw new \Exception($fetchResponse->getBody());
}
$screenshot = $fetchResponse->getBody();
return ['key' => $key, 'screenshot' => $screenshot];
} catch (\Throwable $th) {
$screenshotError = $th->getMessage();
return;
}
};
}, \array_keys($configs)));
if (!\is_null($screenshotError)) {
throw new \Exception($screenshotError);
$captures = [];
foreach (['screenshotLight' => 'light', 'screenshotDark' => 'dark'] as $key => $theme) {
$captures[$key] = $screenshots->create(
url: $routerHost . '/?appwrite-preview=1&appwrite-theme=' . $theme,
theme: $theme,
headers: $headers,
sleep: $sleep,
);
}
Span::add('screenshot.count', \count($screenshots));
Span::add('screenshot.count', \count($captures));
$mimeType = "image/png";
$updates = new Document([]);
foreach ($screenshots as $data) {
$key = $data['key'];
$screenshot = $data['screenshot'];
foreach ($captures as $key => $screenshot) {
$fileId = ID::unique();
$fileName = $fileId . '.png';
$path = $deviceForFiles->getPath($fileName);
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace Appwrite\Screenshots;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Utopia\Psr7\Method;
use Utopia\Psr7\Request\Factory;
class Client
{
private Factory $factory;
/**
* @param ClientInterface $client Configured with the browser service base URI.
*/
public function __construct(private readonly ClientInterface $client)
{
$this->factory = new Factory();
}
/**
* Capture a screenshot of a page and return the PNG bytes.
*
* @param array<string, string> $headers
* @throws Exception on an error response
* @throws ClientExceptionInterface on transport failure
*/
public function create(string $url, string $theme, array $headers = [], int $sleep = 3000): string
{
$response = $this->client->sendRequest($this->factory->json(Method::POST, 'screenshots', [
'url' => $url,
'theme' => $theme,
'headers' => $headers,
'sleep' => $sleep,
]));
$status = $response->getStatusCode();
$body = (string)$response->getBody();
if ($status >= 400) {
$error = \json_decode($body, true)['error'] ?? null;
throw new Exception(\is_string($error) ? $error : 'Screenshot failed with status ' . $status, $status);
}
$contentType = $response->getHeaderLine('Content-Type');
if (!\str_starts_with($contentType, 'image/')) {
throw new Exception('Expected an image response, got ' . ($contentType === '' ? 'no content type' : $contentType), $status);
}
return $body;
}
}
+7
View File
@@ -0,0 +1,7 @@
<?php
namespace Appwrite\Screenshots;
class Exception extends \Exception
{
}
+94
View File
@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Screenshots;
use Appwrite\Screenshots\Client;
use Appwrite\Screenshots\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
{
$test = $this;
return new Client(new class ($response, $test) 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 testCreate(): void
{
$response = (new Response(200, body: new Stream('png-bytes')))
->withHeader('Content-Type', 'image/png');
$screenshot = $this->client($response)->create(
url: 'http://appwrite/',
theme: 'dark',
headers: ['x-appwrite-hostname' => 'example.com'],
sleep: 500,
);
$this->assertSame('png-bytes', $screenshot);
$this->assertSame('POST', $this->request->getMethod());
$this->assertSame('screenshots', (string)$this->request->getUri());
$this->assertEquals([
'url' => 'http://appwrite/',
'theme' => 'dark',
'headers' => ['x-appwrite-hostname' => 'example.com'],
'sleep' => 500,
], \json_decode((string)$this->request->getBody(), true));
}
public function testCreateError(): void
{
$response = (new Response(400, body: new Stream('{"error":"Timeout exceeded"}')))
->withHeader('Content-Type', 'application/json');
$this->expectException(Exception::class);
$this->expectExceptionMessage('Timeout exceeded');
$this->expectExceptionCode(400);
$this->client($response)->create('http://appwrite/', 'light');
}
public function testCreateErrorWithoutBody(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Screenshot failed with status 502');
$this->client(new Response(502))->create('http://appwrite/', 'light');
}
public function testCreateUnexpectedContentType(): void
{
$response = (new Response(200, body: new Stream('<html>')))
->withHeader('Content-Type', 'text/html');
$this->expectException(Exception::class);
$this->expectExceptionMessage('Expected an image response, got text/html');
$this->client($response)->create('http://appwrite/', 'light');
}
}