Skip to content

Commit

Permalink
Merge pull request #16 from City-of-Helsinki/PLATTA-4843
Browse files Browse the repository at this point in the history
PLATTA-4843 images with url special chars should be url encoded
  • Loading branch information
dragos-dumi-ibm authored Jun 21, 2023
2 parents e20fbe6 + 4a39deb commit 91364fa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions helfi_azure_fs.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

declare(strict_types = 1);

use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\file\FileInterface;

/**
Expand Down Expand Up @@ -38,7 +38,7 @@ function helfi_azure_fs_entity_field_storage_info_alter(
* Implements hook_file_presave().
*/
function helfi_azure_fs_file_presave(FileInterface $file) : void {
$new_filename = str_replace(' ', '_', $file->getFilename());
$new_filename = preg_replace('/[ +]/', '_', $file->getFilename());

if ($new_filename !== $file->getFilename()) {
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
Expand Down
2 changes: 1 addition & 1 deletion src/Flysystem/Azure.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function getExternalUrl($uri): string {
// to be decoupled from main request.
$uri = str_replace('azure://', 'public://', $uri);

return $this->fileUrlGenerator->generateString($uri);
return UrlHelper::encodePath($this->fileUrlGenerator->generateString($uri));
}
$target = $this->getTarget($uri);

Expand Down
7 changes: 4 additions & 3 deletions tests/src/Functional/FileNameTransliterateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class FileNameTransliterateTest extends FileManagedTestBase {
public function testFileNameTransliteration() {
$image = $this->createFile(NULL, $this->randomMachineName());

$brokenFileName = 'my test filename.png';
$fixedFileName = 'my_test_filename.png';
$brokenFileName = 'my test+filename+something else.png';
$fixedFileName = 'my_test_filename_something_else.png';

$image->setFilename($brokenFileName);
$image->save();
Expand All @@ -37,7 +37,8 @@ public function testFileNameTransliteration() {
$image2->setFilename($brokenFileName);
$image2->save();

$this->assertEquals('my_test_filename_0.png', $image2->getFilename());
$this->assertEquals('my_test_filename_something_else_0.png',
$image2->getFilename());
}

}
11 changes: 8 additions & 3 deletions tests/src/Unit/AzureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public function testGetExternalUrl() : void {
vfsStream::setup('flysystem');
$fileUrlGenerator = $this->prophesize(FileUrlGeneratorInterface::class);
$fileUrlGenerator->generateString(Argument::any())
->shouldBeCalledTimes(1)
->shouldBeCalledTimes(2)
->willReturn(
'https://localhost/styles/test.jpg',
'/styles/test.jpg',
'/styles/test).jpg',
);
$loggerFactory = new LoggerChannelFactory();
$loggerFactory->addLogger($this->prophesize(LoggerInterface::class)->reveal());
Expand All @@ -55,7 +56,11 @@ public function testGetExternalUrl() : void {
// Make sure non-image style URLs are served directly from blob storage.
$this->assertEquals('https://test.blob.core.windows.net/test/test.jpg', $azure->getExternalUrl('vfs://test.jpg'));
// Make sure image style URL is passed to file url generator service.
$this->assertEquals('https://localhost/styles/test.jpg', $azure->getExternalUrl('vfs://styles/test.jpg'));
$this->assertEquals('/styles/test.jpg', $azure->getExternalUrl('vfs://styles/test.jpg'));

// Check that file uri is encoded.
$this->assertEquals('https://test.blob.core.windows.net/test/test%29.jpg', $azure->getExternalUrl('vfs://test).jpg'));
$this->assertEquals('/styles/test%29.jpg', $azure->getExternalUrl('vfs://styles/test).jpg'));
}

/**
Expand Down

0 comments on commit 91364fa

Please sign in to comment.