diff --git a/app/init/resources.php b/app/init/resources.php index d559213f5e..c41aa4489f 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -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(), []); diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php index 35cbe63a15..b61ab8fa9d 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; 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); diff --git a/src/Appwrite/Screenshots/Client.php b/src/Appwrite/Screenshots/Client.php new file mode 100644 index 0000000000..096d737393 --- /dev/null +++ b/src/Appwrite/Screenshots/Client.php @@ -0,0 +1,53 @@ +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, + ])); + + $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; + } +} diff --git a/src/Appwrite/Screenshots/Exception.php b/src/Appwrite/Screenshots/Exception.php new file mode 100644 index 0000000000..4f6b17a39b --- /dev/null +++ b/src/Appwrite/Screenshots/Exception.php @@ -0,0 +1,7 @@ +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(''))) + ->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'); + } +}