Commit f534b1ac authored by Michael Lander's avatar Michael Lander
Browse files

Derive response cacheability from the public scheme and file_additional_public_schemes

parent 24945e5c
Loading
Loading
Loading
Loading
+14 −1
Changes for src/Controller/ImageStyleDownloadController.php: 14 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\imageapi_optimize_webp\Controller;

use Drupal\Core\Site\Settings;
use Drupal\image\Controller\ImageStyleDownloadController as CoreImageStyleDownloadController;
use Drupal\image\ImageStyleInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
@@ -59,7 +60,19 @@ class ImageStyleDownloadController extends CoreImageStyleDownloadController {
              'Content-Type' => 'image/webp',
              'Content-Length' => $image->getFileSize(),
            ];
            return new BinaryFileResponse($uri, 200, $headers, $scheme !== 'private');
            // Only the public scheme and schemes opted into the
            // file_additional_public_schemes setting may be marked publicly
            // cacheable; all other schemes are access controlled.
            $additional_public_schemes = array_diff(
              Settings::get('file_additional_public_schemes', []),
              ['public', 'private', 'temporary'],
            );
            $is_public = in_array(
              $this->streamWrapperManager->getScheme($derivative_uri),
              array_merge(['public'], $additional_public_schemes),
              TRUE,
            );
            return new BinaryFileResponse($uri, 200, $headers, $is_public);
          }
          // If the derivative does not exist, return a failed reponse.
          return new Response($this->t('Error generating image.'), 500);