Skip to content

Commit

Permalink
IBX-2663: Implemented --no-hash option in normalize-paths command (
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 authored Apr 14, 2022
1 parent 6d608c8 commit cc9dcb9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 */
Expand All @@ -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,
Expand All @@ -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 filename hashing'
)
->setHelp(
<<<EOT
The command <info>%command.name%</info> normalizes paths for images.
Expand All @@ -80,13 +92,19 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$io->title('Normalize image paths');

$io->writeln([
'Determining the number of images that require path normalization.',
'It may take some time.',
]);

$this->skipHashing = $input->getOption(self::SKIP_HASHING_COMMAND_PARAMETER);
$this->skipHashing
? $io->caution('Image filenames will not be hashed.')
: $io->caution('Image filenames will be hashed with format {hash}-{sanitized name}.');

$imagePathsToNormalize = $this->getImagePathsToNormalize($io);

$imagePathsToNormalizeCount = \count($imagePathsToNormalize);
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions eZ/Publish/Core/IO/FilePathNormalizer/Flysystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
$fileName = $this->slugConverter->convert($fileName, '_1', 'urlalias');

$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;

Expand Down
2 changes: 1 addition & 1 deletion eZ/Publish/Core/IO/FilePathNormalizerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
*/
interface FilePathNormalizerInterface
{
public function normalizePath(string $filePath): string;
public function normalizePath(string $filePath, bool $doHash = true): string;
}
31 changes: 29 additions & 2 deletions eZ/Publish/Core/IO/Tests/FilePathNormalizer/FlysystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ public function testNormalizePath(
string $originalPath,
string $fileName,
string $sluggedFileName,
string $regex
string $regex,
bool $doHash
): void {
$this->slugConverter
->expects(self::once())
->method('convert')
->with($fileName)
->willReturn($sluggedFileName);

$normalizedPath = $this->filePathNormalizer->normalizePath($originalPath);
$normalizedPath = $this->filePathNormalizer->normalizePath($originalPath, $doHash);

self::assertStringEndsWith($sluggedFileName, $normalizedPath);
self::assertRegExp($regex, $normalizedPath);
Expand All @@ -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,
],
];
}
Expand Down

0 comments on commit cc9dcb9

Please sign in to comment.