From e42b866ec8e3e93b2c903a5129fa5e267cba47e0 Mon Sep 17 00:00:00 2001 From: Daniel Neto Date: Wed, 18 Dec 2024 12:16:40 -0300 Subject: [PATCH] Update for better social publish --- objects/functions.php | 98 ++++++++++++++++--- plugin/CDN/Storage/CDNStorage.php | 2 +- .../Publisher_video_publisher_logs.php | 5 +- .../Objects/SocialUploader.php | 14 +-- .../SocialMediaPublisher.php | 9 +- .../publishInstagram.json.php | 4 +- plugin/SocialMediaPublisher/script.js | 19 ++++ plugin/SocialMediaPublisher/uploadVideo.php | 2 +- 8 files changed, 123 insertions(+), 30 deletions(-) diff --git a/objects/functions.php b/objects/functions.php index cef7949f95..55d4a81286 100644 --- a/objects/functions.php +++ b/objects/functions.php @@ -333,11 +333,11 @@ function safeString($text, $strict = false, $try = 0) if (empty($try) && empty($text) && function_exists('mb_convert_encoding')) { $originalText2 = preg_replace('/[^\PC\s]/u', '', $originalText); - if(empty($originalText2)){ + if (empty($originalText2)) { $originalText2 = mb_convert_encoding($originalText, 'UTF-8', 'auto'); $originalText2 = preg_replace('/[^\PC\s]/u', '', $originalText2); - } - if(!empty($originalText2)){ + } + if (!empty($originalText2)) { $originalText = $originalText2; } // Remove leading and trailing whitespace @@ -1057,8 +1057,9 @@ function getAudioOrVideoURLOnly($fileName, $recreateCache = false) } foreach ($allFiles as $key => $value) { if ( - ($value['type'] !== 'video' && $value['type'] !== 'audio') || - (preg_match('/offline/i', $key) || preg_match('/.lock/i', $key))) { + ($value['type'] !== 'video' && $value['type'] !== 'audio') || + (preg_match('/offline/i', $key) || preg_match('/.lock/i', $key)) + ) { unset($allFiles[$key]); } } @@ -1176,7 +1177,7 @@ function getVideosURL_V2($fileName, $recreateCache = false, $checkFiles = true) //$timeName2 = "getVideosURL_V2::Video::getSourceFile({$parts['filename']}, .{$parts['extension']})"; //TimeLogStart($timeName2); $source = Video::getSourceFile($parts['filename'], ".{$parts['extension']}"); - + /* if(empty($recreateCache) && $fileName == "video_230816233020_vb81e"){ var_dump($fileName, $source);exit; @@ -1217,12 +1218,12 @@ function getVideosURL_V2($fileName, $recreateCache = false, $checkFiles = true) } $_filename = "{$parts['filename']}.{$parts['extension']}"; - if($parts['basename'] == 'index.mp4' ){ + if ($parts['basename'] == 'index.mp4') { $_filename = "index.mp4"; $source['url'] = str_replace("{$parts['filename']}.mp4", 'index.mp4', $source['url']); $source['url_noCDN'] = str_replace("{$parts['filename']}.mp4", 'index.mp4', $source['url_noCDN']); } - if($parts['basename'] == 'index.mp3' ){ + if ($parts['basename'] == 'index.mp3') { $_filename = "index.mp3"; $source['url'] = str_replace("{$parts['filename']}.mp3", 'index.mp3', $source['url']); $source['url_noCDN'] = str_replace("{$parts['filename']}.mp3", 'index.mp3', $source['url_noCDN']); @@ -2649,7 +2650,7 @@ function rrmdir($dir) function getAdsDebugTag($adCode) { global $global; - if(!empty($_REQUEST['AdsDebug']) && User::isAdmin()){ + if (!empty($_REQUEST['AdsDebug']) && User::isAdmin()) { $function = debug_backtrace()[1]["function"]; $function = str_replace('get', '', $function); $adCode = "
{$function}
{$global['lastAdsCodeReason']}
$adCode
"; @@ -3169,7 +3170,7 @@ function verifyToken($token, $salt = "") _error_log("verifyToken salt fail"); return false; } - if(!empty($obj->videos_id) && $obj->videos_id != getVideos_id()){ + if (!empty($obj->videos_id) && $obj->videos_id != getVideos_id()) { _error_log("This is not to this videos ID"); return false; } @@ -4431,7 +4432,8 @@ function isContentTypeXML() return preg_match('/xml/i', $contentType); } -function successJsonMessage($message){ +function successJsonMessage($message) +{ $obj = new stdClass(); $obj->error = false; $obj->msg = $message; @@ -5024,6 +5026,77 @@ function pathToRemoteURL($filename, $forceHTTP = false, $ignoreCDN = false) return $url; } +function pathOrURLToValidURL($filenameOrURL) +{ + global $global; + $videosURL = "{$global['webSiteRootURL']}videos/"; + $videosPath = getVideosDir(); + $relativePath = ''; + $filePath = ''; + + $parts = explode('?', $filenameOrURL); + $filenameOrURL = $parts[0]; + + $defaultURL = $filenameOrURL; + if (strpos($filenameOrURL, $videosURL) === 0) { + $relativePath = str_replace($videosURL, '', $filenameOrURL); + $filePath = "{$videosPath}{$relativePath}"; + $defaultURL = getCDN() . 'videos/'.$relativePath; + }else if(file_exists($filenameOrURL)){ + $relativePath = str_replace($videosPath, '', $filenameOrURL); + $filePath = $filenameOrURL; + $defaultURL = getCDN() . 'videos/'.$relativePath; + }else if(file_exists("{$global['systemRootPath']}{$filenameOrURL}")){ + $relativePath = str_replace('videos/', '', $filenameOrURL); + $filePath = "{$global['systemRootPath']}videos/{$relativePath}"; + $defaultURL = getCDN() . 'videos/'.$relativePath; + }else if(file_exists("{$videosPath}{$filenameOrURL}")){ + $relativePath = "{$filenameOrURL}"; + $filePath = "{$global['systemRootPath']}videos/{$relativePath}"; + $defaultURL = getCDN() . 'videos/'.$relativePath; + } + + if (file_exists($filePath)) { + if (!isDummyFile($filePath)) { + return $defaultURL; + } + + $obj = AVideoPlugin::getDataObjectIfEnabled('CDN'); + if (!empty($obj) && $obj->enable_storage) { + $pz = CDNStorage::getPZ(); + return "https://{$pz}{$relativePath}"; + } + $yptStorage = AVideoPlugin::loadPluginIfEnabled("YPTStorage"); + if (!empty($yptStorage)) { + $source = $yptStorage->getAddress($relativePath); + if(!empty($source['url'])){ + return $source['url']; + } + } + + if (!preg_match('/index.m3u8$/', $filePath)) { + if ($aws_s3 = AVideoPlugin::loadPluginIfEnabled("AWS_S3")) { + $source = $aws_s3->getAddress("{$filePath}"); + if(!empty($source['url'])){ + return $source['url']; + } + } elseif ($bb_b2 = AVideoPlugin::loadPluginIfEnabled("Blackblaze_B2")) { + $source = $bb_b2->getAddress("{$filePath}"); + if(!empty($source['url'])){ + return $source['url']; + } + } elseif ($ftp = AVideoPlugin::loadPluginIfEnabled("FTP_Storage")) { + $source = $ftp->getAddress("{$filePath}"); + if(!empty($source['url'])){ + return $source['url']; + } + } + } + } + + return $defaultURL; +} + function getFilenameFromPath($path) { global $global; @@ -5146,7 +5219,8 @@ function isWindowsServer() return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; } -function isWindows() { +function isWindows() +{ global $global; // Check if the HTTP_USER_AGENT is set if (isset($_SERVER['HTTP_USER_AGENT'])) { diff --git a/plugin/CDN/Storage/CDNStorage.php b/plugin/CDN/Storage/CDNStorage.php index f2ccc3ed72..c916ce0187 100644 --- a/plugin/CDN/Storage/CDNStorage.php +++ b/plugin/CDN/Storage/CDNStorage.php @@ -1186,7 +1186,7 @@ class CDNStorage global $global; require_once $global['systemRootPath'] . 'objects/video.php'; $file = Video::getPathToFile($filename); - $address = ['path' => $file, 'url' => $this->getURL($filename)]; + $address = ['path' => $file, 'url' => self::getURL($filename)]; return $address; } diff --git a/plugin/SocialMediaPublisher/Objects/Publisher_video_publisher_logs.php b/plugin/SocialMediaPublisher/Objects/Publisher_video_publisher_logs.php index 5541037262..992fe0c7ab 100644 --- a/plugin/SocialMediaPublisher/Objects/Publisher_video_publisher_logs.php +++ b/plugin/SocialMediaPublisher/Objects/Publisher_video_publisher_logs.php @@ -238,10 +238,11 @@ class Publisher_video_publisher_logs extends ObjectYPT } break; case SocialMediaPublisher::SOCIAL_TYPE_INSTAGRAM["name"]: - if(!empty($row['json']->mediaResponse->permalink)){ + if (!empty($row['json']->mediaResponse->permalink)) { $msg[] = "mediaResponse->permalink}' target='_blank'>{$row['json']->mediaResponse->permalink}"; - }else if ($row['status'] === self::STATUS_UNVERIFIED) { + } else if ($row['status'] === self::STATUS_UNVERIFIED) { $msg[] = ' Video is being processed: Your video is currently being processed for publishing on Instagram. Please wait.'; + $msg[] = ''; } elseif ($row['status'] === self::STATUS_VERIFIED) { $msg[] = ' Video successfully published: Your video has been verified and uploaded to Instagram.'; } else { diff --git a/plugin/SocialMediaPublisher/Objects/SocialUploader.php b/plugin/SocialMediaPublisher/Objects/SocialUploader.php index 9ae9a126c9..59dcfe5592 100644 --- a/plugin/SocialMediaPublisher/Objects/SocialUploader.php +++ b/plugin/SocialMediaPublisher/Objects/SocialUploader.php @@ -2,7 +2,7 @@ class SocialUploader { - public static function upload($publisher_user_preferences_id, $videoPath, $title, $description, $visibility = 'public', $isShort = false) + public static function upload($publisher_user_preferences_id, $videoPathMP4, $title, $description, $visibility = 'public', $isShort = false) { $title = strip_tags($title); $description = strip_tags($description); @@ -12,16 +12,18 @@ class SocialUploader } $pub = new Publisher_user_preferences($publisher_user_preferences_id); _error_log("SocialMediaPublisher::upload provider=" . $pub->getProviderName()); + + $videoPathMP4['url'] = addQueryStringParameter($videoPathMP4['url'], 'globalToken', getToken(30)); try { switch ($pub->getProviderName()) { case SocialMediaPublisher::SOCIAL_TYPE_YOUTUBE['name']: - return SocialUploader::uploadYouTube($accessToken, $videoPath, $title, $description, $visibility, $isShort); + return SocialUploader::uploadYouTube($accessToken, $videoPathMP4['path'], $title, $description, $visibility, $isShort); break; case SocialMediaPublisher::SOCIAL_TYPE_FACEBOOK['name']: $json = json_decode($pub->getJson()); $pageId = $json->{'restream.ypt.me'}->facebook->broadcaster_id; _error_log("SocialMediaPublisher::upload json=" . json_encode($json)); - return SocialUploader::uploadFacebook($accessToken, $pageId, $videoPath, $title, $description, $visibility, $isShort); + return SocialUploader::uploadFacebook($accessToken, $pageId, $videoPathMP4['url'], $title, $description, $visibility, $isShort); break; case SocialMediaPublisher::SOCIAL_TYPE_INSTAGRAM['name']: $pub = Publisher_user_preferences::getFromDb($publisher_user_preferences_id); @@ -34,7 +36,7 @@ class SocialUploader $broadcaster_id = $json->{"restream.ypt.me"}->instagram->broadcaster_id; } } - return SocialUploader::uploadInstagram($accessToken, $videoPath, $title, $description, $broadcaster_id); + return SocialUploader::uploadInstagram($accessToken, $videoPathMP4['url'], $title, $description, $broadcaster_id); break; case SocialMediaPublisher::SOCIAL_TYPE_TWITCH['name']: //return SocialUploader::uploadYouTube($accessToken, $videoPath, $title, $description, $visibility, $isShort); @@ -43,7 +45,7 @@ class SocialUploader $json = json_decode($pub->getJson()); $urn = $json->{'restream.ypt.me'}->linkedin->urn; $id = $json->{'restream.ypt.me'}->linkedin->profile_id; - $upload = SocialUploader::uploadLinkedIn($accessToken, $urn, $id, $videoPath, $title, $description, $visibility, $isShort); + $upload = SocialUploader::uploadLinkedIn($accessToken, $urn, $id, $videoPathMP4['path'], $title, $description, $visibility, $isShort); return $upload; break; } @@ -196,8 +198,6 @@ class FacebookUploader // Upload a local video directly $videoUrl = str_replace($global['systemRootPath'], $global['webSiteRootURL'], $videoPath); - $videoUrl = addQueryStringParameter($videoUrl, 'globalToken', getToken(30)); - $VideoUploadResponse = FacebookUploader::uploadDirectHostedVideo($pageId, $accessToken, $videoUrl, $title, PHP_EOL . $description); //$VideoUploadResponse = FacebookUploader::uploadDirectLocalVideo($pageId, $accessToken, $videoPath); diff --git a/plugin/SocialMediaPublisher/SocialMediaPublisher.php b/plugin/SocialMediaPublisher/SocialMediaPublisher.php index e6abc42317..6d2b690317 100644 --- a/plugin/SocialMediaPublisher/SocialMediaPublisher.php +++ b/plugin/SocialMediaPublisher/SocialMediaPublisher.php @@ -257,7 +257,7 @@ class SocialMediaPublisher extends PluginAbstract } $providerName = $o->getProviderName(); - + /* $fromFileLocation = $paths['mp4']['url']; if (!isDummyFile($paths['mp4']['path'])) { $videoPath = $paths['mp4']['path']; @@ -270,7 +270,7 @@ class SocialMediaPublisher extends PluginAbstract _error_log("SocialMediaPublisher::upload($publisher_user_preferences_id, $videos_id) $providerName end conversion "); } _error_log("SocialMediaPublisher::upload($publisher_user_preferences_id, $videos_id) $providerName Upload start "); - + */ if (!empty($_REQUEST['title'])) { $title = $_REQUEST['title']; } else { @@ -287,10 +287,7 @@ class SocialMediaPublisher extends PluginAbstract $visibility = 'public'; } - $response = SocialUploader::upload($publisher_user_preferences_id, $videoPath, $title, $description, $visibility); - if (!empty($videoPathToYouTube)) { - //unlink($videoPathToYouTube); - } + $response = SocialUploader::upload($publisher_user_preferences_id, $paths['mp4'], $title, $description, $visibility); _error_log("SocialMediaPublisher::upload($publisher_user_preferences_id, $videos_id) $providerName complete " . json_encode($response)); self::saveLog($publisher_social_medias_id, $videos_id, array('publisher_user_preferences_id' => $publisher_user_preferences_id, 'response' => $response)); return $response; diff --git a/plugin/SocialMediaPublisher/publishInstagram.json.php b/plugin/SocialMediaPublisher/publishInstagram.json.php index c3f68ebcd2..4138ef353b 100644 --- a/plugin/SocialMediaPublisher/publishInstagram.json.php +++ b/plugin/SocialMediaPublisher/publishInstagram.json.php @@ -9,6 +9,8 @@ $obj->msg = ''; $plugin = AVideoPlugin::loadPluginIfEnabled('SocialMediaPublisher'); -$obj = SocialMediaPublisher::scanInstagam(); +//$obj = SocialMediaPublisher::scanInstagam(); + +$obj = InstagramUploader::publishMediaIfIsReady($_REQUEST['accessToken'], $_REQUEST['containerId'], $_REQUEST['instagramAccountId']); die(json_encode($obj)); diff --git a/plugin/SocialMediaPublisher/script.js b/plugin/SocialMediaPublisher/script.js index 25f7b2741d..c0d8e51ad1 100644 --- a/plugin/SocialMediaPublisher/script.js +++ b/plugin/SocialMediaPublisher/script.js @@ -95,4 +95,23 @@ function reloadSocialAccountsTables() { Publisher_video_publisher_logstableVarVID.ajax.reload(); } +} + +function checkInstagram(accessToken, containerId, instagramAccountId) { + var url = webSiteRootURL + 'plugin/SocialMediaPublisher/publishInstagram.json.php'; + url = addQueryStringParameter(url, 'accessToken', accessToken); + url = addQueryStringParameter(url, 'containerId', containerId); + url = addQueryStringParameter(url, 'instagramAccountId', instagramAccountId); + modal.showPleaseWait(); + $.ajax({ + url: url, + complete: function(response) { + modal.hidePleaseWait(); + if (response.error) { + avideoAlertError(response.msg); + } else { + reloadSocialAccountsTables(); + } + } + }); } \ No newline at end of file diff --git a/plugin/SocialMediaPublisher/uploadVideo.php b/plugin/SocialMediaPublisher/uploadVideo.php index 78b03ad73a..97969c33b1 100644 --- a/plugin/SocialMediaPublisher/uploadVideo.php +++ b/plugin/SocialMediaPublisher/uploadVideo.php @@ -24,7 +24,6 @@ if (empty($videos_id)) { .social-network .btn { margin: 20px 0; } -
@@ -73,6 +72,7 @@ if (empty($videos_id)) {
print();