From 94f277ef75e4bba7aaac06fb30bc14afbe7e41fe Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 10 Jul 2026 20:13:04 +0100 Subject: [PATCH] Extract browser screenshot HTTP logic into Screenshots\Client The client takes a PSR-18 ClientInterface configured with the browser service base URI, so the worker only decides what to capture (theme, headers, sleep) and the transport is swappable in tests. Co-Authored-By: Claude Fable 5 --- .../Modules/Functions/Workers/Screenshots.php | 66 ++++++++----------- src/Appwrite/Screenshots/Client.php | 44 +++++++++++++ 2 files changed, 70 insertions(+), 40 deletions(-) create mode 100644 src/Appwrite/Screenshots/Client.php diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php index aeb3259645..6f09db1bec 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php @@ -7,6 +7,7 @@ use Appwrite\Event\Message\Screenshot; use Appwrite\Event\Realtime; use Appwrite\Permission; use Appwrite\Role; +use Appwrite\Screenshots\Client as ScreenshotsClient; use Exception; use Utopia\Client; use Utopia\Client\Adapter\Curl\Client as CurlAdapter; @@ -17,8 +18,6 @@ use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Query; use Utopia\Platform\Action; -use Utopia\Psr7\Method; -use Utopia\Psr7\Request\Factory as RequestFactory; use Utopia\Queue\Message; use Utopia\Span\Span; use Utopia\Storage\Device; @@ -128,19 +127,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, @@ -165,33 +151,33 @@ class Screenshots extends Action ]); $browserEndpoint = System::getEnv('_APP_BROWSER_HOST', 'http://appwrite-browser:3000/v1'); - $client = (new Client(new CurlAdapter()))->withTimeout($timeout); - $factory = new RequestFactory(); + $client = new ScreenshotsClient( + (new Client(new CurlAdapter())) + ->withBaseUri($browserEndpoint) + ->withTimeout($timeout) + ); + + $headers = [ + 'x-appwrite-hostname' => $rule->getAttribute('domain'), + 'x-appwrite-key' => API_KEY_EPHEMERAL . '_' . $apiKey, + ]; + + $framework = Config::getParam('frameworks', [])[$site->getAttribute('framework', '')] ?? null; + $sleep = $framework['screenshotSleep'] ?? 3000; + + $themes = ['screenshotLight' => 'light', 'screenshotDark' => 'dark']; $screenshotError = null; - $screenshots = batch(\array_map(function ($key) use ($configs, $apiKey, $site, $browserEndpoint, $client, $factory, &$screenshotError) { - return function () use ($key, $configs, $apiKey, $site, $browserEndpoint, $client, $factory, &$screenshotError) { + $screenshots = batch(\array_map(function ($key) use ($themes, $client, $routerHost, $headers, $sleep, &$screenshotError) { + return function () use ($key, $themes, $client, $routerHost, $headers, $sleep, &$screenshotError) { try { - $config = $configs[$key]; - - $config['headers'] = \array_merge($config['headers'], [ - 'x-appwrite-key' => API_KEY_EPHEMERAL . '_' . $apiKey - ]); - $config['sleep'] = 3000; - - $frameworks = Config::getParam('frameworks', []); - $framework = $frameworks[$site->getAttribute('framework', '')] ?? null; - if (!is_null($framework)) { - $config['sleep'] = $framework['screenshotSleep']; - } - - $response = $client->sendRequest($factory->json(Method::POST, $browserEndpoint . '/screenshots', $config)); - - if ($response->getStatusCode() >= 400) { - throw new \Exception((string)$response->getBody()); - } - - $screenshot = (string)$response->getBody(); + $theme = $themes[$key]; + $screenshot = $client->create( + url: $routerHost . '/?appwrite-preview=1&appwrite-theme=' . $theme, + theme: $theme, + headers: $headers, + sleep: $sleep, + ); return ['key' => $key, 'screenshot' => $screenshot]; } catch (\Throwable $th) { @@ -199,7 +185,7 @@ class Screenshots extends Action return; } }; - }, \array_keys($configs))); + }, \array_keys($themes))); if (!\is_null($screenshotError)) { throw new \Exception($screenshotError); diff --git a/src/Appwrite/Screenshots/Client.php b/src/Appwrite/Screenshots/Client.php new file mode 100644 index 0000000000..ad5b78a8f6 --- /dev/null +++ b/src/Appwrite/Screenshots/Client.php @@ -0,0 +1,44 @@ +factory = new Factory(); + } + + /** + * Capture a screenshot of a page and return the PNG bytes. + * + * @param array $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, + ])); + + if ($response->getStatusCode() >= 400) { + throw new \Exception((string)$response->getBody()); + } + + return (string)$response->getBody(); + } +}