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

IBX-2663: Implemented --no-hash option in normalize-paths command #298

Merged
merged 4 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
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 filenames hashing'
barw4 marked this conversation as resolved.
Show resolved Hide resolved
)
->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('Images\' filenames will not be hashed.')
: $io->caution('Images\' filenames will be hashed with format {hash}-{sanitized name}.');
barw4 marked this conversation as resolved.
Show resolved Hide resolved

$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
6 changes: 4 additions & 2 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);

$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