Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-10793: Drush command to migrate existing files #30

Merged
merged 8 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:

- name: Run codecov
working-directory: ${{ env.MODULE_FOLDER }}
run: codecov
run: codecov -t ${{ secrets.CODECOV_TOKEN }}

- name: Create an artifact from test report
uses: actions/upload-artifact@v4
Expand Down
118 changes: 118 additions & 0 deletions src/Drush/Commands/TransliterateFilesCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_azure_fs\Drush\Commands;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\Event\FileUploadSanitizeNameEvent;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drush\Attributes\Command;
use Drush\Commands\AutowireTrait;
use Drush\Commands\DrushCommands;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* A drush command to transliterate existing file names.
*
* Usage:
*
* $ drush helfi:files:transliterate
* This will transliterate all existing files to match the current
* transliterate settings.
*/
final class TransliterateFilesCommands extends DrushCommands {

use AutowireTrait;

/**
* Constructs a new instance.
*/
public function __construct(
private readonly StreamWrapperManagerInterface $streamWrapperManager,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly EntityTypeManagerInterface $entityTypeManager,
private readonly FileSystemInterface $fileSystem,
) {
$this->io = new SymfonyStyle($this->input(), $this->output());

parent::__construct();
}

/**
* Gets the sanitized filename.
*
* @param \Drupal\file\FileInterface $file
* The file to sanitize.
*
* @return string
* The sanitized filename.
*/
private function getSanitizedFilename(FileInterface $file): string {
$event = new FileUploadSanitizeNameEvent($file->getFilename(), '');
$this->eventDispatcher->dispatch($event);

return $event->getFilename();
}

/**
* Transliterates the existing filenames.
*
* @return int
* The exit code.
*/
#[Command(name: 'helfi:files:transliterate')]
public function transliterate() : int {
$ids = $this->entityTypeManager
->getStorage('file')
->getQuery()
->accessCheck(FALSE)
->execute();

foreach ($ids as $id) {
if (!$file = File::load($id)) {
continue;
}

$sanitizedFilename = $this->getSanitizedFilename($file);

if ($sanitizedFilename === $file->getFilename()) {
continue;
}
if (!file_exists($file->getFileUri())) {
$this->io->warning("File {$file->getFileUri()} does not exist on disk. Skipping ...");

continue;
}
$directory = $this->fileSystem->dirname($file->getFileUri());
$sanitizedUri = sprintf('%s/%s', $directory, $sanitizedFilename);
$sanitizedUri = $this->streamWrapperManager->normalizeUri($sanitizedUri);

try {
$this->fileSystem->move($file->getFileUri(), $sanitizedUri);
}
catch (FileException $e) {
$this->io->error($e->getMessage());

continue;
}

// Make sure file was actually renamed.
if (!file_exists($sanitizedUri)) {
continue;
}
$file->setFileUri($sanitizedUri);
$file->setFilename($sanitizedFilename);
$file->save();

$this->io->success("File {$sanitizedUri} has been translited.");
}
return DrushCommands::EXIT_SUCCESS;
}

}
108 changes: 108 additions & 0 deletions tests/src/Kernel/TransliterateFilesCommandsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_azure_fs\Kernel;

use Drupal\Core\File\FileSystemInterface;
use Drupal\Tests\TestFileCreationTrait;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\file\Entity\File;
tuutti marked this conversation as resolved.
Show resolved Hide resolved
use Drupal\helfi_azure_fs\AzureFileSystem;
use Drupal\helfi_azure_fs\Drush\Commands\TransliterateFilesCommands;

/**
* Tests transliterate file Drush command.
*
* @group helfi_azure_fs
*/
class TransliterateFilesCommandsTest extends FieldKernelTestBase {

use TestFileCreationTrait;

/**
* {@inheritdoc}
*/
protected static $modules = [
'image',
'file',
'helfi_azure_fs',
];

/**
* {@inheritdoc}
*/
public function setUp() : void {
parent::setUp();
$this->installConfig(['file', 'helfi_azure_fs']);
$this->installEntitySchema('file');
}

/**
* Enable/disable transliterate settings.
*
* @param bool $enabled
* Whether to enable file sanitization.
*/
private function setTransliterateSetting(bool $enabled) : void {
\Drupal::configFactory()->getEditable('file.settings')
->set('filename_sanitization', [
'transliterate' => $enabled,
'replace_whitespace' => TRUE,
'replace_non_alphanumeric' => TRUE,
'deduplicate_separators' => TRUE,
'lowercase' => TRUE,
'replacement_character' => '_',
])->save();
}

/**
* Make sure files are transliterated.
*/
public function testTransliterateFilesCommand() : void {
/** @var \Drupal\helfi_azure_fs\AzureFileSystem $fileSystem */
$fileSystem = $this->container->get('file_system');
/** @var \Drupal\file\FileStorage $fileStorage */
$fileStorage = $this->container->get('entity_type.manager')->getStorage('file');
$this->assertInstanceOf(AzureFileSystem::class, $fileSystem);

// Make sure transliterate setting is enabled.
$this->setTransliterateSetting(FALSE);

$files = [
'public://folder/Jöö.jpg' => 'public://folder/joo.jpg',
'public://folder/fine.jpg' => 'public://folder/fine.jpg',
'public://Jöö.jpg' => 'public://joo.jpg',
'public://fine.jpg' => 'public://fine.jpg',
];
foreach ($files as $file => $expected) {
$dir = $fileSystem->dirname($file);
$fileSystem->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY);
$fileSystem->saveData('', $file);
$this->assertFileExists($file);

$fileStorage->create([
'uri' => $file,
])->save();
}

$this->setTransliterateSetting(TRUE);
$command = new TransliterateFilesCommands(
$this->container->get('stream_wrapper_manager'),
$this->container->get('event_dispatcher'),
$this->container->get('entity_type.manager'),
$fileSystem,
);
$command->transliterate();

foreach ($files as $expected) {
$this->assertFileExists($expected);
// Make sure uri and filename were updated too.
$files = $fileStorage->loadByProperties(['uri' => $expected]);
$this->assertCount(1, $files);
$file = reset($files);
$this->assertEquals($fileSystem->basename($expected), $file->getFilename());
}
}

}
Loading