Skip to content

Commit

Permalink
Merge branch 'main' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
fballiano committed Jul 19, 2023
2 parents 5f3e3b0 + 0cb5439 commit 94d69b5
Show file tree
Hide file tree
Showing 22 changed files with 124 additions and 67 deletions.
1 change: 1 addition & 0 deletions .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@
## Add default Expires header
## http://developer.yahoo.com/performance/rules.html#expires
ExpiresActive On
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function _prepareLayout()
$browseConfig = $this->getUploader()->getButtonConfig();
$browseConfig
->setAttributes([
'accept' => $browseConfig->getMimeTypesByExtensions('gif, png, jpeg, jpg')
'accept' => $browseConfig->getMimeTypesByExtensions(Varien_Io_File::ALLOWED_IMAGES_EXTENSIONS)
]);

Mage::dispatchEvent('catalog_product_gallery_prepare_layout', ['block' => $this]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Mage_Adminhtml_Model_System_Config_Backend_Image extends Mage_Adminhtml_Mo
*/
protected function _getAllowedExtensions()
{
return ['jpg', 'jpeg', 'gif', 'png'];
return Varien_Io_File::ALLOWED_IMAGES_EXTENSIONS;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function uploadAction()
{
try {
$uploader = new Mage_Core_Model_File_Uploader('image');
$uploader->setAllowedExtensions(['jpg','jpeg','gif','png']);
$uploader->setAllowedExtensions(Varien_Io_File::ALLOWED_IMAGES_EXTENSIONS);
$uploader->addValidateCallback(
'catalog_product_image',
Mage::helper('catalog/image'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,9 @@ public function viewfileAction()
case 'jpg':
$contentType = 'image/jpeg';
break;
case 'webp':
$contentType = 'image/webp';
break;
case 'png':
$contentType = 'image/png';
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ abstract class Mage_Catalog_Model_Api2_Product_Image_Rest extends Mage_Catalog_M
* @var array
*/
protected $_mimeTypes = [
'image/webp' => 'webp',
'image/jpg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,51 @@
class Mage_Catalog_Model_Category_Attribute_Backend_Image extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
/**
* Save uploaded file and set its name to category
* @return array
*/
public function getAllowedExtensions(): array
{
return Varien_Io_File::ALLOWED_IMAGES_EXTENSIONS;
}

/**
* Save uploaded file and set its name to category attribute
* @param Varien_Object $object
* @return $this
*/
public function afterSave($object)
{
$value = $object->getData($this->getAttribute()->getName());
if (empty($value) && empty($_FILES)) {
return $this;
}
$name = $this->getAttribute()->getName();
$value = $object->getData($name);

if (is_array($value) && !empty($value['delete'])) {
$object->setData($this->getAttribute()->getName(), '');
$this->getAttribute()->getEntity()
->saveAttribute($object, $this->getAttribute()->getName());
$object->setData($name, '');
$this->getAttribute()->getEntity()->saveAttribute($object, $name);
return $this;
}

$path = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category' . DS;
if (!empty($_FILES[$name])) {
try {
$validator = Mage::getModel('core/file_validator_image');
$uploader = Mage::getModel('core/file_uploader', $name);
$uploader->setAllowedExtensions($this->getAllowedExtensions());
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
$uploader->addValidateCallback(Mage_Core_Model_File_Validator_Image::NAME, $validator, 'validate');
$uploader->save(Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category');

try {
$validator = Mage::getModel('core/file_validator_image');
$uploader = new Mage_Core_Model_File_Uploader($this->getAttribute()->getName());
$uploader->setAllowedExtensions(['jpg','jpeg','gif','png']);
$uploader->setAllowRenameFiles(true);
$uploader->addValidateCallback(Mage_Core_Model_File_Validator_Image::NAME, $validator, 'validate');
$result = $uploader->save($path);

$object->setData($this->getAttribute()->getName(), $result['file']);
$this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());
} catch (Exception $e) {
if ($e->getCode() != UPLOAD_ERR_NO_FILE) {
Mage::logException($e);
$fileName = $uploader->getUploadedFileName();
if ($fileName) {
$object->setData($name, $fileName);
$this->getAttribute()->getEntity()->saveAttribute($object, $name);
}
} catch (Exception $e) {
if ($e->getCode() != UPLOAD_ERR_NO_FILE) {
Mage::logException($e);
}
}
}

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,7 @@ public function addImage(
Mage::dispatchEvent('catalog_product_media_add_image', ['product' => $product, 'image' => $file]);

$pathinfo = pathinfo($file);
$imgExtensions = ['jpg','jpeg','gif','png'];
if (!isset($pathinfo['extension']) || !in_array(strtolower($pathinfo['extension']), $imgExtensions)) {
if (!isset($pathinfo['extension']) || !in_array(strtolower($pathinfo['extension']), Varien_Io_File::ALLOWED_IMAGES_EXTENSIONS)) {
Mage::throwException(Mage::helper('catalog')->__('Invalid image file type.'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Mage_Catalog_Model_Product_Attribute_Media_Api extends Mage_Catalog_Model_
* @var array
*/
protected $_mimeTypes = [
'image/webp' => 'webp',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
'image/png' => 'png'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,49 @@
class Mage_Catalog_Model_Resource_Product_Attribute_Backend_Image extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
/**
* @return array
*/
public function getAllowedExtensions(): array
{
return Varien_Io_File::ALLOWED_IMAGES_EXTENSIONS;
}

/**
* Save uploaded file and set its name to product attribute
* @param Varien_Object $object
* @return $this
*/
public function afterSave($object)
{
$value = $object->getData($this->getAttribute()->getName());
$name = $this->getAttribute()->getName();
$value = $object->getData($name);

if (is_array($value) && !empty($value['delete'])) {
$object->setData($this->getAttribute()->getName(), '');
$this->getAttribute()->getEntity()
->saveAttribute($object, $this->getAttribute()->getName());
$object->setData($name, '');
$this->getAttribute()->getEntity()->saveAttribute($object, $name);
return $this;
}

try {
$validator = Mage::getModel('core/file_validator_image');
$uploader = new Mage_Core_Model_File_Uploader($this->getAttribute()->getName());
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$uploader->addValidateCallback(Mage_Core_Model_File_Validator_Image::NAME, $validator, 'validate');
$uploader->save(Mage::getBaseDir('media') . '/catalog/product');
if (!empty($_FILES[$name])) {
try {
$validator = Mage::getModel('core/file_validator_image');
$uploader = Mage::getModel('core/file_uploader', $name);
$uploader->setAllowedExtensions($this->getAllowedExtensions());
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$uploader->addValidateCallback(Mage_Core_Model_File_Validator_Image::NAME, $validator, 'validate');
$uploader->save(Mage::getBaseDir('media') . DS . 'catalog' . DS . 'product');

$fileName = $uploader->getUploadedFileName();
if ($fileName) {
$object->setData($this->getAttribute()->getName(), $fileName);
$this->getAttribute()->getEntity()
->saveAttribute($object, $this->getAttribute()->getName());
$fileName = $uploader->getUploadedFileName();
if ($fileName) {
$object->setData($name, $fileName);
$this->getAttribute()->getEntity()->saveAttribute($object, $name);
}
} catch (Exception $e) {
if ($e->getCode() != UPLOAD_ERR_NO_FILE) {
Mage::logException($e);
}
}
} catch (Exception $e) {
return $this;
}

return $this;
Expand Down
2 changes: 2 additions & 0 deletions app/code/core/Mage/Cms/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@
<jpeg>1</jpeg>
<png>1</png>
<gif>1</gif>
<webp>1</webp>
</allowed>
<image_allowed>
<jpg>1</jpg>
<jpeg>1</jpeg>
<png>1</png>
<gif>1</gif>
<webp>1</webp>
</image_allowed>
<media_allowed>
<flv>1</flv>
Expand Down
5 changes: 5 additions & 0 deletions app/code/core/Mage/Core/Model/File/Validator/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Mage_Core_Model_File_Validator_Image
public const NAME = "isImage";

protected $_allowedImageTypes = [
IMAGETYPE_WEBP,
IMAGETYPE_JPEG,
IMAGETYPE_GIF,
IMAGETYPE_JPEG2000,
Expand All @@ -42,6 +43,7 @@ class Mage_Core_Model_File_Validator_Image
public function setAllowedImageTypes(array $imageFileExtensions = [])
{
$map = [
'webp' => [IMAGETYPE_WEBP],
'tif' => [IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM],
'tiff' => [IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM],
'jpg' => [IMAGETYPE_JPEG, IMAGETYPE_JPEG2000],
Expand Down Expand Up @@ -121,6 +123,9 @@ public function validate($filePath)
case IMAGETYPE_JPEG:
imagejpeg($img, $filePath, $imageQuality);
break;
case IMAGETYPE_WEBP:
imagewebp($img, $filePath, $imageQuality);
break;
case IMAGETYPE_PNG:
imagepng($img, $filePath);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function getLinkData()
*/
public function getPriceValue($value)
{
return number_format($value, 2, null, '');
return number_format($value ?? 0, 2, null, '');
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/code/core/Mage/Eav/Model/Attribute/Data/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ protected function _validateByRules($value)
1 => 'gif',
2 => 'jpg',
3 => 'png',
18 => 'webp'
];

if (!isset($allowImageTypes[$imageProp[2]])) {
Expand Down
1 change: 1 addition & 0 deletions app/code/core/Mage/ImportExport/Model/Import/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Mage_ImportExport_Model_Import_Uploader extends Mage_Core_Model_File_Uploa
protected $_tmpDir = '';
protected $_destDir = '';
protected $_allowedMimeTypes = [
'webp' => 'image/webp',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
Expand Down
7 changes: 7 additions & 0 deletions app/code/core/Mage/Media/Model/File/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public function getImage(Mage_Media_Model_Image $object)
$resource = imagecreatefromjpeg($object->getFilePath());
break;

case 'webp':
$resource = imagecreatefromwebp($object->getFilePath());
break;

case 'gif':
$resource = imagecreatefromgif($object->getFilePath());
break;
Expand Down Expand Up @@ -176,6 +180,9 @@ public function saveAs(Mage_Media_Model_Image $object, $extension = null)
case 'jpeg':
$result = imagejpeg($object->getTmpImage(), $object->getFilePath(true), 80);
break;
case 'webp':
$result = imagewebp($object->getTmpImage(), $object->getFilePath(true), 80);
break;
case 'gif':
$result = imagegif($object->getTmpImage(), $object->getFilePath(true));
break;
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/SalesRule/Model/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,10 @@ public function process(Mage_Sales_Model_Quote_Item_Abstract $item)
$discountAmount = $result->getDiscountAmount();
$baseDiscountAmount = $result->getBaseDiscountAmount();

$percentKey = $item->getDiscountPercent();
/**
* Process "delta" rounding
*/
$percentKey = (string) $item->getDiscountPercent();
if ($percentKey) {
$delta = $this->_roundingDeltas[$percentKey] ?? 0;
$baseDelta = $this->_baseRoundingDeltas[$percentKey] ?? 0;
Expand Down
1 change: 1 addition & 0 deletions app/code/core/Mage/Uploader/Helper/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ class Mage_Uploader_Helper_File extends Mage_Core_Helper_Abstract
'xjisp' => 'application/vnd.jisp',
'xjlt' => 'application/vnd.hp-jlyt',
'xjoda' => 'application/vnd.joost.joda-archive',
'xwebp' => 'image/webp',
'xjpe' => 'image/jpeg',
'xjpeg' => 'image/jpeg',
'xjpg' => 'image/jpeg',
Expand Down
Loading

0 comments on commit 94d69b5

Please sign in to comment.