diff --git a/app/Exceptions/Social/SocialPublishException.php b/app/Exceptions/Social/SocialPublishException.php index 526730d9..384695e6 100644 --- a/app/Exceptions/Social/SocialPublishException.php +++ b/app/Exceptions/Social/SocialPublishException.php @@ -23,14 +23,36 @@ public function __construct( public function context(): array { return [ - 'platform' => $this->platform(), + 'platform' => static::platform(), 'category' => $this->category->value, 'platform_error_code' => $this->platformErrorCode, 'user_message' => $this->userMessage, - 'raw_response' => $this->rawResponse, + 'raw_response' => $this->redactTokens($this->rawResponse), ]; } + private function redactTokens(?string $text): ?string + { + if ($text === null) { + return null; + } + + // Redact common token patterns from API error responses + return preg_replace( + [ + '/access_token=([^&"\s]+)/', + '/"access_token"\s*:\s*"([^"]+)"/', + '/Bearer\s+\S+/', + ], + [ + 'access_token=[REDACTED]', + '"access_token":"[REDACTED]"', + 'Bearer [REDACTED]', + ], + $text + ); + } + abstract public static function fromApiResponse(mixed $response): static; abstract public function platform(): string; diff --git a/app/Services/Media/MediaOptimizer.php b/app/Services/Media/MediaOptimizer.php index d701e0b6..c5f0d1c3 100644 --- a/app/Services/Media/MediaOptimizer.php +++ b/app/Services/Media/MediaOptimizer.php @@ -5,6 +5,7 @@ namespace App\Services\Media; use App\Enums\SocialAccount\Platform; +use Illuminate\Support\Facades\Log; use Intervention\Image\Drivers\Gd\Driver; use Intervention\Image\ImageManager; @@ -24,6 +25,32 @@ public function __construct() public function optimizeImage(string $filePath, Platform $platform): string { $config = $this->getImageConfig($platform); + + // Check image dimensions to prevent GD memory overflow + $imageInfo = @getimagesize($filePath); + if ($imageInfo) { + $width = $imageInfo[0]; + $height = $imageInfo[1]; + $channels = $imageInfo['channels'] ?? 4; + $estimatedMemory = $width * $height * $channels * 1.5; // 1.5x safety margin + + // If image would use more than 256MB of RAM, skip optimization and return as-is + if ($estimatedMemory > 256 * 1024 * 1024) { + Log::warning('MediaOptimizer: Image too large for GD processing', [ + 'width' => $width, + 'height' => $height, + 'estimated_memory' => $estimatedMemory, + 'platform' => $platform->value, + ]); + + // Copy original file to temp location and return + $tempFile = tempnam(sys_get_temp_dir(), 'media_opt_'); + copy($filePath, $tempFile); + + return $tempFile; + } + } + $image = $this->manager->decodePath($filePath); $maxWidth = data_get($config, 'max_width');