From aa6bc88335e8f4b82c005a2169e569a58c9907eb Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Tue, 31 Mar 2026 21:46:58 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20code=20review=20issues=20=E2=80=94=20Ins?= =?UTF-8?q?tagram=20arg=20count,=20LinkedIn=20file=20handle,=20TikTok=20da?= =?UTF-8?q?ta=5Fget,=20YouTube=20token=20timestamp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/Social/InstagramPublisher.php | 2 +- app/Services/Social/LinkedInPagePublisher.php | 11 ++++++----- app/Services/Social/LinkedInPublisher.php | 11 ++++++----- app/Services/Social/TikTokPublisher.php | 6 +++--- app/Services/Social/YouTubePublisher.php | 10 ++++++---- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/app/Services/Social/InstagramPublisher.php b/app/Services/Social/InstagramPublisher.php index eeac921b..e216254a 100644 --- a/app/Services/Social/InstagramPublisher.php +++ b/app/Services/Social/InstagramPublisher.php @@ -233,7 +233,7 @@ private function publishCarousel(string $instagramId, string $accessToken, ?stri Log::error('Instagram carousel container creation failed', [ 'body' => $carouselResponse->body(), ]); - $this->handleApiError($carouselResponse, 'Instagram API error'); + $this->handleApiError($carouselResponse); } $carouselId = $carouselResponse->json()['id'] ?? null; diff --git a/app/Services/Social/LinkedInPagePublisher.php b/app/Services/Social/LinkedInPagePublisher.php index a1ce5cb6..38026eb7 100644 --- a/app/Services/Social/LinkedInPagePublisher.php +++ b/app/Services/Social/LinkedInPagePublisher.php @@ -367,17 +367,16 @@ private function doUploadVideo(string $tempFile, $mediaItem, string $ownerUrn): // Step 2: Upload chunks $uploadedPartIds = []; + $handle = fopen($tempFile, 'r'); foreach ($uploadInstructions as $index => $instruction) { - $uploadUrl = $instruction['uploadUrl']; - $firstByte = $instruction['firstByte']; - $lastByte = $instruction['lastByte']; + $uploadUrl = data_get($instruction, 'uploadUrl'); + $firstByte = data_get($instruction, 'firstByte'); + $lastByte = data_get($instruction, 'lastByte'); $chunkLength = $lastByte - $firstByte + 1; - $handle = fopen($tempFile, 'r'); fseek($handle, $firstByte); $chunkData = fread($handle, $chunkLength); - fclose($handle); Log::info('LinkedIn Page uploading video chunk', [ 'index' => $index, @@ -410,6 +409,8 @@ private function doUploadVideo(string $tempFile, $mediaItem, string $ownerUrn): Log::info('LinkedIn Page video chunk uploaded', ['index' => $index, 'etag' => $etag]); } + fclose($handle); + // Step 3: Finalize upload Log::info('LinkedIn Page finalizing video upload', ['videoUrn' => $videoUrn]); diff --git a/app/Services/Social/LinkedInPublisher.php b/app/Services/Social/LinkedInPublisher.php index be659ca1..790d3630 100644 --- a/app/Services/Social/LinkedInPublisher.php +++ b/app/Services/Social/LinkedInPublisher.php @@ -348,17 +348,16 @@ private function doUploadVideo(string $tempFile, $mediaItem, string $ownerUrn): // Step 2: Upload chunks $uploadedPartIds = []; + $handle = fopen($tempFile, 'r'); foreach ($uploadInstructions as $index => $instruction) { - $uploadUrl = $instruction['uploadUrl']; - $firstByte = $instruction['firstByte']; - $lastByte = $instruction['lastByte']; + $uploadUrl = data_get($instruction, 'uploadUrl'); + $firstByte = data_get($instruction, 'firstByte'); + $lastByte = data_get($instruction, 'lastByte'); $chunkLength = $lastByte - $firstByte + 1; - $handle = fopen($tempFile, 'r'); fseek($handle, $firstByte); $chunkData = fread($handle, $chunkLength); - fclose($handle); Log::info('LinkedIn uploading video chunk', [ 'index' => $index, @@ -391,6 +390,8 @@ private function doUploadVideo(string $tempFile, $mediaItem, string $ownerUrn): Log::info('LinkedIn video chunk uploaded', ['index' => $index, 'etag' => $etag]); } + fclose($handle); + // Step 3: Finalize upload Log::info('LinkedIn finalizing video upload', ['videoUrn' => $videoUrn]); diff --git a/app/Services/Social/TikTokPublisher.php b/app/Services/Social/TikTokPublisher.php index df47c19a..06419ca9 100644 --- a/app/Services/Social/TikTokPublisher.php +++ b/app/Services/Social/TikTokPublisher.php @@ -128,7 +128,7 @@ private function publishVideo(PostPlatform $postPlatform, $media): array Log::info('TikTok video init response', ['data' => $data]); - $publishId = data_get($data, 'data')['publish_id'] ?? null; + $publishId = data_get($data, 'data.publish_id'); if (! $publishId) { throw new \Exception('TikTok did not return a publish_id'); @@ -191,7 +191,7 @@ private function publishPhotos(PostPlatform $postPlatform, $mediaCollection): ar Log::info('TikTok photo init response', ['data' => $data]); - $publishId = data_get($data, 'data')['publish_id'] ?? null; + $publishId = data_get($data, 'data.publish_id'); if (! $publishId) { throw new \Exception('TikTok did not return a publish_id'); @@ -226,7 +226,7 @@ private function waitForPublishStatus(string $publishId, int $maxAttempts = 20): } $data = $response->json(); - $status = data_get($data, 'data')['status'] ?? 'UNKNOWN'; + $status = data_get($data, 'data.status', 'UNKNOWN'); Log::info('TikTok publish status', [ 'status' => $status, diff --git a/app/Services/Social/YouTubePublisher.php b/app/Services/Social/YouTubePublisher.php index b182dae0..1c6e8b58 100644 --- a/app/Services/Social/YouTubePublisher.php +++ b/app/Services/Social/YouTubePublisher.php @@ -51,12 +51,14 @@ private function createGoogleClient(SocialAccount $account): GoogleClient $client->setClientId(config('services.google.client_id')); $client->setClientSecret(config('services.google.client_secret')); + $remainingSeconds = $account->token_expires_at + ? max(0, (int) now()->diffInSeconds($account->token_expires_at, false)) + : 3600; + $tokenData = [ 'access_token' => $account->access_token, - 'created' => $account->token_expires_at - ? $account->token_expires_at->subHour()->getTimestamp() - : time(), - 'expires_in' => 3600, + 'created' => time(), + 'expires_in' => $remainingSeconds, ]; if ($account->refresh_token) {