From 3ee6b7e8138a1c6fb9e54f272257894c1a50568f Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Fri, 8 Apr 2022 12:14:11 +0200 Subject: [PATCH 1/4] IBX-2663: Implemented `--no-hash` option in `normalize-paths` command --- .../Command/NormalizeImagesPathsCommand.php | 20 +++++++++++- .../Core/IO/FilePathNormalizer/Flysystem.php | 6 ++-- .../Core/IO/FilePathNormalizerInterface.php | 2 +- .../FilePathNormalizer/FlysystemTest.php | 31 +++++++++++++++++-- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/eZ/Bundle/EzPublishCoreBundle/Command/NormalizeImagesPathsCommand.php b/eZ/Bundle/EzPublishCoreBundle/Command/NormalizeImagesPathsCommand.php index f5acd52729..33e2e78819 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Command/NormalizeImagesPathsCommand.php +++ b/eZ/Bundle/EzPublishCoreBundle/Command/NormalizeImagesPathsCommand.php @@ -18,6 +18,7 @@ use eZ\Publish\Core\IO\Values\MissingBinaryFile; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -34,6 +35,8 @@ final class NormalizeImagesPathsCommand extends Command - Manually clear SPI/HTTP cache after running this command. EOT; + private const SKIP_HASHING_COMMAND_PARAMETER = 'no-hash'; + protected static $defaultName = 'ibexa:images:normalize-paths'; /** @var \eZ\Publish\Core\FieldType\Image\ImageStorage\Gateway */ @@ -48,6 +51,9 @@ final class NormalizeImagesPathsCommand extends Command /** @var \eZ\Publish\Core\IO\IOServiceInterface */ private $ioService; + /** @var bool */ + private $skipHashing; + public function __construct( ImageStorageGateway $imageGateway, FilePathNormalizerInterface $filePathNormalizer, @@ -68,6 +74,12 @@ protected function configure() $this ->setDescription('Normalizes stored paths for images.') + ->addOption( + self::SKIP_HASHING_COMMAND_PARAMETER, + null, + InputOption::VALUE_NONE, + 'Skip filenames hashing' + ) ->setHelp( <<%command.name% normalizes paths for images. @@ -80,6 +92,7 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); + $io->title('Normalize image paths'); $io->writeln([ @@ -87,6 +100,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'It may take some time.', ]); + $this->skipHashing = $input->getOption(self::SKIP_HASHING_COMMAND_PARAMETER); + $this->skipHashing + ? $io->caution('Images\'s filenames will not be hashed.') + : $io->caution('Images\'s filenames will be hashed with format {hash}-{sanitized name}.'); + $imagePathsToNormalize = $this->getImagePathsToNormalize($io); $imagePathsToNormalizeCount = \count($imagePathsToNormalize); @@ -222,7 +240,7 @@ static function (array $data) use ($filePath) { return !empty($processedPaths) ? $processedPaths[0]['newPath'] - : $this->filePathNormalizer->normalizePath($filePath); + : $this->filePathNormalizer->normalizePath($filePath, !$this->skipHashing); } private function getImagePathsToNormalize(SymfonyStyle $io): array diff --git a/eZ/Publish/Core/IO/FilePathNormalizer/Flysystem.php b/eZ/Publish/Core/IO/FilePathNormalizer/Flysystem.php index c0f706a767..72721df4c0 100644 --- a/eZ/Publish/Core/IO/FilePathNormalizer/Flysystem.php +++ b/eZ/Publish/Core/IO/FilePathNormalizer/Flysystem.php @@ -24,14 +24,16 @@ public function __construct(SlugConverter $slugConverter) $this->slugConverter = $slugConverter; } - public function normalizePath(string $filePath): string + public function normalizePath(string $filePath, bool $doHash = true): string { $fileName = pathinfo($filePath, PATHINFO_BASENAME); $directory = pathinfo($filePath, PATHINFO_DIRNAME); $fileName = $this->slugConverter->convert($fileName); - $hash = preg_match(self::HASH_PATTERN, $fileName) ? '' : bin2hex(random_bytes(6)) . '-'; + $hash = $doHash + ? (preg_match(self::HASH_PATTERN, $fileName) ? '' : bin2hex(random_bytes(6)) . '-') + : ''; $filePath = $directory . \DIRECTORY_SEPARATOR . $hash . $fileName; diff --git a/eZ/Publish/Core/IO/FilePathNormalizerInterface.php b/eZ/Publish/Core/IO/FilePathNormalizerInterface.php index c4122600e3..c1f2fcd0a7 100644 --- a/eZ/Publish/Core/IO/FilePathNormalizerInterface.php +++ b/eZ/Publish/Core/IO/FilePathNormalizerInterface.php @@ -13,5 +13,5 @@ */ interface FilePathNormalizerInterface { - public function normalizePath(string $filePath): string; + public function normalizePath(string $filePath, bool $doHash = true): string; } diff --git a/eZ/Publish/Core/IO/Tests/FilePathNormalizer/FlysystemTest.php b/eZ/Publish/Core/IO/Tests/FilePathNormalizer/FlysystemTest.php index 8c2ae8bd49..f1cee07943 100644 --- a/eZ/Publish/Core/IO/Tests/FilePathNormalizer/FlysystemTest.php +++ b/eZ/Publish/Core/IO/Tests/FilePathNormalizer/FlysystemTest.php @@ -33,7 +33,8 @@ public function testNormalizePath( string $originalPath, string $fileName, string $sluggedFileName, - string $regex + string $regex, + bool $doHash ): void { $this->slugConverter ->expects(self::once()) @@ -41,7 +42,7 @@ public function testNormalizePath( ->with($fileName) ->willReturn($sluggedFileName); - $normalizedPath = $this->filePathNormalizer->normalizePath($originalPath); + $normalizedPath = $this->filePathNormalizer->normalizePath($originalPath, $doHash); self::assertStringEndsWith($sluggedFileName, $normalizedPath); self::assertRegExp($regex, $normalizedPath); @@ -57,30 +58,56 @@ public function providerForTestNormalizePath(): array 'image.jpg', 'image.jpg', $defaultPattern . 'image.jpg/', + true, ], 'Spaces in the filename' => [ '4/3/2/234/1/image with spaces.jpg', 'image with spaces.jpg', 'image-with-spaces.jpg', $defaultPattern . 'image-with-spaces.jpg/', + true, ], 'Encoded spaces in the name' => [ '4/3/2/234/1/image%20+no+spaces.jpg', 'image%20+no+spaces.jpg', 'image-20-nospaces.jpg', $defaultPattern . 'image-20-nospaces.jpg/', + true, ], 'Special chars in the name' => [ '4/3/2/234/1/image%20+no+spaces?.jpg', 'image%20+no+spaces?.jpg', 'image-20-nospaces.jpg', $defaultPattern . 'image-20-nospaces.jpg/', + true, ], 'Already hashed name' => [ '4/3/2/234/1/14ff44718877-hashed.jpg', '14ff44718877-hashed.jpg', '14ff44718877-hashed.jpg', '/^4\/3\/2\/234\/1\/14ff44718877-hashed.jpg$/', + true, + ], + 'No special chars and no hashing' => [ + '4/3/2/234/1/image.jpg', + 'image.jpg', + 'image.jpg', + '/\/image.jpg/', + false, + ], + 'Special chars and no hashing' => [ + '4/3/2/234/1/image%20+no+spaces.jpg', + 'image%20+no+spaces.jpg', + 'image-20-nospaces.jpg', + '/\/image-20-nospaces.jpg/', + false, + ], + 'Already hashed name and no hashing' => [ + '4/3/2/234/1/14ff44718877-hashed.jpg', + '14ff44718877-hashed.jpg', + '14ff44718877-hashed.jpg', + '/^4\/3\/2\/234\/1\/14ff44718877-hashed.jpg$/', + false, ], ]; } From db8e5ed0b47320170b62925e341cd1c2fba75fac Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Fri, 8 Apr 2022 13:47:46 +0200 Subject: [PATCH 2/4] IBX-2663: Fixed typos --- .../Command/NormalizeImagesPathsCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eZ/Bundle/EzPublishCoreBundle/Command/NormalizeImagesPathsCommand.php b/eZ/Bundle/EzPublishCoreBundle/Command/NormalizeImagesPathsCommand.php index 33e2e78819..fdd150b71e 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Command/NormalizeImagesPathsCommand.php +++ b/eZ/Bundle/EzPublishCoreBundle/Command/NormalizeImagesPathsCommand.php @@ -102,8 +102,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->skipHashing = $input->getOption(self::SKIP_HASHING_COMMAND_PARAMETER); $this->skipHashing - ? $io->caution('Images\'s filenames will not be hashed.') - : $io->caution('Images\'s filenames will be hashed with format {hash}-{sanitized name}.'); + ? $io->caution('Images\' filenames will not be hashed.') + : $io->caution('Images\' filenames will be hashed with format {hash}-{sanitized name}.'); $imagePathsToNormalize = $this->getImagePathsToNormalize($io); From 7ca6c422a60fee852d8071f7cc5dae0bb8d921dd Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Mon, 11 Apr 2022 12:15:01 +0200 Subject: [PATCH 3/4] IBX-2663: Linguistic corrections --- .../Command/NormalizeImagesPathsCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/eZ/Bundle/EzPublishCoreBundle/Command/NormalizeImagesPathsCommand.php b/eZ/Bundle/EzPublishCoreBundle/Command/NormalizeImagesPathsCommand.php index fdd150b71e..aecf75488f 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Command/NormalizeImagesPathsCommand.php +++ b/eZ/Bundle/EzPublishCoreBundle/Command/NormalizeImagesPathsCommand.php @@ -78,7 +78,7 @@ protected function configure() self::SKIP_HASHING_COMMAND_PARAMETER, null, InputOption::VALUE_NONE, - 'Skip filenames hashing' + 'Skip filename hashing' ) ->setHelp( <<skipHashing = $input->getOption(self::SKIP_HASHING_COMMAND_PARAMETER); $this->skipHashing - ? $io->caution('Images\' filenames will not be hashed.') - : $io->caution('Images\' filenames will be hashed with format {hash}-{sanitized name}.'); + ? $io->caution('Image filenames will not be hashed.') + : $io->caution('Image filenames will be hashed with format {hash}-{sanitized name}.'); $imagePathsToNormalize = $this->getImagePathsToNormalize($io); From 2f3244709d614a7fd6b2539edc222f2eff2744cb Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Tue, 12 Apr 2022 16:21:55 +0200 Subject: [PATCH 4/4] IBX-2663: Changed transformation to 'urlalias' --- eZ/Publish/Core/IO/FilePathNormalizer/Flysystem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eZ/Publish/Core/IO/FilePathNormalizer/Flysystem.php b/eZ/Publish/Core/IO/FilePathNormalizer/Flysystem.php index 72721df4c0..fc96e6dbb1 100644 --- a/eZ/Publish/Core/IO/FilePathNormalizer/Flysystem.php +++ b/eZ/Publish/Core/IO/FilePathNormalizer/Flysystem.php @@ -29,7 +29,7 @@ public function normalizePath(string $filePath, bool $doHash = true): string $fileName = pathinfo($filePath, PATHINFO_BASENAME); $directory = pathinfo($filePath, PATHINFO_DIRNAME); - $fileName = $this->slugConverter->convert($fileName); + $fileName = $this->slugConverter->convert($fileName, '_1', 'urlalias'); $hash = $doHash ? (preg_match(self::HASH_PATTERN, $fileName) ? '' : bin2hex(random_bytes(6)) . '-')