magefan / module-blog

Magento 2 Blog Extension is a solution that helps you to create a blog on Magento and manage posts,categories, tags, authors, and comments.
https://magefan.com/magento2-blog-extension
Other
263 stars 135 forks source link

Post image upload is not working #600

Open gfrancisco-sh opened 3 weeks ago

gfrancisco-sh commented 3 weeks ago

Describe the bug It's not possible to upload image using upload button in admin. image

Preconditions:

To Reproduce Steps to reproduce the behavior:

  1. Go to Magefan -> Posts page and add or edit any post.
  2. Click on Upload button inside Display settings section
  3. Select a valid image (png, jpg)
  4. See error

Expected behavior Image is uploaded and visible when saved

Actual behavior Error message is shown: File validation failed

Screenshots image

Additional context I did a research about the issue and the problem is the \Magefan\Blog\Model\ImageUploader class is overriding \Magento\Catalog\Model\ImageUploader but is missing two variables in constructor (only $allowedMimeTypes is relevant for this isse)

to fix, I changed the constructor like this:

public function __construct(
        \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Psr\Log\LoggerInterface $logger,
        $baseTmpPath,
        $basePath,
        $allowedExtensions,
        array $allowedMimeTypes = [], //added missing variable
        Name $fileNameLookup = null //added missing variable
    ) {
        parent::__construct(
            $coreFileStorageDatabase,
            $filesystem,
            $uploaderFactory,
            $storeManager,
            $logger,
            $baseTmpPath,
            $basePath,
            $allowedExtensions,
            $allowedMimeTypes, //added to parent class constructor
            $fileNameLookup //added to parent class constructor
        );
        $this->filesystem = $filesystem;
    }

Then, I added the allowed mime types in di for this class:

<type name="Magefan\Blog\Model\ImageUploader">
        <arguments>
            <argument name="allowedMimeTypes" xsi:type="array">
                <item name="jpg" xsi:type="string">image/jpg</item>
                <item name="jpeg" xsi:type="string">image/jpeg</item>
                <item name="gif" xsi:type="string">image/gif</item>
                <item name="png" xsi:type="string">image/png</item>
            </argument>
        </arguments>
    </type>
magefan commented 3 days ago

@gfrancisco-sh thank you for your report. Unfortunatly we cannot reproduce the issue. It should work without allowedMimeTypes . If we don't set it it is an empty array https://github.com/magento/magento2/blob/2.4.5-p7/app/code/Magento/Catalog/Model/ImageUploader.php#L99

Then this empty array is passed to the validation https://github.com/magento/magento2/blob/2.4.5-p7/app/code/Magento/Catalog/Model/ImageUploader.php#L248

And if array is empty checkMimeType it returns true

Maybe you have some third-party extension that modifies allowedMimeTypes for Magefan\Blog\Model\ImageUploader ? Or Magento\Catalog\Model\ImageUploader

The problem that we cannot add allowedMimeTypes to our extension yet, as we currently still provide support for Magento 2.3.0 which does not have this property. Once we stop supporting it we will be able to add this modification.