Skip to content

Commit

Permalink
Merge pull request #29 from City-of-Helsinki/UHF-10626
Browse files Browse the repository at this point in the history
UHF-10626: Transliterate and normalize filenames on upload
  • Loading branch information
tuutti authored Oct 3, 2024
2 parents 8f3de38 + 8513d49 commit 955f8b7
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 71 deletions.
2 changes: 2 additions & 0 deletions helfi_azure_fs.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ name: HELfi azure file system
type: module
package: Custom
core_version_requirement: ^10 || ^11
dependencies:
- drupal:file
34 changes: 33 additions & 1 deletion helfi_azure_fs.install
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,43 @@

declare(strict_types=1);

/**
* Implements hook_install().
*/
function helfi_azure_fs_install($is_syncing) : void {
if ($is_syncing) {
return;
}

if (!Drupal::moduleHandler()->moduleExists('file')) {
\Drupal::service('module_installer')->install([
'file',
]);
}
\Drupal::configFactory()->getEditable('file.settings')
->set('filename_sanitization', [
'transliterate' => TRUE,
'replace_whitespace' => TRUE,
'replace_non_alphanumeric' => TRUE,
'deduplicate_separators' => TRUE,
'lowercase' => TRUE,
'replacement_character' => '_',
])
->save();
}

/**
* Uninstall flysystem_azure module.
*/
function helfi_azure_fs_update_90201() {
function helfi_azure_fs_update_90201() : void {
\Drupal::service('module_installer')->uninstall([
'flysystem_azure',
]);
}

/**
* Make sure file sanitize settings are enabled.
*/
function helfi_azure_fs_update_90202() : void {
helfi_azure_fs_install(FALSE);
}
20 changes: 0 additions & 20 deletions helfi_azure_fs.module
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
declare(strict_types=1);

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

/**
* Implements hook_entity_field_storage_info_alter().
Expand All @@ -32,22 +31,3 @@ function helfi_azure_fs_entity_field_storage_info_alter(
$field->setSetting('uri_scheme', $scheme);
}
}

/**
* Implements hook_file_presave().
*/
function helfi_azure_fs_file_presave(FileInterface $file) : void {
$new_filename = preg_replace('/[ +]/', '_', $file->getFilename());

if ($new_filename !== $file->getFilename()) {
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$uri = $file->getFileUri();
$directory = $file_system->dirname($uri);
$uri = $directory . '/' . $new_filename;
if ($new_uri = $file_system->move($file->getFileUri(), $uri)) {
$file->set('uri', $new_uri);
$file->set('filename', $file_system->basename($new_uri));
}
}
}
44 changes: 0 additions & 44 deletions tests/src/Functional/FileNameTransliterateTest.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/src/Kernel/StorageInfoAlterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Drupal\Tests\helfi_azure_fs\Kernel;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;

/**
* Tests that file field's uri_scheme can be altered.
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Unit/AzureBlobStorageAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Drupal\Tests\helfi_azure_fs\Unit;

use Drupal\helfi_azure_fs\Flysystem\Adapter\AzureBlobStorageAdapter;
use Drupal\Tests\UnitTestCase;
use Drupal\helfi_azure_fs\Flysystem\Adapter\AzureBlobStorageAdapter;
use GuzzleHttp\Psr7\Response;
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Blob\Models\GetBlobPropertiesResult;
Expand Down
4 changes: 2 additions & 2 deletions tests/src/Unit/AzureFileSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\helfi_azure_fs\AzureFileSystem;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
use Drupal\helfi_azure_fs\AzureFileSystem;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use org\bovigo\vfs\vfsStream;

/**
* Tests AzureFileSystem.
Expand Down
4 changes: 2 additions & 2 deletions tests/src/Unit/AzureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
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 Drupal\helfi_azure_fs\Flysystem\Azure;
use MicrosoftAzure\Storage\Common\Internal\Authentication\SharedAccessSignatureAuthScheme;
use MicrosoftAzure\Storage\Common\Internal\Authentication\SharedKeyAuthScheme;
use org\bovigo\vfs\vfsStream;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use org\bovigo\vfs\vfsStream;

/**
* Tests Azure.
Expand Down

0 comments on commit 955f8b7

Please sign in to comment.