magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.55k stars 9.32k forks source link

2.4.7 breaks uploads of additional file type to CMS media gallery #39025

Open adamlavery opened 3 months ago

adamlavery commented 3 months ago

Preconditions and environment

M2.4.7

Steps to reproduce

Implement DI overrides to allow more than just jpeg, jpg, png, gif files to be uploaded to the CMS media gallery e.g. add PDF

Expected result

PDF files can be uploaded and used in CMS content.

Actual result

PDF files are now disallowed again in 2.4.7

Additional information

Why are they now disallowed? Simple - some developer decided to "fix" a check for allowed files types in the media-uploader.js script. Instead of supplying allowed types to the script, as specified in DI, s/he hardcoded the allowed extensions in the script.

        _create: function () {
            let self = this,
                arrayFromObj = Array.from,
                progressTmpl = mageTemplate('[data-template="uploader"]'),
                uploaderElement = '#fileUploader',
                targetElement = this.element.find('.fileinput-button.form-buttons')[0],
                uploadUrl = $(uploaderElement).attr('data-url'),
                fileId = null,
                allowedExt = ['jpeg', 'jpg', 'png', 'gif'],
                allowedResize = false,
                options = {

In fact someone thought of doing this in earlier versions but never followed it through - acceptFileTypes is never used:

                dropZone: '[data-tab-panel=image-management]',
                sequentialUploads: true,
                acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
                maxFileSize: this.options.maxFileSize,

Come on, how does this kind of schoolboy error make it into production? We learnt in the '80s that you never duplicate code/constants. If you do then your code is wrong and needs to be restructured to remove duplication.

Now, in addition to adding multiple DI entries to allow more file types in CMS content (which is also very poor code design), we have to override Magento_Backend/js/media-uploader.js to allow them, and manage core changes going forward.

Magento developers - you must do better! The time and cost of your errors is considerable.

Release note

Fix error introduced in 2,4.7 that negates DI changes to allow additional file types in CMS pages/blocks.

Triage and priority

m2-assistant[bot] commented 3 months ago

Hi @adamlavery. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

hostep commented 3 months ago

There once was (back in 2017) a suggestion to be able to upload pdf files through a separate "file upload" mechanism that wouldn't abuse the "media gallery upload" (because pdf files aren't images that need to be resized to show a thumbnail): https://github.com/magento/magento2/issues/9850

Maybe that idea should get picked up again?

m2-assistant[bot] commented 3 months ago

Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Hotel commented 3 months ago

Hello @adamlavery,

Thanks for the report and collaboration!

We have tried to reproduce this issue in the latest development branch i.e. 2.4-develop and the issue seems reproducible for us. Currently, there is no way to upload files other than the ones mentioned in the existing module, need to override Magento_Backend/js/media-uploader.js to allow it.

Hence confirming the issue.

Thanks

github-jira-sync-bot commented 3 months ago

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-12718 is successfully created for this GitHub issue.

m2-assistant[bot] commented 3 months ago

:white_check_mark: Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

adamlavery commented 3 months ago

@hostep this needs to be more flexible. It was already ridiculous how many duplicate DI entries needed to added. Poor design means each module needed to be told to allow additional types. There should be one single source of allowed CMS file types and it should be in admin, not require code changes. And it should be Media not Images; some sites need to use more than just images on CMS pages and want to use modern image formats nowadays (WEBP, AVIF) . For one of my customer stores we need the following. It's a print store and they provide templates in various formats for their customers to use.

                <item name="allowed" xsi:type="array">
                    <item name="pdf" xsi:type="string">application/pdf</item>
                    <item name="eps" xsi:type="string">application/postscript</item>
                    <item name="idml" xsi:type="string">application/octet-stream</item>
                    <item name="ai" xsi:type="string">application/postscript</item>
                    <item name="psd" xsi:type="string">application/octet-stream</item>
                    <item name="zip" xsi:type="string">application/zip</item>
                </item>

We already had to repeat this 3 times, now it's 5 times as each module has it's own separate config; as well as now also having to override the upload script!

Modern image converters can handle much more than just basic image files to generate thumbnails, including PDFs. If a format can't be converted, display a generic image type thumb.