From d9f7fe86e60c5fbfc4411c22edbb437c448f3ab5 Mon Sep 17 00:00:00 2001 From: pini-girit Date: Mon, 14 Jan 2019 12:20:43 +0200 Subject: [PATCH] v1.7.4: Fixed PHP_CodeSniffer errors & installation error on php5.6 + updated composer.json --- Block/Adminhtml/Product/Edit/NewVideo.php | 3 + Command/UploadImages.php | 2 +- Core/CloudinaryImageProvider.php | 4 +- Core/ConfigurationBuilder.php | 1 + Core/Image/Transformation/Dimensions.php | 1 + Core/Migration/BatchUploader.php | 3 + Core/ValidateRemoteUrlRequest.php | 1 + Model/Api/ResourcesManagement.php | 34 +-- Model/BatchUploader.php | 2 +- Model/Config/Backend/Credentials.php | 2 + Model/Config/Backend/Free.php | 2 - Model/Config/Source/Dropdown/Dpr.php | 12 +- Model/Configuration.php | 1 + Model/Observer/Configuration.php | 3 +- Model/ProductImageFinder.php | 3 +- .../ProductImageFinder/DeletedImageFilter.php | 2 +- Model/ProductImageFinder/ImageCreator.php | 1 - Model/ProductImageFinder/ImageFilter.php | 2 +- Model/ProductImageFinder/NewImageFilter.php | 2 +- Model/SynchronisationRepository.php | 3 +- Model/Transformation.php | 1 + Plugin/Catalog/Block/Product/ImageFactory.php | 1 + .../Model/Product/Image/UrlBuilder.php | 1 + Plugin/ImageHelper.php | 1 + Plugin/Widget/Model/Template/Filter.php | 2 + Setup/UpgradeData.php | 1 + marketplace.composer.json | 24 +++ .../adminhtml/web/js/get-video-information.js | 203 +++++++++--------- 28 files changed, 183 insertions(+), 135 deletions(-) create mode 100644 marketplace.composer.json diff --git a/Block/Adminhtml/Product/Edit/NewVideo.php b/Block/Adminhtml/Product/Edit/NewVideo.php index 8c775a9..88e20d0 100644 --- a/Block/Adminhtml/Product/Edit/NewVideo.php +++ b/Block/Adminhtml/Product/Edit/NewVideo.php @@ -49,6 +49,7 @@ protected function getCloudinaryConfig() $this->_cloudinaryConfig['api_url'] = "https://api.cloudinary.com/v1_1/{$this->_cloudinaryConfig['cloud_name']}/"; } } + return $this->_cloudinaryConfig; } @@ -85,11 +86,13 @@ protected function getNoteVideoUrl() } else { $result .= __(', YouTube'); } + if (!$this->getCloudinaryConfig()) { $messages .= __('
*To add Cloudinary video, please enter your Cloudinary Account Credentials first.', $this->getCloudinaryConfigUrl()); } else { $result .= __(', Cloudinary'); } + return $result . $messages; } diff --git a/Command/UploadImages.php b/Command/UploadImages.php index 7a2cca8..3bdfaff 100644 --- a/Command/UploadImages.php +++ b/Command/UploadImages.php @@ -33,7 +33,7 @@ public function __construct(BatchUploader $batchUploader, OutputLogger $outputLo /** * Configure the command - * + * * @return void */ protected function configure() diff --git a/Core/CloudinaryImageProvider.php b/Core/CloudinaryImageProvider.php index be1c033..dd69802 100644 --- a/Core/CloudinaryImageProvider.php +++ b/Core/CloudinaryImageProvider.php @@ -84,7 +84,8 @@ public function upload(Image $image) public function retrieveTransformed(Image $image, Transformation $transformation) { $imagePath = \cloudinary_url( - $image->getId(), [ + $image->getId(), + [ 'transformation' => $transformation->build(), 'secure' => true, 'sign_url' => $this->configuration->getUseSignedUrls() @@ -141,6 +142,7 @@ public function validateCredentials() } catch (\Exception $e) { return false; } + return true; } diff --git a/Core/ConfigurationBuilder.php b/Core/ConfigurationBuilder.php index 21e8f50..d559f1d 100644 --- a/Core/ConfigurationBuilder.php +++ b/Core/ConfigurationBuilder.php @@ -25,6 +25,7 @@ public function build() if ($this->configuration->getCdnSubdomainStatus()) { $config['cdn_subdomain'] = true; } + return $config; } } diff --git a/Core/Image/Transformation/Dimensions.php b/Core/Image/Transformation/Dimensions.php index 757c23d..93370be 100644 --- a/Core/Image/Transformation/Dimensions.php +++ b/Core/Image/Transformation/Dimensions.php @@ -36,6 +36,7 @@ public static function squareMissingDimension(Dimensions $dimensions) } elseif (!$dimensions->getHeight()) { return Dimensions::square($dimensions->getWidth()); } + return $dimensions; } diff --git a/Core/Migration/BatchUploader.php b/Core/Migration/BatchUploader.php index 3515c1e..d1c4b4c 100644 --- a/Core/Migration/BatchUploader.php +++ b/Core/Migration/BatchUploader.php @@ -88,8 +88,10 @@ public function uploadImages(array $images) if ($this->migrationTask->hasBeenStopped()) { break; } + $this->uploadImage($image); } + $this->logger->notice(sprintf(self::MESSAGE_STATUS, $this->countMigrated, $this->countFailed)); } @@ -160,6 +162,7 @@ private function addRetryMessage($retryMessage, \Exception $e) } else { $e = new \Exception($e->getMessage() . $retryMessage); } + return $e; } diff --git a/Core/ValidateRemoteUrlRequest.php b/Core/ValidateRemoteUrlRequest.php index cbb32db..5014f9c 100644 --- a/Core/ValidateRemoteUrlRequest.php +++ b/Core/ValidateRemoteUrlRequest.php @@ -19,6 +19,7 @@ public function validate() if ($result->responseCode == 200 && is_null($result->error)) { return true; } + return false; } diff --git a/Model/Api/ResourcesManagement.php b/Model/Api/ResourcesManagement.php index 1e374f7..3545802 100644 --- a/Model/Api/ResourcesManagement.php +++ b/Model/Api/ResourcesManagement.php @@ -6,6 +6,8 @@ use Cloudinary\Api; use Cloudinary\Cloudinary\Core\ConfigurationBuilder; use Cloudinary\Cloudinary\Core\ConfigurationInterface; +use Magento\Framework\App\Request\Http; +use Magento\Framework\Json\EncoderInterface; class ResourcesManagement implements \Cloudinary\Cloudinary\Api\ResourcesManagementInterface { @@ -28,26 +30,36 @@ class ResourcesManagement implements \Cloudinary\Cloudinary\Api\ResourcesManagem private $_api; /** - * @var \Magento\Framework\App\Request\Http + * @var Http */ private $_request; + /** + * @var EncoderInterface + */ + private $_jsonEncoder; + /** * ApiClient constructor. * * @param ConfigurationInterface $configuration * @param ConfigurationBuilder $configurationBuilder + * @param Api $api + * @param Http $request + * @param EncoderInterface $jsonEncoder */ public function __construct( ConfigurationInterface $configuration, ConfigurationBuilder $configurationBuilder, Api $api, - \Magento\Framework\App\Request\Http $request + Http $request, + EncoderInterface $jsonEncoder ) { $this->_configuration = $configuration; $this->_configurationBuilder = $configurationBuilder; $this->_api = $api; $this->_request = $request; + $this->_jsonEncoder = $jsonEncoder; if ($this->_configuration->isEnabled()) { $this->_authorise(); } @@ -59,13 +71,6 @@ private function _authorise() Cloudinary::$USER_PLATFORM = $this->_configuration->getUserPlatform(); } - public function _sendJsonResponse($response) - { - header('Content-Type: application/json'); - echo json_encode($response); - die; - } - /** * Get details of a single resource * @@ -76,18 +81,19 @@ protected function _getResourceData() { try { $this->_resourceData = $this->_api->resource( - $this->_request->getParam("id"), [ + $this->_request->getParam("id"), + [ "resource_type" => $this->_resourceType ] ); - $this->_sendJsonResponse( + return $this->_jsonEncoder->encode( [ "error" => 0, "data" => $this->_resourceData ] ); } catch (\Exception $e) { - $this->_sendJsonResponse( + return $this->_jsonEncoder->encode( [ "error" => 1, "message" => $e->getMessage() @@ -102,7 +108,7 @@ protected function _getResourceData() public function getImage() { $this->_resourceType = "image"; - $this->_getResourceData(); + return $this->_getResourceData(); } /** @@ -111,6 +117,6 @@ public function getImage() public function getVideo() { $this->_resourceType = "video"; - $this->_getResourceData(); + return $this->_getResourceData(); } } diff --git a/Model/BatchUploader.php b/Model/BatchUploader.php index 4aa8e03..a481048 100644 --- a/Model/BatchUploader.php +++ b/Model/BatchUploader.php @@ -85,6 +85,7 @@ public function uploadUnsynchronisedImages(OutputInterface $output = null) $this->displayMessage($output, self::MESSAGE_UPLOAD_INTERRUPTED); return false; } + $this->uploadAndSynchronise($image, $output); } @@ -92,7 +93,6 @@ public function uploadUnsynchronisedImages(OutputInterface $output = null) $this->displayMessage($output, sprintf(self::MESSAGE_UPLOAD_COMPLETE, count($images))); return true; - } catch (\Exception $e) { $this->migrationTask->stop(); throw $e; diff --git a/Model/Config/Backend/Credentials.php b/Model/Config/Backend/Credentials.php index 87a9267..ff07e39 100644 --- a/Model/Config/Backend/Credentials.php +++ b/Model/Config/Backend/Credentials.php @@ -105,6 +105,7 @@ public function beforeSave() if (!$rawValue) { throw new ValidatorException(__(self::CREDENTIALS_CHECK_MISSING)); } + if ($this->isSaveAllowed()) { $this->validate($this->getCredentialsFromEnvironmentVariable($rawValue)); } else { @@ -143,6 +144,7 @@ private function getCredentialsFromEnvironmentVariable($environmentVariable) if (Cloudinary::config_get('private_cdn')) { $credentials["private_cdn"] = Cloudinary::config_get('private_cdn'); } + return $credentials; } catch (\Exception $e) { throw new ValidatorException(__(self::CREDENTIALS_CHECK_FAILED)); diff --git a/Model/Config/Backend/Free.php b/Model/Config/Backend/Free.php index e61e0d4..03e1ebe 100644 --- a/Model/Config/Backend/Free.php +++ b/Model/Config/Backend/Free.php @@ -73,13 +73,11 @@ public function __construct( public function beforeSave() { if ($this->hasAccountConfigured() && $this->getValue()) { - $transform = $this->configuration ->getDefaultTransformation() ->withFreeform(Freeform::fromString($this->getValue())); $this->validate($this->sampleImageUrl($transform)); - } parent::beforeSave(); diff --git a/Model/Config/Source/Dropdown/Dpr.php b/Model/Config/Source/Dropdown/Dpr.php index 1726467..38ebdc5 100644 --- a/Model/Config/Source/Dropdown/Dpr.php +++ b/Model/Config/Source/Dropdown/Dpr.php @@ -8,15 +8,15 @@ class Dpr implements OptionSourceInterface { public function toOptionArray() { - return array( - array( + return [ + [ 'value' => '1.0', 'label' => '1.0', - ), - array( + ], + [ 'value' => '2.0', 'label' => '2.0', - ), - ); + ], + ]; } } diff --git a/Model/Configuration.php b/Model/Configuration.php index 0323f2c..1d18078 100644 --- a/Model/Configuration.php +++ b/Model/Configuration.php @@ -261,6 +261,7 @@ public function getEnvironmentVariable() $this->logger->critical($invalidConfigException); } } + return $this->environmentVariable; } diff --git a/Model/Observer/Configuration.php b/Model/Observer/Configuration.php index b294a9e..052502e 100644 --- a/Model/Observer/Configuration.php +++ b/Model/Observer/Configuration.php @@ -73,7 +73,8 @@ public function execute(Observer $observer) $this->changedPaths = (array) $observer->getEvent()->getChangedPaths(); if (count( array_intersect( - $this->changedPaths, [ + $this->changedPaths, + [ \Cloudinary\Cloudinary\Model\Configuration::CONFIG_PATH_ENABLED, \Cloudinary\Cloudinary\Model\Configuration::CONFIG_PATH_ENVIRONMENT_VARIABLE, \Cloudinary\Cloudinary\Model\AutoUploadMapping\AutoUploadConfiguration::REQUEST_PATH diff --git a/Model/ProductImageFinder.php b/Model/ProductImageFinder.php index 1a050e2..b6d3eb7 100644 --- a/Model/ProductImageFinder.php +++ b/Model/ProductImageFinder.php @@ -57,7 +57,8 @@ public function findDeletedImages(Product $product) private function find(Product $product, ImageFilter $filter) { return array_map( - $this->imageCreator, array_filter( + $this->imageCreator, + array_filter( $product->getMediaGallery('images') ?: [], $filter ) diff --git a/Model/ProductImageFinder/DeletedImageFilter.php b/Model/ProductImageFinder/DeletedImageFilter.php index 32e8f0c..ac8193e 100644 --- a/Model/ProductImageFinder/DeletedImageFilter.php +++ b/Model/ProductImageFinder/DeletedImageFilter.php @@ -12,4 +12,4 @@ public function __invoke($imageData) { return isset($imageData['removed']) && $imageData['removed'] == 1; } -} \ No newline at end of file +} diff --git a/Model/ProductImageFinder/ImageCreator.php b/Model/ProductImageFinder/ImageCreator.php index 8f987c7..72f78ea 100644 --- a/Model/ProductImageFinder/ImageCreator.php +++ b/Model/ProductImageFinder/ImageCreator.php @@ -53,4 +53,3 @@ public function __invoke(array $imageData) ); } } - diff --git a/Model/ProductImageFinder/ImageFilter.php b/Model/ProductImageFinder/ImageFilter.php index a027b9b..9908020 100644 --- a/Model/ProductImageFinder/ImageFilter.php +++ b/Model/ProductImageFinder/ImageFilter.php @@ -14,4 +14,4 @@ interface ImageFilter * @return boolean */ public function __invoke($imageData); -} \ No newline at end of file +} diff --git a/Model/ProductImageFinder/NewImageFilter.php b/Model/ProductImageFinder/NewImageFilter.php index 03dfce6..f07db2a 100644 --- a/Model/ProductImageFinder/NewImageFilter.php +++ b/Model/ProductImageFinder/NewImageFilter.php @@ -16,4 +16,4 @@ public function __invoke($imageData) { return !empty($imageData['new_file']); } -} \ No newline at end of file +} diff --git a/Model/SynchronisationRepository.php b/Model/SynchronisationRepository.php index 1b335e9..5deffd4 100644 --- a/Model/SynchronisationRepository.php +++ b/Model/SynchronisationRepository.php @@ -16,8 +16,7 @@ use Magento\Framework\Api\SearchResultsInterface; use Magento\Framework\Api\SearchResultsInterfaceFactory; -class SynchronisationRepository - implements SynchronisationRepositoryInterface, SynchroniseAssetsRepositoryInterface +class SynchronisationRepository implements SynchronisationRepositoryInterface, SynchroniseAssetsRepositoryInterface { /** * @var CollectionFactory diff --git a/Model/Transformation.php b/Model/Transformation.php index d2979eb..205842a 100644 --- a/Model/Transformation.php +++ b/Model/Transformation.php @@ -98,6 +98,7 @@ public function addFreeformTransformationForImage(ImageTransformation $transform if (($this->getImageName() === $imageFile) && $this->hasFreeTransformation()) { $transformation->withFreeform(Freeform::fromString($this->getFreeTransformation())); } + return $transformation; } diff --git a/Plugin/Catalog/Block/Product/ImageFactory.php b/Plugin/Catalog/Block/Product/ImageFactory.php index aa22d98..15f197e 100644 --- a/Plugin/Catalog/Block/Product/ImageFactory.php +++ b/Plugin/Catalog/Block/Product/ImageFactory.php @@ -177,6 +177,7 @@ private function createTransformation(array $imageMiscParams) if (isset($imageMiscParams['keep_frame'])) { $this->keepFrame = ($imageMiscParams['keep_frame'] === 'frame') ? true : false; } + if ($this->keepFrame) { $transform->withCrop(Crop::fromString('lpad')) ->withDimensions(Dimensions::squareMissingDimension($dimensions)); diff --git a/Plugin/Catalog/Model/Product/Image/UrlBuilder.php b/Plugin/Catalog/Model/Product/Image/UrlBuilder.php index 60ca77a..ec9b194 100644 --- a/Plugin/Catalog/Model/Product/Image/UrlBuilder.php +++ b/Plugin/Catalog/Model/Product/Image/UrlBuilder.php @@ -174,6 +174,7 @@ private function createTransformation(array $imageMiscParams) if (isset($imageMiscParams['keep_frame'])) { $this->keepFrame = ($imageMiscParams['keep_frame'] === 'frame') ? true : false; } + if ($this->keepFrame) { $transform->withCrop(Crop::fromString('lpad')) ->withDimensions(Dimensions::squareMissingDimension($dimensions)); diff --git a/Plugin/ImageHelper.php b/Plugin/ImageHelper.php index 2cc2a54..4e14ed6 100644 --- a/Plugin/ImageHelper.php +++ b/Plugin/ImageHelper.php @@ -137,6 +137,7 @@ public function aroundGetUrl(CatalogImageHelper $helper, \Closure $originalMetho if (!$this->configuration->isEnabled()) { return $originalMethod(); } + $imagePath = $this->imageFile ?: $this->product->getData($helper->getType()); $image = $this->imageFactory->build(sprintf('catalog/product%s', $imagePath), $originalMethod); diff --git a/Plugin/Widget/Model/Template/Filter.php b/Plugin/Widget/Model/Template/Filter.php index 463978f..dcd93b2 100644 --- a/Plugin/Widget/Model/Template/Filter.php +++ b/Plugin/Widget/Model/Template/Filter.php @@ -64,10 +64,12 @@ public function aroundMediaDirective(\Magento\Widget\Model\Template\Filter $widg if (!$this->_configuration->isEnabled()) { return $proceed($construction); } + $params = $this->_cloudinaryWidgetFilter->getParams($construction[2]); if (!isset($params['url'])) { return $proceed($construction); } + $url = (preg_match('/^".+"$/', $params['url'])) ? preg_replace('/(^")|("$)/', '', $params['url']) : $params['url']; $image = $this->_imageFactory->build( diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php index 5fd1bc9..063a518 100644 --- a/Setup/UpgradeData.php +++ b/Setup/UpgradeData.php @@ -51,6 +51,7 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface if ($context->getVersion()) { $this->output->writeln("Reseting configurations for 'website' & 'store' scopes (only supports 'default' at the moment)"); } + $this->resourceConnection->getConnection()->delete( $this->resourceConnection->getTableName('core_config_data'), "path LIKE 'cloudinary/%' AND scope != 'default'" diff --git a/marketplace.composer.json b/marketplace.composer.json new file mode 100644 index 0000000..d747db2 --- /dev/null +++ b/marketplace.composer.json @@ -0,0 +1,24 @@ +{ + "name": "cloudinary/cloudinary", + "type": "magento2-module", + "version": "1.7.4", + "description": "Cloudinary Magento 2 Integration.", + "license": "MIT", + "require": { + "cloudinary/cloudinary_php": "*" + }, + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "Cloudinary\\Cloudinary\\": "" + } + }, + "repositories": [ + { + "type": "composer", + "url": "https://repo.magento.com/" + } + ] +} diff --git a/view/adminhtml/web/js/get-video-information.js b/view/adminhtml/web/js/get-video-information.js index 82a68f5..0c17bc2 100644 --- a/view/adminhtml/web/js/get-video-information.js +++ b/view/adminhtml/web/js/get-video-information.js @@ -9,7 +9,7 @@ define( 'jquery/ui', 'mage/translate' ], - function ($, alert) { + function($, alert) { 'use strict'; var videoRegister = { @@ -21,7 +21,7 @@ define( * @param {String} api * @returns {bool} */ - isRegistered: function (api) { + isRegistered: function(api) { return this._register[api] !== undefined; }, @@ -31,7 +31,7 @@ define( * @param {String} api * @returns {bool} */ - isLoaded: function (api) { + isLoaded: function(api) { return this._register[api] !== undefined && this._register[api] === true; }, @@ -41,7 +41,7 @@ define( * @param {String} api * @param {bool} loaded */ - register: function (api, loaded) { + register: function(api, loaded) { loaded = loaded || false; this._register[api] = loaded; } @@ -53,22 +53,22 @@ define( /** * @private */ - _create: function () { + _create: function() { switch (this.element.data('type')) { - case 'youtube': - this.element.videoYoutube(); - this._player = this.element.data('mageVideoYoutube'); + case 'youtube': + this.element.videoYoutube(); + this._player = this.element.data('mageVideoYoutube'); break; - case 'vimeo': - this.element.videoVimeo(); - this._player = this.element.data('mageVideoVimeo'); + case 'vimeo': + this.element.videoVimeo(); + this._player = this.element.data('mageVideoVimeo'); break; - case 'cloudinary': - this.element.videoCloudinary(); - this._player = this.element.data('mageVideoCloudinary'); + case 'cloudinary': + this.element.videoCloudinary(); + this._player = this.element.data('mageVideoCloudinary'); break; - default: + default: throw { name: $.mage.__('Video Error'), message: $.mage.__('Unknown video type'), @@ -76,10 +76,10 @@ define( /** * Return string */ - toString: function () { + toString: function() { return this.name + ': ' + this.message; } - }; + }; } }, @@ -88,7 +88,7 @@ define( * * @private */ - _initialize: function () { + _initialize: function() { this._params = this.element.data('params') || {}; this._code = this.element.data('code'); this._width = this.element.data('width'); @@ -110,35 +110,35 @@ define( /** * Abstract play command */ - play: function () { + play: function() { this._player.play(); }, /** * Abstract pause command */ - pause: function () { + pause: function() { this._player.pause(); }, /** * Abstract stop command */ - stop: function () { + stop: function() { this._player.stop(); }, /** * Abstract playing command */ - playing: function () { + playing: function() { return this._player.playing(); }, /** * Abstract destroying command */ - destroy: function () { + destroy: function() { this._player.destroy(); }, @@ -147,7 +147,7 @@ define( * * @private */ - _calculateRatio: function () { + _calculateRatio: function() { if (!this._responsive) { return; } @@ -164,7 +164,7 @@ define( * * @private */ - _create: function () { + _create: function() { var self = this; this._initialize(); @@ -179,7 +179,7 @@ define( * * @private */ - 'youtubeapiready': function () { + 'youtubeapiready': function() { var host = 'https://www.youtube.com'; if (self.useYoutubeNocookie) { @@ -207,21 +207,20 @@ define( /** * @private */ - 'onReady': function onPlayerReady() - { + 'onReady': function onPlayerReady() { self._player.getDuration(); }, /** * State change flag init */ - onStateChange: function (data) { + onStateChange: function(data) { switch (window.parseInt(data.data, 10)) { - case 1: - self._playing = true; + case 1: + self._playing = true; break; - default: - self._playing = false; + default: + self._playing = false; break; } @@ -243,7 +242,7 @@ define( * * @private */ - _loadApi: function () { + _loadApi: function() { var element, scriptTag; @@ -266,7 +265,7 @@ define( /** * Trigger youtube api ready event */ - window.onYouTubeIframeAPIReady = function () { + window.onYouTubeIframeAPIReady = function() { $(window).trigger('youtubeapiready'); videoRegister.register('youtube', true); }; @@ -275,7 +274,7 @@ define( /** * Play command for Youtube */ - play: function () { + play: function() { this._player.playVideo(); this._playing = true; }, @@ -283,7 +282,7 @@ define( /** * Pause command for Youtube */ - pause: function () { + pause: function() { this._player.pauseVideo(); this._playing = false; }, @@ -291,7 +290,7 @@ define( /** * Stop command for Youtube */ - stop: function () { + stop: function() { this._player.stopVideo(); this._playing = false; }, @@ -299,7 +298,7 @@ define( /** * Playing command for Youtube */ - playing: function () { + playing: function() { return this._playing; }, @@ -308,7 +307,7 @@ define( * * @private */ - destroy: function () { + destroy: function() { this.stop(); this._player.destroy(); } @@ -323,7 +322,7 @@ define( * * @private */ - _create: function () { + _create: function() { var timestamp, src, additionalParams; @@ -361,7 +360,7 @@ define( * * @private */ - _create: function () { + _create: function() { this._initialize(); this.element.append( @@ -374,13 +373,13 @@ define( .attr('src', this._videoSrc.replace(/(^\w+:|^)/, '')) .on( "loadstart", - function () { + function() { $('body').loader('show'); } ) .on( "load", - function () { + function() { $('body').loader('hide'); } ) @@ -439,12 +438,12 @@ define( /** * @private */ - _init: function () { + _init: function() { this.element.on(this._START_UPDATE_INFORMATION_TRIGGER, $.proxy(this._onRequestHandler, this)); this.element.on(this._ERROR_UPDATE_INFORMATION_TRIGGER, $.proxy(this._onVideoInvalid, this)); this.element.on( this._FINISH_UPDATE_INFORMATION_TRIGGER, $.proxy( - function () { + function() { this._currentVideoUrl = null; }, this ) @@ -455,7 +454,7 @@ define( /** * @private */ - _onUrlValidateHandler: function (event, callback, forceVideo) { + _onUrlValidateHandler: function(event, callback, forceVideo) { var url = this.element.val(), videoInfo; @@ -471,7 +470,7 @@ define( /** * @private */ - _onRequestHandler: function () { + _onRequestHandler: function() { var url = this.element.val(), self = this, videoInfo, @@ -508,8 +507,7 @@ define( * @param {Object} data * @private */ - function _onYouTubeLoaded(data) - { + function _onYouTubeLoaded(data) { var tmp, uploadedFormatted, respData, @@ -520,7 +518,7 @@ define( * * @returns {String} */ - createErrorMessage = function () { + createErrorMessage = function() { var error = data.error, errors = error.errors, i, @@ -580,8 +578,7 @@ define( /** * @private */ - function _onVimeoLoaded(data) - { + function _onVimeoLoaded(data) { var tmp, respData; @@ -610,23 +607,31 @@ define( /** * @private */ - function _onCloudinaryLoaded(data) - { + function _onCloudinaryLoaded(data) { var tmp, respData, context, thumbnail, thumbnail_bytes; + try { + data = JSON.parse(data); + } catch (e) { + this._onRequestError($.mage.__('Video not found')); + return null; + } + if (data.length < 1) { this._onRequestError($.mage.__('Video not found')); return null; } + if (data.error) { this._onRequestError($.mage.__('Video not found')); console.error(data.message); return null; } + tmp = data.data; context = (tmp.context && tmp.context.custom) ? tmp.context.custom : {}; @@ -665,69 +670,63 @@ define( }, $.proxy(_onYouTubeLoaded, self) ).fail( - function () { + function() { self._onRequestError('Video not found'); } ); } else if (type === 'vimeo') { - $.ajax( - { - url: 'https://www.vimeo.com/api/v2/video/' + id + '.json', - dataType: 'jsonp', - data: { - format: 'json' - }, - timeout: 5000, - success: $.proxy(_onVimeoLoaded, self), + $.ajax({ + url: 'https://www.vimeo.com/api/v2/video/' + id + '.json', + dataType: 'jsonp', + data: { + format: 'json' + }, + timeout: 5000, + success: $.proxy(_onVimeoLoaded, self), - /** - * @private - */ - error: function () { - self._onRequestError($.mage.__('Video not found')); - } + /** + * @private + */ + error: function() { + self._onRequestError($.mage.__('Video not found')); } - ); + }); } else if (type === 'cloudinary') { - $.ajax( - { - method: "GET", - url: '/rest/V1/cloudinary/resources/video', - dataType: 'json', - data: { - id: videoInfo.id - }, - timeout: 5000, - success: $.proxy(_onCloudinaryLoaded, self), + $.ajax({ + method: "GET", + url: '/rest/V1/cloudinary/resources/video', + dataType: 'json', + data: { + id: videoInfo.id + }, + timeout: 5000, + success: $.proxy(_onCloudinaryLoaded, self), - /** - * @private - */ - error: function () { - self._onRequestError($.mage.__('Video not found')); - } + /** + * @private + */ + error: function() { + self._onRequestError($.mage.__('Video not found')); } - ); + }); } }, /** * @private */ - _onVideoInvalid: function (event, data) { + _onVideoInvalid: function(event, data) { this._videoInformation = null; this.element.val(''); - alert( - { - content: 'Error: "' + data + '"' - } - ); + alert({ + content: 'Error: "' + data + '"' + }); }, /** * @private */ - _onRequestError: function (error) { + _onRequestError: function(error) { this.element.trigger(this._ERROR_UPDATE_INFORMATION_TRIGGER, error); this.element.trigger(this._FINISH_UPDATE_INFORMATION_TRIGGER, false); this._currentVideoUrl = null; @@ -736,7 +735,7 @@ define( /** * @private */ - _formatYoutubeDuration: function (duration) { + _formatYoutubeDuration: function(duration) { var match = duration.match(/PT(\d+H)?(\d+M)?(\d+S)?/), hours = parseInt(match[1], 10) || 0, minutes = parseInt(match[2], 10) || 0, @@ -748,14 +747,14 @@ define( /** * @private */ - _formatVimeoDuration: function (seconds) { + _formatVimeoDuration: function(seconds) { return (new Date(seconds * 1000)).toUTCString().match(/(\d\d:\d\d:\d\d)/)[0]; }, /** * @private */ - _parseHref: function (href) { + _parseHref: function(href) { var a = document.createElement('a'); a.href = href; @@ -766,7 +765,7 @@ define( /** * @private */ - _baseName: function (str) { + _baseName: function(str) { var base = new String(str).substring(str.lastIndexOf('/') + 1); if (base.lastIndexOf(".") != -1) { base = base.substring(0, base.lastIndexOf(".")); @@ -777,7 +776,7 @@ define( /** * @private */ - _fileExtension: function (str) { + _fileExtension: function(str) { var re = /(?:\.([^.]+))?$/; return re.exec(str)[1]; }, @@ -785,7 +784,7 @@ define( /** * @private */ - _validateURL: function (href, forceVideo) { + _validateURL: function(href, forceVideo) { var id, type, ampersandPosition,