Skip to content

Commit

Permalink
Merge pull request #27 from City-of-Helsinki/UHF-10462
Browse files Browse the repository at this point in the history
UHF-10462: Make sure image style requires correct derivative scheme
  • Loading branch information
tuutti authored Aug 16, 2024
2 parents 65f46c8 + 12b5a53 commit 25c7b80
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 18 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
"drupal/coder": "^8.3"
},
"conflict": {
"drupal/core": "<10.3"
},
"extra": {
"patches": {
"twistor/flysystem-stream-wrapper": {
Expand Down
2 changes: 1 addition & 1 deletion helfi_azure_fs.info.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: HELfi azure file system
type: module
package: Custom
core_version_requirement: ^9 || ^10
core_version_requirement: ^9 || ^10 || ^11
5 changes: 2 additions & 3 deletions helfi_azure_fs.module
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
declare(strict_types=1);

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

/**
* Implements hook_entity_field_storage_info_alter().
*/
function helfi_azure_fs_entity_field_storage_info_alter(
&$fields,
EntityTypeInterface $entity_type
EntityTypeInterface $entity_type,
) : void {
$config = Drupal::config('helfi_azure_fs.settings');

Expand Down Expand Up @@ -46,7 +45,7 @@ function helfi_azure_fs_file_presave(FileInterface $file) : void {
$uri = $file->getFileUri();
$directory = $file_system->dirname($uri);
$uri = $directory . '/' . $new_filename;
if ($new_uri = $file_system->move($file->getFileUri(), $uri, FileSystemInterface::EXISTS_RENAME)) {
if ($new_uri = $file_system->move($file->getFileUri(), $uri)) {
$file->set('uri', $new_uri);
$file->set('filename', $file_system->basename($new_uri));
}
Expand Down
6 changes: 5 additions & 1 deletion helfi_azure_fs.services.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
services:
_defaults:
autoconfigure: true
autowire: true
azure_file_system:
class: Drupal\helfi_azure_fs\AzureFileSystem
decorates: file_system
Expand All @@ -7,4 +10,5 @@ services:
- '@azure_file_system.inner'
- '@stream_wrapper_manager'
- '@settings'
- '@logger.channel.file'

Drupal\helfi_azure_fs\Routing\RouteSubscriber: ~
2 changes: 1 addition & 1 deletion src/AzureBlobStorageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class AzureBlobStorageAdapter extends AbstractAdapter {
public function __construct(
protected BlobRestProxy $client,
protected string $container,
?string $prefix = NULL
?string $prefix = NULL,
) {
$this->setPathPrefix($prefix);
}
Expand Down
8 changes: 2 additions & 6 deletions src/AzureFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Psr\Log\LoggerInterface;

/**
* Provides Azure specific file system.
Expand Down Expand Up @@ -38,18 +37,15 @@ final class AzureFileSystem extends FileSystem {
* The stream wrapper manager.
* @param \Drupal\Core\Site\Settings $settings
* The settings.
* @param \Psr\Log\LoggerInterface $logger
* The logger.
*/
public function __construct(
private FileSystemInterface $decorated,
StreamWrapperManagerInterface $streamWrapperManager,
Settings $settings,
LoggerInterface $logger,
) {
$this->skipFsOperations = $settings::get('is_azure', FALSE);

parent::__construct($streamWrapperManager, $settings, $logger);
parent::__construct($streamWrapperManager, $settings);
}

/**
Expand All @@ -69,7 +65,7 @@ public function mkdir(
$uri,
$mode = NULL,
$recursive = FALSE,
$context = NULL
$context = NULL,
): bool {
if (!$this->skipFsOperations) {
return $this->decorated->mkdir($uri, $mode, $recursive, $context);
Expand Down
2 changes: 1 addition & 1 deletion src/Flysystem/Azure.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final class Azure implements FlysystemPluginInterface, ContainerFactoryPluginInt
public function __construct(
private array $configuration,
private LoggerInterface $logger,
private FileUrlGeneratorInterface $fileUrlGenerator
private FileUrlGeneratorInterface $fileUrlGenerator,
) {
}

Expand Down
44 changes: 44 additions & 0 deletions src/Routing/RouteSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_azure_fs\Routing;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

/**
* Alters image style routes.
*/
final class RouteSubscriber extends RouteSubscriberBase {

/**
* Constructs a new instance.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory service.
*/
public function __construct(
private readonly ConfigFactoryInterface $configFactory,
) {
}

/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) : void {
$config = $this->configFactory->get('helfi_azure_fs.settings');

// Skip if blob storage is not in use.
if (!$config->get('use_blob_storage')) {
return;
}

// Make sure image style URL requires 'azure' derivative scheme.
if ($route = $collection->get('image.style_public')) {
$route->setDefault('required_derivative_scheme', 'azure');
}
}

}
5 changes: 1 addition & 4 deletions tests/src/Unit/AzureFileSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use org\bovigo\vfs\vfsStream;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Log\LoggerInterface;

/**
* Tests AzureFileSystem.
Expand Down Expand Up @@ -95,9 +94,7 @@ private function getSut(FileSystemInterface $decorated, Settings $settings, ?Str
if (!$streamWrapperManager) {
$streamWrapperManager = $this->createMock(StreamWrapperManagerInterface::class);
}
$logger = $this->createMock(LoggerInterface::class);

return new AzureFileSystem($decorated, $streamWrapperManager, $settings, $logger);
return new AzureFileSystem($decorated, $streamWrapperManager, $settings);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion tests/src/Unit/AzureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Logger\LoggerChannelFactory;
use Drupal\Core\Session\AccountInterface;
use Drupal\helfi_azure_fs\Flysystem\Azure;
use Drupal\Tests\UnitTestCase;
use MicrosoftAzure\Storage\Common\Internal\Authentication\SharedAccessSignatureAuthScheme;
Expand All @@ -15,6 +16,7 @@
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Tests Azure.
Expand All @@ -37,7 +39,10 @@ public function testGetExternalUrl() : void {
'/styles/test.jpg',
'/styles/test).jpg',
);
$loggerFactory = new LoggerChannelFactory();
$loggerFactory = new LoggerChannelFactory(
$this->prophesize(RequestStack::class)->reveal(),
$this->prophesize(AccountInterface::class)->reveal(),
);
$loggerFactory->addLogger($this->prophesize(LoggerInterface::class)->reveal());

$configuration = [
Expand Down

0 comments on commit 25c7b80

Please sign in to comment.