Uploaded image for project: 'eZ Publish / Platform'
  1. eZ Publish / Platform
  2. EZP-27944

Fatal error when trying to download files named with non-ASCII characters using BinaryStreamResponse

    XMLWordPrintable

Details

    Description

      When using BinaryStreamResponse for file downloading, it generates a fatal error for files with non-ASCII characters. Here is an excerpt from logs:

      [2017-09-25 14:30:59] request.CRITICAL: Uncaught PHP Exception InvalidArgumentException: "The filename fallback must only contain ASCII characters." at xxx\ezpublish\vendor\symfony\symfony\src\Symfony\Component\HttpFoundation\ResponseHeaderBag.php line 251 {"exception":"[object] (InvalidArgumentException(code: 0): The filename fallback must only contain ASCII characters. at xxx\\ezpublish\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\HttpFoundation\\ResponseHeaderBag.php:251)"} []
      

      This issue was already solved for version 1.6.0 and later (see EZP-26554), but it should be backported to 5.4 since it can also be reproduced there.

      Steps to reproduce

      1. Create new eZ Publish 5.4 installation.
      2. Create and register a custom bundle (AppBundle).
      3. In the AppBundle, create a new Controller file named DownloadController.php with the following contents:
        <?php
        
        namespace AppBundle\Controller;
        
        use eZ\Bundle\EzPublishCoreBundle\Controller;
        use eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse;
        use Symfony\Component\HttpFoundation\Request;
        use Symfony\Component\HttpFoundation\ResponseHeaderBag;
        use Symfony\Component\Routing\Annotation\Route;
        
        class DownloadController extends Controller
        {
            /**
             * @Route("/content/download/{contentId}/{attributeId}/{fieldIdentifier}/{filename}", name = "custom_content_download")
             */
            public function downloadBinaryFileAction($contentId, $attributeId, $fieldIdentifier, $filename, Request $request)
            {
                $contentService = $this->getRepository()->getContentService();
                $translationHelper = $this->get('ezpublish.translation_helper');
                $ioService = $this->get('ezpublish.fieldType.ezbinaryfile.io_service');
        
                $content = $contentService->loadContent($contentId);
        
                $field = $translationHelper->getTranslatedField(
                    $content,
                    $fieldIdentifier,
                    $request->query->has('language') ? $request->query->get('language') : null
                );
        
                $response = new BinaryStreamResponse($ioService->loadBinaryFile($field->value->id), $ioService);
                $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename);
        
                return $response;
            }
        }
        
      4. Add the following to the ezpublish/config/routing.yml file:
        custom_download:
            resource: "@AppBundle/Controller/DownloadController.php"
            type:     annotation
        
      5. Choose any file and rename it to wontÆØÅwork.ext.
      6. Open the admin interface, create a new file and upload wontÆØÅwork.ext.
      7. Embed this file inside a Folder Content Object.
      8. Open that Content Object with the wontÆØÅwork.ext in the frontend, and try to download the file.

      Attachments

        Activity

          People

            Unassigned Unassigned
            jacek.foremski-obsolete@ez.no Jacek Foremski (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: