From e732a416a179ea1d7ac491427d89e5bfa2ee7410 Mon Sep 17 00:00:00 2001
From: Oleksandr Kuzava <oleksandr.kuzava@dropsolid.com>
Date: Mon, 22 Aug 2022 23:40:08 +0300
Subject: [PATCH] Improve detecting cloudinary video.

---
 .../video_embed_field/Provider/Cloudinary.php | 21 +++++++++----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/modules/cloudinary_video/src/Plugin/video_embed_field/Provider/Cloudinary.php b/modules/cloudinary_video/src/Plugin/video_embed_field/Provider/Cloudinary.php
index 825e07d..49917bc 100644
--- a/modules/cloudinary_video/src/Plugin/video_embed_field/Provider/Cloudinary.php
+++ b/modules/cloudinary_video/src/Plugin/video_embed_field/Provider/Cloudinary.php
@@ -43,7 +43,7 @@ class Cloudinary extends ProviderPluginBase {
    *   The video format.
    */
   protected function getVideoFormat() {
-    $info = pathinfo($this->getInput());
+    $info = pathinfo($this->getVideoId());
 
     return $info['extension'];
   }
@@ -93,9 +93,7 @@ class Cloudinary extends ProviderPluginBase {
    * {@inheritdoc}
    */
   public function getRemoteThumbnailUrl() {
-    [, $uri] = explode('://', $this->getInput());
-
-    $source = str_replace(".{$this->getVideoFormat()}", '.jpg', $uri);
+    $source = str_replace(".{$this->getVideoFormat()}", '.jpg', $this->getVideoId());
     $options['secure'] = TRUE;
     $options['resource_type'] = 'video';
 
@@ -113,7 +111,7 @@ class Cloudinary extends ProviderPluginBase {
    *   The url to use for remote video.
    */
   public function getRemoteVideoUrl() {
-    [, $source] = explode('://', $this->getInput());
+    $source = $this->getVideoId();
 
     $options['secure'] = TRUE;
     $options['resource_type'] = 'video';
@@ -293,15 +291,16 @@ class Cloudinary extends ProviderPluginBase {
    * {@inheritdoc}
    */
   public static function getIdFromInput($input) {
-    [, $source] = explode('://', $input, 2);
-
-    if (preg_match('/^(.+)\.(.+)$/', $source, $matches)) {
-      return $matches[1];
+    // Get public id from the video schema.
+    if (str_contains($input, 'cloudinary://')) {
+      return substr($input, strlen('cloudinary://'));
     }
 
-    \Drupal::messenger()->addError('We could not fetch id from the video URL.');
+    if (str_contains($input, '/video/upload/')) {
+      \Drupal::messenger()->addWarning(t('External cloudinary URL is currently not supported.'));
+    }
 
-    return '';
+    return FALSE;
   }
 
 }
-- 
GitLab