Skip to content

Commit

Permalink
Merge pull request #55 from cloudinary/v3.0.2
Browse files Browse the repository at this point in the history
v3.0.2: Applied Magento 1 coding standards.
  • Loading branch information
Pniel (Pini) Cohen authored Sep 8, 2019
2 parents 64a157d + d69fb2a commit 0cb70de
Show file tree
Hide file tree
Showing 48 changed files with 287 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ protected function _prepareLayout()
$this->setChild(
'cloudinary_ml_button',
$this->getLayout()->createBlock('adminhtml/widget_button')
->addData(array(
->addData(
array(
// Workaround for IE9
'before_html' => sprintf(
'<div style="display:inline-block;" id="%s">',
Expand All @@ -53,7 +54,8 @@ protected function _prepareLayout()
'id' => $this->getElementId(self::DEFAULT_CLD_ML_BUTTON_ID_SUFFIX . '_button'),
'label' => Mage::helper('uploader')->__('Add From Cloudinary...'),
'type' => 'button',
))
)
)
);
}

Expand Down Expand Up @@ -82,7 +84,9 @@ public function getCloudinaryMediaLibraryWidgetOptions($multiple = true, $refres
if (!($cloudinaryMLoptions = Mage::helper('cloudinary_cloudinary/MediaLibraryHelper')->getCloudinaryMLOptions($multiple, $refresh))) {
return null;
}
return Mage::helper('core')->jsonEncode(array(

return Mage::helper('core')->jsonEncode(
array(
'htmlId' => $this->getHtmlId(),
'cldMLid' => self::DEFAULT_CLD_ML_BUTTON_ID_SUFFIX . '_' . $this->getHtmlId(),
'imageUploaderUrl' => $this->getCldImageUploaderUrl(),
Expand All @@ -95,7 +99,8 @@ public function getCloudinaryMediaLibraryWidgetOptions($multiple = true, $refres
'addTmpExtension' => $this->getAddTmpExtension(),
'cloudinaryMLoptions' => $cloudinaryMLoptions,
'cloudinaryMLshowOptions' => Mage::helper('cloudinary_cloudinary/MediaLibraryHelper')->getCloudinaryMLshowOptions('image'),
));
)
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function getMigrationInfo()
if (!$this->hasData('migration_info')) {
$this->setData('migration_info', new Varien_Object($this->_migrationTask->getInfo()));
}

return $this->getData('migration_info');
}

Expand All @@ -99,12 +100,14 @@ public function getMigrateButton()

return $this->getLayout()
->createBlock('adminhtml/widget_button')
->setData(array(
->setData(
array(
'id' => 'cloudinary_migration_start_' . $type,
'label' => $this->helper('adminhtml')->__('Start Migration'),
'disabled' => $type === Cloudinary_Cloudinary_Model_Migration::UPLOAD_MIGRATION_TYPE && $this->allImagesSynced(),
'onclick' => 'openCloudinaryMigrationPopup(\''.$type.'\');'
))
)
)
->toHtml();
}

Expand All @@ -122,12 +125,14 @@ public function getClearErrorsButton()
private function _makeButton($label, $action, $disabled = false)
{
$button = $this->getLayout()->createBlock('adminhtml/widget_button')
->setData(array(
->setData(
array(
'id' => 'cloudinary_migration_start_' . $action,
'label' => $this->helper('adminhtml')->__($label),
'disabled' => $disabled,
'onclick' => "setLocation('{$this->getUrl(sprintf('*/cloudinary/%s/type/%s', $action, $this->getType()))}')"
));
)
);

return $button->toHtml();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ protected function _prepareForm()

$fieldset = $form->addFieldset('display', array('class' => 'fieldset-wide'));

$fieldset->addField('password', 'password', array(
$fieldset->addField(
'password', 'password', array(
'name' => 'password',
'label' => Mage::helper('cloudinary_cloudinary')->__('Enter your admin password to confirm reset')
));
)
);

return parent::_prepareForm();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ protected function _prepareLayout()
$this->setChild(
'cloudinary_ml_button',
$this->getLayout()->createBlock('adminhtml/widget_button')
->addData(array(
->addData(
array(
// Workaround for IE9
'before_html' => sprintf(
'<div style="display:inline-block;" id="%s">',
Expand All @@ -42,7 +43,8 @@ protected function _prepareLayout()
'id' => $this->getElementId(self::DEFAULT_CLD_ML_BUTTON_ID_SUFFIX . '_button'),
'label' => Mage::helper('uploader')->__('Add From Cloudinary...'),
'type' => 'button',
))
)
)
);
}

Expand Down Expand Up @@ -71,7 +73,9 @@ public function getCloudinaryMediaLibraryWidgetOptions($multiple = true, $refres
if (!($cloudinaryMLoptions = Mage::helper('cloudinary_cloudinary/MediaLibraryHelper')->getCloudinaryMLOptions($multiple, $refresh))) {
return null;
}
return Mage::helper('core')->jsonEncode(array(

return Mage::helper('core')->jsonEncode(
array(
'htmlId' => $this->getHtmlId(),
'cldMLid' => self::DEFAULT_CLD_ML_BUTTON_ID_SUFFIX . '_' . $this->getHtmlId(),
'imageUploaderUrl' => $this->getCldImageUploaderUrl(),
Expand All @@ -84,7 +88,8 @@ public function getCloudinaryMediaLibraryWidgetOptions($multiple = true, $refres
'addTmpExtension' => $this->getAddTmpExtension(),
'cloudinaryMLoptions' => $cloudinaryMLoptions,
'cloudinaryMLshowOptions' => Mage::helper('cloudinary_cloudinary/MediaLibraryHelper')->getCloudinaryMLshowOptions('image'),
));
)
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ protected function _prepareLayout()
$this->setChild(
'cloudinary_ml_button',
$this->getLayout()->createBlock('adminhtml/widget_button')
->addData(array(
->addData(
array(
// Workaround for IE9
'before_html' => sprintf(
'<div style="display:inline-block;" id="%s">',
Expand All @@ -42,7 +43,8 @@ protected function _prepareLayout()
'id' => $this->getElementId(self::DEFAULT_CLD_ML_BUTTON_ID_SUFFIX . '_button'),
'label' => Mage::helper('uploader')->__('Add From Cloudinary...'),
'type' => 'button',
))
)
)
);
}

Expand Down Expand Up @@ -71,7 +73,9 @@ public function getCloudinaryMediaLibraryWidgetOptions($multiple = false, $refre
if (!($cloudinaryMLoptions = Mage::helper('cloudinary_cloudinary/MediaLibraryHelper')->getCloudinaryMLOptions($multiple, $refresh))) {
return null;
}
return Mage::helper('core')->jsonEncode(array(

return Mage::helper('core')->jsonEncode(
array(
'htmlId' => $this->getHtmlId(),
'cldMLid' => self::DEFAULT_CLD_ML_BUTTON_ID_SUFFIX . '_' . $this->getHtmlId(),
'imageUploaderUrl' => $this->getCldImageUploaderUrl(),
Expand All @@ -84,7 +88,8 @@ public function getCloudinaryMediaLibraryWidgetOptions($multiple = false, $refre
'addTmpExtension' => $this->getAddTmpExtension(),
'cloudinaryMLoptions' => $cloudinaryMLoptions,
'cloudinaryMLshowOptions' => Mage::helper('cloudinary_cloudinary/MediaLibraryHelper')->getCloudinaryMLshowOptions('image'),
));
)
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ private function _registerCloudinaryExtensionAutoloader()
{
spl_autoload_register(
function ($className) {
if(
strpos($className, Cloudinary_Cloudinary_Helper_Autoloader::CLOUDINARY_EXTENSION_LIB_PATH . '\\') === 0 ||
if(strpos($className, Cloudinary_Cloudinary_Helper_Autoloader::CLOUDINARY_EXTENSION_LIB_PATH . '\\') === 0 ||
strpos($className, Cloudinary_Cloudinary_Helper_Autoloader::CLOUDINARY_LIB_PATH . '\\') === 0
) {
include_once preg_replace(Cloudinary_Cloudinary_Helper_Autoloader::CONVERT_CLASS_TO_PATH_REGEX, '/', $className) . '.php';
Expand Down
30 changes: 20 additions & 10 deletions app/code/community/Cloudinary/Cloudinary/Helper/BatchDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,27 @@ public function downloadImages()
$this->_info["iteration"]++;
$this->log('Iteration #' . $this->_info["iteration"]);
$this->migrationTask->setBatchCount($this->_info["iteration"]);
$this->saveInfo(array(
$this->saveInfo(
array(
"iteration" => $this->_info["iteration"],
"resources_count" => 0,
"resources_processed" => 0,
"resources_downloaded" => 0,
"resources_skipped" => 0,
"resources_failed" => 0,
));
)
);

$response = $this->getResources($this->_info["next_cursor"]);
$response->setResourcesCount(count($response->getResources()));
$this->saveInfo(array(
$this->saveInfo(
array(
"next_cursor" => $response->getNextCursor(),
"more_expected" => ($response->getNextCursor())? true : false,
"resources_count" => $this->_info["resources_count"] + $response->getResourcesCount(),
"resources_count_total" => $this->_info["resources_count_total"] + $response->getResourcesCount(),
));
)
);
if ($response->getResourcesCount() > 0) {
$this->log('Found ' . $response->getResourcesCount() . ' image(s) to sync on this round. ' . (($response->getNextCursor()) ? '*More Rounds Expected*' : '*Last Round*'));
foreach ($response->getResources() as $i => &$resource) {
Expand Down Expand Up @@ -174,14 +178,16 @@ public function downloadImages()
$res = Cloudinary_Cloudinary_Helper_Data::curlGetContents($remoteFileUrl);
if (!$res || $res->getError() || empty(($image = $res->getBody()))) {
throw new Mage_Core_Exception(
__('The preview image information is unavailable. Check your connection and try again.')
);
__('The preview image information is unavailable. Check your connection and try again.')
);
}

$this->log('=== [Processing] Saving...');
Mage::getSingleton('core/file_storage_file')->saveFile(array('filename' => $localFileName, 'content' => $image), true);
if (!@file_exists($localFilePath)) {
throw new Mage_Core_Exception(__("Image not saved."));
}

$this->log('=== [Processing] Saved.');
$this->saveInfo(array("resources_downloaded" => $this->_info["resources_downloaded"]+1, "resources_downloaded_total" => $this->_info["resources_downloaded_total"]+1));
}
Expand Down Expand Up @@ -228,13 +234,15 @@ public function downloadImages()
*/
private function getResources($nextCursor = null)
{
$response = $this->api->resources(array(
$response = $this->api->resources(
array(
"resource_type" => 'image',
"type" => "upload",
"prefix" => 'media' . DIRECTORY_SEPARATOR,
"max_results" => self::API_REQUEST_MAX_RESULTS,
"next_cursor" => $nextCursor,
));
)
);
$this->_rateLimitResetAt = $response->rate_limit_reset_at;
$this->_rateLimitAllowed = $response->rate_limit_allowed;
$this->_rateLimitRemaining = $response->rate_limit_remaining;
Expand All @@ -255,6 +263,7 @@ private function log($message, $type = 'notice')
$this->logger->notice($message);
break;
}

return $this;
}

Expand All @@ -268,7 +277,7 @@ private function log($message, $type = 'notice')
private function validateRemoteFileExtensions($filePath)
{
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
if (!in_array($extension, ['jpg','jpeg','gif','png'])) {
if (!in_array($extension, array('jpg','jpeg','gif','png'))) {
throw new Mage_Core_Exception(__('Disallowed file type.'));
}
}
Expand All @@ -290,13 +299,14 @@ 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 Mage_Core_Exception(__(self::CREDENTIALS_CHECK_FAILED));
}
}

private function saveInfo(array $info = [])
private function saveInfo(array $info = array())
{
$this->_info = array_merge($this->_info, $info);
$this->migrationTask->setInfo($this->_info)->save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ public function getStorageRoot()
if (!Mage::getModel('cloudinary_cloudinary/configuration')->isEnabled()) {
return parent::getStorageRoot();
}

if (!$this->_storageRoot) {
$this->_storageRoot = Mage::getConfig()->getOptions()->getMediaDir()
. DS . Mage_Cms_Model_Wysiwyg_Config::IMAGE_DIRECTORY . DS;
}

return $this->_storageRoot;
}

Expand All @@ -33,13 +35,15 @@ public function getCurrentUrl()
if (!Mage::getModel('cloudinary_cloudinary/configuration')->isEnabled()) {
return parent::getCurrentUrl();
}

if (!$this->_currentUrl) {
$mediaPath = Mage::getConfig()->getOptions()->getMediaDir();
$path = str_replace($mediaPath, '', $this->getCurrentPath());
$path = trim($path, DS);
$this->_currentUrl = Mage::app()->getStore($this->_storeId)->getBaseUrl('media') .
$this->convertPathToUrl($path) . '/';
}

return $this->_currentUrl;
}
}
2 changes: 1 addition & 1 deletion app/code/community/Cloudinary/Cloudinary/Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private function doFlatten($key, $value, array &$output)
$this->doFlatten(sprintf('%s/%s', $key, $childKey), $childValue, $output);
}
} else {
$output[substr($key, 0, -6)] = $value;
$output[substr($key, 0, -6)] = $value;
}
}
}
6 changes: 4 additions & 2 deletions app/code/community/Cloudinary/Cloudinary/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public static function curlGetContents($url)
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$err = curl_error($ch);
curl_close($ch);
return new Varien_Object(array(
return new Varien_Object(
array(
"code" => $httpCode,
"body" => $result,
"error" => $err
));
)
);
}
}
1 change: 1 addition & 0 deletions app/code/community/Cloudinary/Cloudinary/Helper/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public function __toString()
if (!$this->_configuration->isEnabled()) {
return parent::__toString();
}

$image = $this->_imageFactory->build(
$this->_getRequestedImageFile(),
function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,21 @@ public function getCloudinaryMLOptions($multiple = false, $refresh = true)
if (($this->credentials["username"] = $this->configuration->getAutomaticLoginUser())) {
$this->cloudinaryMLoptions["timestamp"] = $this->timestamp;
$this->cloudinaryMLoptions["username"] = $this->credentials["username"];
$this->cloudinaryMLoptions["signature"] = $this->signature = hash('sha256', urldecode(http_build_query([
'cloud_name' => $this->credentials['cloud_name'],
'timestamp' => $this->timestamp,
'username' => $this->credentials['username'],
])) . $this->credentials['api_secret']);
$this->cloudinaryMLoptions["signature"] = $this->signature = hash(
'sha256', urldecode(
http_build_query(
array(
'cloud_name' => $this->credentials['cloud_name'],
'timestamp' => $this->timestamp,
'username' => $this->credentials['username'],
)
)
) . $this->credentials['api_secret']
);
}
}
}

if ($this->cloudinaryMLoptions) {
$this->cloudinaryMLoptions['multiple'] = $multiple;
}
Expand All @@ -92,13 +99,14 @@ public function getCloudinaryMLOptions($multiple = false, $refresh = true)
*/
public function getCloudinaryMLshowOptions($resourceType = null, $path = "")
{
$options = [];
$options = array();
if ($resourceType || $resourceType) {
$options["folder"] = array(
"path" => $path,
"resource_type" => $resourceType,
);
}

return $options;
}
}
Loading

0 comments on commit 0cb70de

Please sign in to comment.