andersundsehr / aus_driver_amazon_s3

Provides a TYPO3 FAL driver for the Amazon Web Service S3
GNU Lesser General Public License v3.0
21 stars 40 forks source link

Fix MIME type during upload for SVG images without UTF8 encoding #84

Open Moongazer opened 3 years ago

Moongazer commented 3 years ago

Problem Part 1

Some optimization tools e.g. to compress SVG images removing the XML encoding line <?xml version="1.0" encoding="UTF-8"?>. Without this line, PHP function finfo_open() detects the file as image/svg instead of image/svg+xml. It seems this is a know bug which is described here: https://bugs.php.net/bug.php?id=79045

Problem Part 2

Uploading such image to AWS S3 will trigger the browser to download the image instead of show it inside the website. The reason is, that during the upload process the wrong content-type image/svg is explicit set for the S3 object by PHP.

Solution

A simple solution could be to add a clean-up function for the content-type. The following method was tested and works like expected (means: the correct content-type for SVG images without encoding line is set during the upload and successfully shown inside the website):

./aus_driver_amazon_s3/Classes/S3Adapter/MultipartUploaderAdapter.php:

// line 45
'ContentType' => $this->cleanMimeContentType($contentType),

// ...

    /**
     * Fix wrong MIME detection by PHP for SVG images (see https://bugs.php.net/bug.php?id=79045)
     * @param string $contentType
     * @return string
     */
    private function cleanMimeContentType (string $contentType): string {
        if ($contentType === 'image/svg') {
            return 'image/svg+xml';
        }

        return $contentType;
    }

Edit (2021-07-20): The newest TYPO3 release implements a SVG Sanitizer (https://github.com/TYPO3/TYPO3.CMS/commit/45b389d44d), which might check (and/or fix?) "invalid" files. I'll report here after having more information and close this issue in case it works.

weakbit commented 2 days ago

Hi @Moongazer It's a while ago. I merged https://github.com/andersundsehr/aus_driver_amazon_s3/pull/141 today and tried to reproduce the problem you described. On my current setup which also uses Cloudfront uploaded the stripped svg '<?xml version="1.0" encoding="UTF-8"?>' was added.

Can you check if there is still a problem? For me it looks good.