Skip to content

Commit

Permalink
Merge pull request #41 from cloudinary/v2.9.5
Browse files Browse the repository at this point in the history
V2.9.5
  • Loading branch information
Pini authored Dec 31, 2018
2 parents 3188ff0 + b667cf4 commit e8c5db3
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,24 @@ public function getStorageRoot()
}
return $this->_storageRoot;
}

/**
* Return URL based on current selected directory or root directory for startup
*
* @return string
*/
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public function mediaDirective($construction)
if (ini_get('allow_url_fopen')) {
$image = $this->imageFactory->build(
$this->imagePath($construction),
function() use($construction) { return parent::mediaDirective($construction); }
function () use ($construction) {
return parent::mediaDirective($construction);
}
);

return $this->urlGenerator->generateFor($image);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,101 @@ public function __construct()
);
}

/**
* Return files
*
* @param string $path Parent directory path
* @param string $type Type of storage, e.g. image, media etc.
* @return Varien_Data_Collection_Filesystem
*/
public function getFilesCollection($path, $type = null)
{
if (!$this->_configuration->isEnabled()) {
return parent::getFilesCollection($path, $type);
}

if (Mage::helper('core/file_storage_database')->checkDbUsage()) {
$files = Mage::getModel('core/file_storage_database')->getDirectoryFiles($path);

$fileStorageModel = Mage::getModel('core/file_storage_file');
foreach ($files as $file) {
$fileStorageModel->saveFile($file);
}
}

$collection = $this->getCollection($path)
->setCollectDirs(false)
->setCollectFiles(true)
->setCollectRecursively(false)
->setOrder('mtime', Varien_Data_Collection::SORT_ORDER_ASC);

// Add files extension filter
if ($allowed = $this->getAllowedExtensions($type)) {
$collection->setFilesFilter('/\.(' . implode('|', $allowed). ')$/i');
}

$helper = $this->getHelper();

// prepare items
foreach ($collection as $item) {
$item->setId($helper->idEncode($item->getBasename()));
$item->setName($item->getBasename());
$item->setShortName($helper->getShortFilename($item->getBasename()));
$item->setUrl($helper->getCurrentUrl() . $item->getBasename());

if ($this->isImage($item->getBasename())) {
$thumbUrl = $this->getThumbnailUrl(
Mage_Core_Model_File_Uploader::getCorrectFileName($item->getFilename()),
true,
$item->getFilename()
);
// generate thumbnail "on the fly" if it does not exists
if (! $thumbUrl) {
$thumbUrl = Mage::getSingleton('adminhtml/url')->getUrl('*/*/thumbnail', array('file' => $item->getId()));
}

$size = @getimagesize($item->getFilename());

if (is_array($size)) {
$item->setWidth($size[0]);
$item->setHeight($size[1]);
}
} else {
$thumbUrl = Mage::getDesign()->getSkinBaseUrl() . self::THUMB_PLACEHOLDER_PATH_SUFFIX;
}

$item->setThumbUrl($thumbUrl);
}

return $collection;
}

/**
* @param string $filePath
* @param bool $checkFile
* @return string
*/
public function getThumbnailUrl($filePath, $checkFile = false)
public function getThumbnailUrl($filePath, $checkFile = false, $origFilePath = null)
{
$_origUrl = $origUrl = parent::getThumbnailUrl($filePath, $checkFile);

if (!$this->_configuration->isEnabled()) {
return $_origUrl;
}

if (!$_origUrl && !is_null($origFilePath)) {
$filePath = $origFilePath;
$origUrl = parent::getThumbnailUrl($filePath, $checkFile);
}

if (!$origUrl) {
return $_origUrl;
}

$image = $this->_imageFactory->build(
$filePath,
function() use($filePath, $checkFile) {
return parent::getThumbnailUrl($filePath, $checkFile);
function () use ($_origUrl) {
return (string) $_origUrl;
}
);

Expand All @@ -71,7 +155,7 @@ function() use($filePath, $checkFile) {
public function uploadFile($targetPath, $type = null)
{
if (!$this->_configuration->isEnabled()) {
return parent::uploadFile($targetPath, $type);
return parent::uploadFile($targetPath, $type);
}

$uploader = new Cloudinary_Cloudinary_Model_Cms_Uploader('image');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private function setStoreConfig($configPath, $value)
/**
* @return CloudinaryEnvironmentVariable
*/
private function getEnvironmentVariable()
public function getEnvironmentVariable()
{
if (is_null($this->environmentVariable)) {
if (Mage::registry('cloudinaryEnvironmentVariable')) {
Expand Down
73 changes: 3 additions & 70 deletions app/code/community/Cloudinary/Cloudinary/Model/Observer/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,6 @@ class Cloudinary_Cloudinary_Model_Observer_Config extends Mage_Core_Model_Abstra
const AUTO_UPLOAD_SETUP_FAIL_MESSAGE = 'error. Unable to setup auto upload mapping.';
const AUTO_UPLOAD_SETUP_SUCCESS_MESSAGE = 'auto upload mapping configured: %s';

/**
* @param Varien_Event_Observer $observer
*/
public function configSave(Varien_Event_Observer $observer)
{
$config = $observer->getEvent()->getObject();
if ($config->getSection() != self::CLOUDINARY_CONFIG_SECTION) {
return;
}

$data = Mage::helper('cloudinary_cloudinary/config')->flatten('cloudinary', $config->getGroups());
if ($data[self::ENABLED_FIELD] == '1') {
Mage::register('cloudinaryIsEnabled', true);
$this->validateEnvironmentVariable($data);
$this->logConfigChange($data);
} else {
Mage::register('cloudinaryIsEnabled', false);
}
}

/**
* @param Varien_Event_Observer $observer
*/
Expand All @@ -44,9 +24,10 @@ public function cloudinaryConfigChanged(Varien_Event_Observer $observer)
//Clear config cache before mapping
Mage::app()->getCacheInstance()->cleanType("config");
Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => "config"));
Mage::getConfig()->reinit();

if (!Mage::registry('cloudinaryIsEnabled')) {
return;
if (!Mage::getModel('cloudinary_cloudinary/configuration')->isEnabled()) {
return $this;
}

if (!$this->autoUploadRequestProcessor()->handle('media', Mage::getBaseUrl('media'), true)) {
Expand All @@ -60,54 +41,6 @@ public function cloudinaryConfigChanged(Varien_Event_Observer $observer)
}
}

/**
* @param array $data
*/
private function validateEnvironmentVariable(array $data)
{
$value = (string) $data[self::ENVIRONMENT_FIELD];
if (preg_match('/^\*+$/', $value)) {
$value = Mage::helper('core')->decrypt($value);
}
$credentialValidator = new CredentialValidator();
$environmentVariable = CloudinaryEnvironmentVariable::fromString($value);

if (!$credentialValidator->validate($environmentVariable->getCredentials())) {
Mage::register('cloudinaryEnvironmentVariableIsValid', false);
throw new Mage_Core_Exception(self::ERROR_WRONG_CREDENTIALS);
}
Mage::register('cloudinaryEnvironmentVariableIsValid', true);
}

/**
* @param array $data
*/
private function logConfigChange(array $data)
{
$data[self::ENVIRONMENT_FIELD] = md5($data[self::ENVIRONMENT_FIELD]);
Mage::getModel('cloudinary_cloudinary/logger')->notice(
sprintf(self::CONFIG_CHANGE_MESSAGE, $this->formatConfigData($data))
);
}

/**
* @param array $data
* @return string
*/
private function formatConfigData(array $data)
{
return implode(
', ',
array_map(
function ($key, $value) {
return sprintf('(%s: %s)', $key, $value);
},
array_keys($data),
array_values($data)
)
);
}

/**
* @return RequestProcessor
*/
Expand Down
Loading

0 comments on commit e8c5db3

Please sign in to comment.