From 7aaaf4df4f03bfe750b64a530a734225ca894770 Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Wed, 1 Apr 2026 15:07:02 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20revert=20Facebook=20Reel/Story=20to=20UR?= =?UTF-8?q?L=20pull=20(video=5Ffile=5Fchunk=20as=20URL)=20=E2=80=94=20bina?= =?UTF-8?q?ry=20upload=20was=20breaking=20the=20transfer=20phase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/Social/FacebookPublisher.php | 73 +++++-------------- .../views/auth/social-callback.blade.php | 2 +- 2 files changed, 19 insertions(+), 56 deletions(-) diff --git a/app/Services/Social/FacebookPublisher.php b/app/Services/Social/FacebookPublisher.php index 68f501b9..6efe0213 100644 --- a/app/Services/Social/FacebookPublisher.php +++ b/app/Services/Social/FacebookPublisher.php @@ -9,7 +9,6 @@ use App\Models\PostPlatform; use App\Services\Social\Concerns\HasSocialHttpClient; use Illuminate\Http\Client\Response; -use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; class FacebookPublisher @@ -225,34 +224,16 @@ private function publishReel(string $pageId, string $accessToken, ?string $conte $data = $response->json(); $videoId = data_get($data, 'video_id'); - // Upload the video file - $tempFile = tempnam(sys_get_temp_dir(), 'fb_reel_'); - try { - $downloadResponse = Http::withOptions(['sink' => $tempFile])->timeout(600)->get($media->url); + // Upload the video file (Facebook accepts URL in video_file_chunk) + $uploadResponse = $this->socialHttp()->post("{$this->baseUrl}/{$videoId}", [ + 'upload_phase' => 'transfer', + 'video_file_chunk' => $media->url, + 'access_token' => $accessToken, + ]); - if ($downloadResponse->failed()) { - throw new \Exception('Failed to download media: HTTP '.$downloadResponse->status()); - } - - $stream = fopen($tempFile, 'r'); - - $uploadResponse = Http::withToken($accessToken) - ->attach('video_file_chunk', $stream, 'video.mp4') - ->post("{$this->baseUrl}/{$videoId}", [ - 'upload_phase' => 'transfer', - 'access_token' => $accessToken, - ]); - - if (is_resource($stream)) { - fclose($stream); - } - - if ($uploadResponse->failed()) { - Log::error('Facebook reel upload transfer failed', ['body' => $this->redactResponseBody($uploadResponse->body())]); - $this->handleApiError($uploadResponse); - } - } finally { - @unlink($tempFile); + if ($uploadResponse->failed()) { + Log::error('Facebook reel upload transfer failed', ['body' => $this->redactResponseBody($uploadResponse->body())]); + $this->handleApiError($uploadResponse); } // Finish and publish the reel @@ -301,34 +282,16 @@ private function publishStory(string $pageId, string $accessToken, $media): arra throw new \Exception('Facebook story upload failed: no video ID returned'); } - // Transfer the video - $tempFile = tempnam(sys_get_temp_dir(), 'fb_story_'); - try { - $downloadResponse = Http::withOptions(['sink' => $tempFile])->timeout(600)->get($media->url); + // Transfer the video (Facebook accepts URL in video_file_chunk) + $transferResponse = $this->socialHttp()->post("{$this->baseUrl}/{$videoId}", [ + 'upload_phase' => 'transfer', + 'video_file_chunk' => $media->url, + 'access_token' => $accessToken, + ]); - if ($downloadResponse->failed()) { - throw new \Exception('Failed to download media: HTTP '.$downloadResponse->status()); - } - - $stream = fopen($tempFile, 'r'); - - $transferResponse = Http::withToken($accessToken) - ->attach('video_file_chunk', $stream, 'video.mp4') - ->post("{$this->baseUrl}/{$videoId}", [ - 'upload_phase' => 'transfer', - 'access_token' => $accessToken, - ]); - - if (is_resource($stream)) { - fclose($stream); - } - - if ($transferResponse->failed()) { - Log::error('Facebook video story transfer failed', ['body' => $this->redactResponseBody($transferResponse->body())]); - $this->handleApiError($transferResponse); - } - } finally { - @unlink($tempFile); + if ($transferResponse->failed()) { + Log::error('Facebook video story transfer failed', ['body' => $this->redactResponseBody($transferResponse->body())]); + $this->handleApiError($transferResponse); } // Finish the story diff --git a/resources/views/auth/social-callback.blade.php b/resources/views/auth/social-callback.blade.php index eb7b68ba..13bdb753 100644 --- a/resources/views/auth/social-callback.blade.php +++ b/resources/views/auth/social-callback.blade.php @@ -75,7 +75,7 @@ function closeWindow() { } // Close after a short delay - setTimeout(closeWindow, 6000); + setTimeout(closeWindow, 2000); // If still open after 2 seconds, show manual close message setTimeout(function() {