Commit e8b89f34 authored by catch's avatar catch
Browse files

Issue #3127116 by phthlaap, AjitS, cmlara, AmbyH, smustgrave, alexpott,...

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 fcc1ba65)
parent 82923eaf
Loading
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -58,6 +58,11 @@ function template_preprocess_image_style_preview(&$variables) {
    $style->createDerivative($original_path, $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'] = [
    'url' => $file_url_generator->generateString($preview_file),
    'width' => $preview_image->getWidth(),
@@ -89,7 +94,7 @@ function template_preprocess_image_style_preview(&$variables) {
  // to prevent caching of images on the client side.
  $variables['derivative']['rendered'] = [
    '#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'),
    '#title' => '',
    '#attributes' => [
+14 −0
Original line number Diff line number Diff line
@@ -167,6 +167,20 @@ function image_file_download($uri) {
    }
    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.
    ];
  }
}

/**
+6 −0
Original line number Diff line number Diff line
@@ -110,6 +110,7 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st
    $target = $request->query->get('file');
    $image_uri = $scheme . '://' . $target;
    $image_uri = $this->streamWrapperManager->normalizeUri($image_uri);
    $sample_image_uri = $scheme . '://' . $this->config('image.settings')->get('preview_image');

    if ($this->streamWrapperManager->isValidScheme($scheme)) {
      $normalized_target = $this->streamWrapperManager->getTarget($image_uri);
@@ -175,6 +176,11 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st
      }
    }

    // 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.
    if (!$this->sourceImageExists($image_uri, $token_is_valid)) {
      // If the image style converted the extension, it has been added to the
+34 −0
Original line number Diff line number Diff line
@@ -518,4 +518,38 @@ public function testImageStyleAccess() {
    $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');
  }

}