Issue #3127116 by phthlaap, AjitS, cmlara, AmbyH, smustgrave, alexpott, quietone: Image styles - thumbnails are broken in config page when private file system is used

(cherry picked from commit fcc1ba65fd)
merge-requests/6796/head
catch 2024-02-26 16:43:30 +00:00
parent 555c098f19
commit d95dc67e1f
4 changed files with 60 additions and 1 deletions

View File

@ -58,6 +58,11 @@ function template_preprocess_image_style_preview(&$variables) {
$style->createDerivative($original_path, $preview_file); $style->createDerivative($original_path, $preview_file);
} }
$preview_image = $image_factory->get($preview_file); $preview_image = $image_factory->get($preview_file);
// Generate an itok.
$defaultScheme = \Drupal::config('system.file')->get('default_scheme');
$variables['itok'] = $style->getPathToken($defaultScheme . '://' . $original_path);
$variables['derivative'] = [ $variables['derivative'] = [
'url' => $file_url_generator->generateString($preview_file), 'url' => $file_url_generator->generateString($preview_file),
'width' => $preview_image->getWidth(), 'width' => $preview_image->getWidth(),
@ -89,7 +94,7 @@ function template_preprocess_image_style_preview(&$variables) {
// to prevent caching of images on the client side. // to prevent caching of images on the client side.
$variables['derivative']['rendered'] = [ $variables['derivative']['rendered'] = [
'#theme' => 'image', '#theme' => 'image',
'#uri' => $variables['derivative']['url'] . '?cache_bypass=' . $variables['cache_bypass'], '#uri' => $variables['derivative']['url'] . '?cache_bypass=' . $variables['cache_bypass'] . '&itok=' . $variables['itok'],
'#alt' => t('Sample modified image'), '#alt' => t('Sample modified image'),
'#title' => '', '#title' => '',
'#attributes' => [ '#attributes' => [

View File

@ -167,6 +167,20 @@ function image_file_download($uri) {
} }
return -1; return -1;
} }
// If it is the sample image we need to grant access.
$samplePath = \Drupal::config('image.settings')->get('preview_image');
if ($path === $samplePath) {
$image = \Drupal::service('image.factory')->get($samplePath);
return [
// Send headers describing the image's size, and MIME-type.
'Content-Type' => $image->getMimeType(),
'Content-Length' => $image->getFileSize(),
// By not explicitly setting them here, this uses normal Drupal
// Expires, Cache-Control and ETag headers to prevent proxy or
// browser caching of private images.
];
}
} }
/** /**

View File

@ -112,6 +112,7 @@ class ImageStyleDownloadController extends FileDownloadController {
$target = $request->query->get('file'); $target = $request->query->get('file');
$image_uri = $scheme . '://' . $target; $image_uri = $scheme . '://' . $target;
$image_uri = $this->streamWrapperManager->normalizeUri($image_uri); $image_uri = $this->streamWrapperManager->normalizeUri($image_uri);
$sample_image_uri = $scheme . '://' . $this->config('image.settings')->get('preview_image');
if ($this->streamWrapperManager->isValidScheme($scheme)) { if ($this->streamWrapperManager->isValidScheme($scheme)) {
$normalized_target = $this->streamWrapperManager->getTarget($image_uri); $normalized_target = $this->streamWrapperManager->getTarget($image_uri);
@ -181,6 +182,11 @@ class ImageStyleDownloadController extends FileDownloadController {
} }
} }
// If it is default sample.png, ignore scheme.
if ($image_uri === $sample_image_uri) {
$image_uri = $target;
}
// Don't try to generate file if source is missing. // Don't try to generate file if source is missing.
if (!$this->sourceImageExists($image_uri, $token_is_valid)) { if (!$this->sourceImageExists($image_uri, $token_is_valid)) {
// If the image style converted the extension, it has been added to the // If the image style converted the extension, it has been added to the

View File

@ -518,4 +518,38 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
$this->assertSession()->pageTextContains("Select a new effect"); $this->assertSession()->pageTextContains("Select a new effect");
} }
/**
* Tests the display of preview images using a private scheme.
*/
public function testPreviewImageShowInPrivateScheme(): void {
$this->config('system.file')->set('default_scheme', 'private')->save();
/** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
$file_url_generator = \Drupal::service('file_url_generator');
// Get the original preview image file in core config.
$original_path = $this->config('image.settings')->get('preview_image');
$style = ImageStyle::create(['name' => 'test_foo', 'label' => 'test foo']);
$style->save();
// Build the derivative preview image file with the Image Style.
// @see template_preprocess_image_style_preview()
$preview_file = $style->buildUri($original_path);
$style->createDerivative($original_path, $preview_file);
// Check if the derivative image exists.
$this->assertFileExists($preview_file);
// Generate itok token for the preview image.
$itok = $style->getPathToken('private://' . $original_path);
$url = $file_url_generator->generateAbsoluteString($preview_file);
$url .= '?itok=' . $itok;
// Check if the preview image with style is shown.
$this->drupalGet($url);
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->responseHeaderContains('Content-Type', 'image/png');
}
} }