|
3 | 3 | namespace Drupal\system; |
4 | 4 |
|
5 | 5 | use Drupal\Core\Controller\ControllerBase; |
| 6 | +use Drupal\Core\Site\Settings; |
6 | 7 | use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface; |
7 | 8 | use Symfony\Component\HttpFoundation\BinaryFileResponse; |
8 | 9 | use Symfony\Component\HttpFoundation\Request; |
@@ -74,9 +75,24 @@ public function download(Request $request, $scheme = 'private') { |
74 | 75 | if (count($headers)) { |
75 | 76 | // \Drupal\Core\EventSubscriber\FinishResponseSubscriber::onRespond() |
76 | 77 | // sets response as not cacheable if the Cache-Control header is not |
77 | | - // already modified. We pass in FALSE for non-private schemes for the |
78 | | - // $public parameter to make sure we don't change the headers. |
79 | | - return new BinaryFileResponse($uri, 200, $headers, $scheme !== 'private'); |
| 78 | + // already modified. Pass in FALSE for the $public parameter so that |
| 79 | + // existing headers from hook_file_download() are preserved. If any of |
| 80 | + // those headers set a Cache-Control header, return the response. |
| 81 | + $response = new BinaryFileResponse($uri, 200, $headers, FALSE); |
| 82 | + if ($response->headers->has('Cache-Control')) { |
| 83 | + return $response; |
| 84 | + } |
| 85 | + |
| 86 | + // If there is no Cache-Control header, then respect the |
| 87 | + // file_additional_public_schemes setting, but never treat the core |
| 88 | + // 'private' or 'temporary' schemes as cacheable. |
| 89 | + $additional_public_schemes = array_diff( |
| 90 | + Settings::get('file_additional_public_schemes', []), |
| 91 | + ['private', 'temporary'], |
| 92 | + ); |
| 93 | + return in_array($scheme, $additional_public_schemes, TRUE) |
| 94 | + ? $response->setPublic() |
| 95 | + : $response->setPrivate(); |
80 | 96 | } |
81 | 97 |
|
82 | 98 | throw new AccessDeniedHttpException(); |
|
0 commit comments