Search

Mar 29, 2023

Magento 2: Adding link Vimeo not working

Steps to reproduce: 

1. Log in to the Admin. 

2. To edit an existing product go to CATALOG > Products > Edit or to create a new product go to CATALOG > Products > Edit > Add Product . 

3. Click the Images And Videos tab in the Product page. 

4. Click Add Video and add a Vimeo video's URL. 

5. Expected result: The new video is found and saved. 

Actuals result: Error: "Video not Found" is displayed.


SOLUTION FOR M2 CLOUD:

1. Install the latest QPT package: 

    composer update magento/ece-tools --with-dependencies

2. Add MDVA-35092 to .magento.env.yaml file

    Example:

stage:
  build:
    QUALITY_PATCHES:
      - MDVA-35092


SOLUTION 2:

Follow the patch

diff --git a/vendor/magento/module-product-video/etc/csp_whitelist.xml
b/vendor/magento/module-product-video/etc/csp_whitelist.xml
new file mode 100644
index 00000000000..2e091440330
--- /dev/null
+++ b/vendor/magento/module-product-video/etc/csp_whitelist.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+/**
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+-->
+<csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+               xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp:etc/csp_whitelist.xsd">
+    <policies>
+        <policy id="script-src">
+            <values>
+                <value id="youtube_cdn" type="host">s.ytimg.com</value>
+                <value id="google_video" type="host">www.googleapis.com</value>
+                <value id="vimeo" type="host">vimeo.com</value>
+                <value id="www_vimeo" type="host">www.vimeo.com</value>
+                <value id="vimeo_cdn" type="host">*.vimeocdn.com</value>
+            </values>
+        </policy>
+        <policy id="img-src">
+            <values>
+                <value id="vimeo_cdn" type="host">*.vimeocdn.com</value>
+            </values>
+        </policy>
+        <policy id="frame-src">
+            <values>
+                <value id="player_vimeo" type="host">player.vimeo.com</value>
+            </values>
+        </policy>
+    </policies>
+</csp_whitelist>
diff --git a/vendor/magento/module-product-video/view/adminhtml/web/js/get-video-information.js b/vendor/magento/module-product-video/view/adminhtml/web/js/get-video-information.js
index 653434f1008..3b166b044e1 100644
--- a/vendor/magento/module-product-video/view/adminhtml/web/js/get-video-information.js
+++ b/vendor/magento/module-product-video/view/adminhtml/web/js/get-video-information.js
@@ -492,20 +492,20 @@ define([
                     var tmp,
                         respData;
 
-                    if (data.length < 1) {
+                    if (!data) {
                         this._onRequestError($.mage.__('Video not found'));
 
                         return null;
                     }
-                    tmp = data[0];
+                    tmp = data;
                     respData = {
                         duration: this._formatVimeoDuration(tmp.duration),
-                        channel: tmp['user_name'],
-                        channelId: tmp['user_url'],
+                        channel: tmp['author_name'],
+                        channelId: tmp['author_url'],
                         uploaded: tmp['upload_date'],
                         title: tmp.title,
                         description: tmp.description.replace(/(&nbsp;|<([^>]+)>)/ig, ''),
-                        thumbnail: tmp['thumbnail_large'],
+                        thumbnail: tmp['thumbnail_url'],
                         videoId: videoInfo.id,
                         videoProvider: videoInfo.type
                     };
@@ -534,10 +534,11 @@ define([
                     );
                 } else if (type === 'vimeo') {
                     $.ajax({
-                        url: 'https://www.vimeo.com/api/v2/video/' + id + '.json',
+                        url: 'https://vimeo.com/api/oembed.json',
                         dataType: 'jsonp',
                         data: {
-                            format: 'json'
+                            format: 'json',
+                            url: 'https://vimeo.com/' + id
                         },
                         timeout: 5000,
                         success:  $.proxy(_onVimeoLoaded, self),
diff --git a/vendor/magento/framework/File/Uploader.php b/vendor/magento/framework/File/Uploader.php
index c0126acf700..69ae2616c2d 100644
--- a/vendor/magento/framework/File/Uploader.php
+++ b/vendor/magento/framework/File/Uploader.php
@@ -6,7 +6,11 @@
 namespace Magento\Framework\File;
 
 use Magento\Framework\App\Filesystem\DirectoryList;
+use Magento\Framework\App\ObjectManager;
 use Magento\Framework\Exception\FileSystemException;
+use Magento\Framework\Filesystem;
+use Magento\Framework\Filesystem\DriverInterface;
+use Magento\Framework\Filesystem\DriverPool;
 use Magento\Framework\Validation\ValidationException;
 
 /**
@@ -144,15 +148,13 @@ class Uploader
 
     /**
      * Maximum Image Width resolution in pixels. For image resizing on client side
-     * @deprecated
-     * @see \Magento\Framework\Image\Adapter\UploadConfigInterface::getMaxWidth()
+     * @deprecated @see \Magento\Framework\Image\Adapter\UploadConfigInterface::getMaxWidth()
      */
     const MAX_IMAGE_WIDTH = 1920;
 
     /**
      * Maximum Image Height resolution in pixels. For image resizing on client side
-     * @deprecated
-     * @see \Magento\Framework\Image\Adapter\UploadConfigInterface::getMaxHeight()
+     * @deprecated @see \Magento\Framework\Image\Adapter\UploadConfigInterface::getMaxHeight()
      */
     const MAX_IMAGE_HEIGHT = 1200;
 
@@ -169,21 +171,32 @@ class Uploader
      */
     private $directoryList;
 
+    /**
+     * @var DriverPool|null
+     */
+    private $driverPool;
+
+    /**
+     * @var DriverInterface|null
+     */
+    private $fileDriver;
+
     /**
      * Init upload
      *
      * @param string|array $fileId
      * @param \Magento\Framework\File\Mime|null $fileMime
      * @param DirectoryList|null $directoryList
+     * @param DriverPool|null $driverPool
      * @throws \DomainException
      */
     public function __construct(
         $fileId,
         Mime $fileMime = null,
-        DirectoryList $directoryList = null
+        DirectoryList $directoryList = null,
+        DriverPool $driverPool = null
     ) {
-        $this->directoryList= $directoryList ?: \Magento\Framework\App\ObjectManager::getInstance()
-            ->get(DirectoryList::class);
+        $this->directoryList= $directoryList ?: ObjectManager::getInstance()->get(DirectoryList::class);
 
         $this->_setUploadFileId($fileId);
         if (!file_exists($this->_file['tmp_name'])) {
@@ -192,7 +205,8 @@ class Uploader
         } else {
             $this->_fileExists = true;
         }
-        $this->fileMime = $fileMime ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Mime::class);
+        $this->fileMime = $fileMime ?: ObjectManager::getInstance()->get(Mime::class);
+        $this->driverPool = $driverPool;
     }
 
     /**
@@ -230,7 +244,7 @@ class Uploader
             $this->setAllowCreateFolders(true);
             $this->_dispretionPath = static::getDispersionPath($fileName);
             $destinationFile .= $this->_dispretionPath;
-            $this->_createDestinationFolder($destinationFile);
+            $this->createDestinationFolder($destinationFile);
         }
 
         if ($this->_allowRenameFiles) {
@@ -275,13 +289,11 @@ class Uploader
      * @return void
      * @throws FileSystemException
      */
-    private function validateDestination($destinationFolder)
+    private function validateDestination(string $destinationFolder): void
     {
         if ($this->_allowCreateFolders) {
-            $this->_createDestinationFolder($destinationFolder);
-        }
-
-        if (!is_writable($destinationFolder)) {
+            $this->createDestinationFolder($destinationFolder);
+        } elseif (!$this->getFileDriver()->isWritable($destinationFolder)) {
             throw new FileSystemException(__('Destination folder is not writable or does not exists.'));
         }
     }
@@ -655,7 +667,7 @@ class Uploader
      * @return \Magento\Framework\File\Uploader
      * @throws FileSystemException
      */
-    private function _createDestinationFolder($destinationFolder)
+    private function createDestinationFolder(string $destinationFolder)
     {
         if (!$destinationFolder) {
             return $this;
@@ -665,11 +677,13 @@ class Uploader
             $destinationFolder = substr($destinationFolder, 0, -1);
         }
 
-        if (!(@is_dir($destinationFolder)
-            || @mkdir($destinationFolder, 0777, true)
-        )) {
-            throw new FileSystemException(__('Unable to create directory %1.', $destinationFolder));
+        if (!$this->getFileDriver()->isDirectory($destinationFolder)) {
+            $result = $this->getFileDriver()->createDirectory($destinationFolder);
+            if (!$result) {
+                throw new FileSystemException(__('Unable to create directory %1.', $destinationFolder));
+            }
         }
+
         return $this;
     }
 
@@ -681,20 +695,22 @@ class Uploader
      */
     public static function getNewFileName($destinationFile)
     {
+        /** @var Filesystem $fileSystem */
+        $fileSystem = ObjectManager::getInstance()->get(Filesystem::class);
+        $local = $fileSystem->getDirectoryRead(DirectoryList::ROOT);
+
+        $fileExists = function ($path) use ($local) {
+            return $local->isExist($path);
+        };
+
         $fileInfo = pathinfo($destinationFile);
-        if (file_exists($destinationFile)) {
-            $index = 1;
-            $baseName = $fileInfo['filename'] . '.' . $fileInfo['extension'];
-            while (file_exists($fileInfo['dirname'] . '/' . $baseName)) {
-                $baseName = $fileInfo['filename'] . '_' . $index . '.' . $fileInfo['extension'];
-                $index++;
-            }
-            $destFileName = $baseName;
-        } else {
-            return $fileInfo['basename'];
+        $index = 1;
+        while ($fileExists($fileInfo['dirname'] . '/' . $fileInfo['basename'])) {
+            $fileInfo['basename'] = $fileInfo['filename'] . '_' . ($index++);
+            $fileInfo['basename'] .= isset($fileInfo['extension']) ? '.' . $fileInfo['extension'] : '';
         }
 
-        return $destFileName;
+        return $fileInfo['basename'];
     }
 
     /**
@@ -732,4 +748,20 @@ class Uploader
         }
         return $dispersionPath;
     }
+
+    /**
+     * Get driver for file
+     *
+     * @deprecated
+     * @return DriverInterface
+     */
+    private function getFileDriver(): DriverInterface
+    {
+        if (!$this->fileDriver) {
+            $this->driverPool = $this->driverPool ?: ObjectManager::getInstance()->get(DriverPool::class);
+            $this->fileDriver = $this->driverPool->getDriver(DriverPool::FILE);
+        }
+
+        return $this->fileDriver;
+    }
 }
diff --git a/lib/web/fotorama/fotorama.js b/lib/web/fotorama/fotorama.js
index ddf5a7f000d..62abff99e3b 100644
--- a/lib/web/fotorama/fotorama.js
+++ b/lib/web/fotorama/fotorama.js
@@ -858,13 +858,16 @@ fotoramaVersion = '4.6.4';
             dataFrame.thumbsReady = true;
         } else if (video.type === 'vimeo') {
             $.ajax({
-                url: getProtocol() + 'vimeo.com/api/v2/video/' + video.id + '.json',
+                url: getProtocol() + 'vimeo.com/api/oembed.json',
+                data: {
+                    url: 'https://vimeo.com/' + video.id
+                },
                 dataType: 'jsonp',
                 success: function (json) {
                     dataFrame.thumbsReady = true;
                     updateData(data, {
-                        img: json[0].thumbnail_large,
-                        thumb: json[0].thumbnail_small
+                        img: json[0].thumbnail_url,
+                        thumb: json[0].thumbnail_url
                     }, dataFrame.i, fotorama);
                 }
             });


No comments:

Post a Comment