Skip to content
Snippets Groups Projects
Unverified Commit aa12b0e7 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2630230 by mondrake, ayushmishra206, chr.fritsch, eiriksm, borisson_,...

Issue #2630230 by mondrake, ayushmishra206, chr.fritsch, eiriksm, borisson_, msuthars, jjcarrion, mohit1604, marvin_B8, mikelutz, vacho, vakulrai, larowlan: Image effect convert fails when image file is in the public files root
parent 9ca47a6c
No related branches found
No related tags found
8 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!1012Issue #3226887: Hreflang on non-canonical content pages,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10,!596Issue #3046532: deleting an entity reference field, used in a contextual view, makes the whole site unrecoverable,!496Issue #2463967: Use .user.ini file for PHP settings,!144Issue #2666286: Clean up menu_ui to conform to Drupal coding standards,!16Draft: Resolve #2081585 "History storage",!13Resolve #2903456
...@@ -3,8 +3,10 @@ ...@@ -3,8 +3,10 @@
namespace Drupal\image\Controller; namespace Drupal\image\Controller;
use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\Crypt;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Image\ImageFactory; use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Lock\LockBackendInterface; use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface; use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\image\ImageStyleInterface; use Drupal\image\ImageStyleInterface;
use Drupal\system\FileDownloadController; use Drupal\system\FileDownloadController;
...@@ -42,6 +44,13 @@ class ImageStyleDownloadController extends FileDownloadController { ...@@ -42,6 +44,13 @@ class ImageStyleDownloadController extends FileDownloadController {
*/ */
protected $logger; protected $logger;
/**
* File system service,
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/** /**
* Constructs a ImageStyleDownloadController object. * Constructs a ImageStyleDownloadController object.
* *
...@@ -51,12 +60,20 @@ class ImageStyleDownloadController extends FileDownloadController { ...@@ -51,12 +60,20 @@ class ImageStyleDownloadController extends FileDownloadController {
* The image factory. * The image factory.
* @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
* The stream wrapper manager. * The stream wrapper manager.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The system service.
*/ */
public function __construct(LockBackendInterface $lock, ImageFactory $image_factory, StreamWrapperManagerInterface $stream_wrapper_manager = NULL) { public function __construct(LockBackendInterface $lock, ImageFactory $image_factory, StreamWrapperManagerInterface $stream_wrapper_manager = NULL, FileSystemInterface $file_system = NULL) {
parent::__construct($stream_wrapper_manager); parent::__construct($stream_wrapper_manager);
$this->lock = $lock; $this->lock = $lock;
$this->imageFactory = $image_factory; $this->imageFactory = $image_factory;
$this->logger = $this->getLogger('image'); $this->logger = $this->getLogger('image');
if (!isset($file_system)) {
@trigger_error('Not defining the $file_system argument to ' . __METHOD__ . ' is deprecated in drupal:9.1.0 and will throw an error in drupal:10.0.0.', E_USER_DEPRECATED);
$file_system = \Drupal::service('file_system');
}
$this->fileSystem = $file_system;
} }
/** /**
...@@ -66,7 +83,8 @@ public static function create(ContainerInterface $container) { ...@@ -66,7 +83,8 @@ public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('lock'), $container->get('lock'),
$container->get('image.factory'), $container->get('image.factory'),
$container->get('stream_wrapper_manager') $container->get('stream_wrapper_manager'),
$container->get('file_system')
); );
} }
...@@ -137,8 +155,8 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st ...@@ -137,8 +155,8 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st
// original file, resulting in filenames like image.png.jpeg. So to find // original file, resulting in filenames like image.png.jpeg. So to find
// the actual source image, we remove the extension and check if that // the actual source image, we remove the extension and check if that
// image exists. // image exists.
$path_info = pathinfo($image_uri); $path_info = pathinfo(StreamWrapperManager::getTarget($image_uri));
$converted_image_uri = $path_info['dirname'] . DIRECTORY_SEPARATOR . $path_info['filename']; $converted_image_uri = sprintf('%s://%s%s%s', $this->streamWrapperManager->getScheme($derivative_uri), $path_info['dirname'], DIRECTORY_SEPARATOR, $path_info['filename']);
if (!file_exists($converted_image_uri)) { if (!file_exists($converted_image_uri)) {
$this->logger->notice('Source image at %source_image_path not found while trying to generate derivative image at %derivative_path.', ['%source_image_path' => $image_uri, '%derivative_path' => $derivative_uri]); $this->logger->notice('Source image at %source_image_path not found while trying to generate derivative image at %derivative_path.', ['%source_image_path' => $image_uri, '%derivative_path' => $derivative_uri]);
return new Response($this->t('Error generating image, missing source file.'), 404); return new Response($this->t('Error generating image, missing source file.'), 404);
......
<?php
namespace Drupal\Tests\image\Functional\ImageEffect;
use Drupal\Core\File\FileSystemInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\Tests\BrowserTestBase;
/**
* Tests for the Convert image effect.
*
* @group image
*/
class ConvertTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'classy';
/**
* {@inheritdoc}
*/
protected static $modules = ['image'];
/**
* Tests that files stored in the root folder are converted properly.
*/
public function testConvertFileInRoot() {
// Create the test image style with a Convert effect.
$image_style = ImageStyle::create([
'name' => 'image_effect_test',
'label' => 'Image Effect Test',
]);
$this->assertEquals(SAVED_NEW, $image_style->save());
$image_style->addImageEffect([
'id' => 'image_convert',
'data' => [
'extension' => 'jpeg',
],
]);
$this->assertEquals(SAVED_UPDATED, $image_style->save());
// Create a copy of a test image file in root.
$test_uri = 'public://image-test-do.png';
\Drupal::service('file_system')->copy('core/tests/fixtures/files/image-test.png', $test_uri, FileSystemInterface::EXISTS_REPLACE);
$this->assertFileExists($test_uri);
// Execute the image style on the test image via a GET request.
$derivative_uri = 'public://styles/image_effect_test/public/image-test-do.png.jpeg';
$this->assertFileNotExists($derivative_uri);
$url = file_url_transform_relative($image_style->buildUrl($test_uri));
$this->drupalGet($this->getAbsoluteUrl($url));
$this->assertSession()->statusCodeEquals(200);
$this->assertFileExists($derivative_uri);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment