|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Actions; |
| 6 | + |
| 7 | +use App\Models\Package; |
| 8 | +use Illuminate\Support\Facades\Log; |
| 9 | +use Illuminate\Support\Str; |
| 10 | +use Spatie\Browsershot\Browsershot; |
| 11 | + |
| 12 | +class GeneratePackageOgImageAction |
| 13 | +{ |
| 14 | + public function handle(Package $package): void |
| 15 | + { |
| 16 | + try { |
| 17 | + $tempUrl = route('og-images.package', ['package' => $package->id]); |
| 18 | + |
| 19 | + // Target file |
| 20 | + $storagePath = storage_path('app/public/package-og-images'); |
| 21 | + $this->ensureDir($storagePath); |
| 22 | + |
| 23 | + $filename = Str::slug($package->name) . '-' . Str::random(8) . '.jpg'; |
| 24 | + $fullPath = $storagePath . '/' . $filename; |
| 25 | + |
| 26 | + // Binaries |
| 27 | + $nodePath = $this->which('node') ?? '/usr/bin/node'; |
| 28 | + $npmPath = $this->which('npm') ?? '/usr/bin/npm'; |
| 29 | + |
| 30 | + // Writable Chrome profile/cache dir |
| 31 | + $userDataDir = rtrim((string) env('BROWSERSHOT_USER_DATA_DIR', '/var/www/browsershot-cache'), '/'); |
| 32 | + $this->ensureDir($userDataDir); |
| 33 | + |
| 34 | + // Ensure the env seen by the child process points to our writable dir |
| 35 | + foreach (['HOME', 'XDG_CONFIG_HOME', 'XDG_CACHE_HOME'] as $var) { |
| 36 | + putenv($var . '=' . $userDataDir); |
| 37 | + $_ENV[$var] = $userDataDir; |
| 38 | + $_SERVER[$var] = $userDataDir; |
| 39 | + } |
| 40 | + |
| 41 | + // Prefer explicit path via env; otherwise auto-detect |
| 42 | + $chromePath = env('CHROME_PATH') ?: $this->detectChromePath(); |
| 43 | + |
| 44 | + // IMPORTANT: do NOT prefix with `--` here; Browsershot will add them |
| 45 | + $chromiumArgs = [ |
| 46 | + 'headless=new', |
| 47 | + 'no-sandbox', |
| 48 | + 'disable-setuid-sandbox', |
| 49 | + 'disable-dev-shm-usage', |
| 50 | + 'disable-gpu', |
| 51 | + 'no-zygote', |
| 52 | + 'single-process', |
| 53 | + 'hide-scrollbars', |
| 54 | + "user-data-dir={$userDataDir}", |
| 55 | + 'no-first-run', |
| 56 | + 'no-default-browser-check', |
| 57 | + 'disable-crash-reporter', |
| 58 | + "data-path={$userDataDir}/data", |
| 59 | + "disk-cache-dir={$userDataDir}/cache", |
| 60 | + ]; |
| 61 | + |
| 62 | + $browserShot = Browsershot::url($tempUrl) |
| 63 | + ->setNodeBinary($nodePath) |
| 64 | + ->setNpmBinary($npmPath) |
| 65 | + ->windowSize(1200, 630) |
| 66 | + ->waitUntilNetworkIdle() |
| 67 | + ->setScreenshotType('jpeg', 90) |
| 68 | + ->addChromiumArguments($chromiumArgs); |
| 69 | + |
| 70 | + if ($chromePath) { |
| 71 | + $browserShot->setChromePath($chromePath); |
| 72 | + } |
| 73 | + |
| 74 | + $browserShot->save($fullPath); |
| 75 | + |
| 76 | + if (is_file($fullPath)) { |
| 77 | + $package->addMedia($fullPath) |
| 78 | + ->usingName('og-image') |
| 79 | + ->usingFileName($filename) |
| 80 | + ->toMediaCollection('og-images', 'package-og-images'); |
| 81 | + |
| 82 | + Log::info('OG Image Generated: ' . $package->getFirstMediaUrl('og-images')); |
| 83 | + } else { |
| 84 | + Log::error('OG image file missing after save', ['path' => $fullPath]); |
| 85 | + } |
| 86 | + } catch (\Throwable $e) { |
| 87 | + Log::error('OG Image Generation Error: ' . $e->getMessage(), ['exception' => $e]); |
| 88 | + throw $e; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private function ensureDir(string $dir): void |
| 93 | + { |
| 94 | + if (! is_dir($dir)) { |
| 95 | + @mkdir($dir, 0755, true); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private function which(string $bin): ?string |
| 100 | + { |
| 101 | + $path = @exec(sprintf('command -v %s', escapeshellarg($bin))); |
| 102 | + return $path ?: null; |
| 103 | + } |
| 104 | + |
| 105 | + private function detectChromePath(): ?string |
| 106 | + { |
| 107 | + $candidates = [ |
| 108 | + '/usr/bin/google-chrome', |
| 109 | + '/usr/bin/google-chrome-stable', |
| 110 | + '/usr/local/bin/google-chrome', |
| 111 | + '/usr/bin/chromium', |
| 112 | + '/usr/bin/chromium-browser', |
| 113 | + ]; |
| 114 | + |
| 115 | + foreach ($candidates as $path) { |
| 116 | + if (is_file($path) && is_executable($path)) { |
| 117 | + return $path; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return null; |
| 122 | + } |
| 123 | +} |
0 commit comments