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 6f09db1bec..4898ff7c88 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Screenshots.php @@ -9,8 +9,6 @@ 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; use Utopia\Compression\Compression; use Utopia\Config\Config; use Utopia\Database\Database; @@ -49,6 +47,7 @@ class Screenshots extends Action ->inject('project') ->inject('deviceForFiles') ->inject('telemetry') + ->inject('screenshots') ->callback($this->action(...)); } @@ -59,7 +58,8 @@ class Screenshots extends Action Database $dbForProject, Document $project, Device $deviceForFiles, - Telemetry $telemetry + Telemetry $telemetry, + ScreenshotsClient $screenshots ): void { Span::add('project.id', $project->getId()); @@ -113,8 +113,6 @@ class Screenshots extends Action throw new \Exception("Rule for deployment not found"); } - $timeout = \intval($site->getAttribute('timeout', '15')); - $bucket = $dbForPlatform->getDocument('buckets', 'screenshots'); if ($bucket->isEmpty()) { @@ -150,13 +148,6 @@ class Screenshots extends Action 'deploymentStatusIgnored' => true ]); - $browserEndpoint = System::getEnv('_APP_BROWSER_HOST', 'http://appwrite-browser:3000/v1'); - $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, @@ -168,11 +159,11 @@ class Screenshots extends Action $themes = ['screenshotLight' => 'light', 'screenshotDark' => 'dark']; $screenshotError = null; - $screenshots = batch(\array_map(function ($key) use ($themes, $client, $routerHost, $headers, $sleep, &$screenshotError) { - return function () use ($key, $themes, $client, $routerHost, $headers, $sleep, &$screenshotError) { + $captures = batch(\array_map(function ($key) use ($themes, $screenshots, $routerHost, $headers, $sleep, &$screenshotError) { + return function () use ($key, $themes, $screenshots, $routerHost, $headers, $sleep, &$screenshotError) { try { $theme = $themes[$key]; - $screenshot = $client->create( + $screenshot = $screenshots->create( url: $routerHost . '/?appwrite-preview=1&appwrite-theme=' . $theme, theme: $theme, headers: $headers, @@ -191,12 +182,12 @@ class Screenshots extends Action throw new \Exception($screenshotError); } - Span::add('screenshot.count', \count($screenshots)); + Span::add('screenshot.count', \count($captures)); $mimeType = "image/png"; $updates = new Document([]); - foreach ($screenshots as $data) { + foreach ($captures as $data) { $key = $data['key']; $screenshot = $data['screenshot'];