Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion src/lib/FieldType/Image/ImageStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ public function storeFieldData(VersionInfo $versionInfo, Field $field, array $co
),
$field->value->externalData['fileName']
);
$targetPath = $this->filePathNormalizer->normalizePath($targetPath);
$targetPath = $this->filePathNormalizer->normalizePath(
$targetPath,
true,
$field->value->externalData['inputUri'] ?? null,
);

if (isset($field->value->externalData['inputUri'])) {
$localFilePath = $field->value->externalData['inputUri'];
Expand Down
16 changes: 12 additions & 4 deletions src/lib/IO/FilePathNormalizer/Flysystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Ibexa\Core\IO\FilePathNormalizer;

use const DIRECTORY_SEPARATOR;
use Ibexa\Core\IO\FilePathNormalizerInterface;
use Ibexa\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter;
use League\Flysystem\PathNormalizer;
Expand All @@ -26,22 +27,29 @@ public function __construct(SlugConverter $slugConverter, PathNormalizer $pathNo
$this->pathNormalizer = $pathNormalizer;
}

public function normalizePath(string $filePath, bool $doHash = true): string
public function normalizePath(string $filePath, bool $doHash = true, ?string $realFilePath = null): string
{
$fileName = pathinfo($filePath, PATHINFO_BASENAME);
$directory = pathinfo($filePath, PATHINFO_DIRNAME);

$fileName = $this->slugConverter->convert($fileName, '_1', 'urlalias');

$hash = $doHash
? (preg_match(self::HASH_PATTERN, $fileName) ? '' : bin2hex(random_bytes(6)) . '-')
$hash = $doHash && !preg_match(self::HASH_PATTERN, $fileName)
? $this->generateFilePathHash($realFilePath)
: '';

$filePath = $directory . \DIRECTORY_SEPARATOR . $hash;
$filePath = $directory . DIRECTORY_SEPARATOR . $hash;
$normalizedFileName = $this->pathNormalizer->normalizePath($fileName);

return $filePath . $normalizedFileName;
}

private function generateFilePathHash(?string $realFilePath = null): string
{
$hash = $realFilePath !== null ? md5_file($realFilePath) : false;

return ($hash !== false ? substr($hash, 0, 12) : bin2hex(random_bytes(6))) . '-';
}
}

class_alias(Flysystem::class, 'eZ\Publish\Core\IO\FilePathNormalizer\Flysystem');
2 changes: 1 addition & 1 deletion src/lib/IO/FilePathNormalizerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
interface FilePathNormalizerInterface
{
public function normalizePath(string $filePath, bool $doHash = true): string;
public function normalizePath(string $filePath, bool $doHash = true, ?string $realFilePath = null): string;
}

class_alias(FilePathNormalizerInterface::class, 'eZ\Publish\Core\IO\FilePathNormalizerInterface');
65 changes: 65 additions & 0 deletions tests/integration/Core/Image/ImageStorage/ImageStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,71 @@ public function testStoreFieldDataDuringUpdateWithDifferentImage(VersionInfo $ve
self::assertSame(1, $this->gateway->countImageReferences($binaryFile->uri));
}

/**
* @dataProvider providerOfFieldData
*/
public function testStoreFieldDataWithSameImageOnAutosave(VersionInfo $versionInfo, Field $field): void
{
$targetPath = '1/8/6/232-eng-GB/' . $field->value->externalData['fileName'];

$binaryFile = new BinaryFile([
'id' => $targetPath,
'uri' => $targetPath,
]);

$this->filePathNormalizer
->expects(self::exactly(2))
->method('normalizePath')
->willReturn($targetPath);

$this->ioService
->expects(self::exactly(2))
->method('newBinaryCreateStructFromLocalFile')
->with($field->value->externalData['inputUri'])
->willReturn(new BinaryFileCreateStruct());

$this->ioService
->expects(self::exactly(2))
->method('createBinaryFile')
->willReturn($binaryFile);

$this->ioService
->expects(self::exactly(2))
->method('getMimeType')
->with($binaryFile->id)
->willReturn('image/jpeg');

$this->redecorator
->method('redecorateFromSource')
->with($binaryFile->uri)
->willReturn($binaryFile->uri);

// First save
$this->storage->storeFieldData($versionInfo, $field, $this->getContext());

// Simulate autosave with same image — rebuild externalData
$field->value = new FieldValue([
'externalData' => [
'id' => null,
'path' => $field->value->data['path'] ?? __DIR__ . '/image.jpg',
'inputUri' => __DIR__ . '/image.jpg',
'fileName' => 'image.jpg',
'fileSize' => '12345',
'mimeType' => 'image/jpeg',
'width' => null,
'height' => null,
'alternativeText' => null,
'imageId' => null,
'uri' => null,
'additionalData' => [],
],
]);

$this->storage->storeFieldData($versionInfo, $field, $this->getContext());

self::assertSame(1, $this->gateway->countImageReferences($binaryFile->uri));
}

private function runCommonStoreFieldDataMocks(Field $field): BinaryFile
{
$this->filePathNormalizer
Expand Down
49 changes: 49 additions & 0 deletions tests/lib/IO/FilePathNormalizer/FlysystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,53 @@ public function providerForTestNormalizePath(): array
],
];
}

public function testNormalizePathWithRealFilePath(): void
{
$tempFile = tempnam(sys_get_temp_dir(), 'test_image_');
file_put_contents($tempFile, 'test image content');

try {
$md5File = md5_file($tempFile);
self::assertNotFalse($md5File);
$expectedHash = substr($md5File, 0, 12);

$this->slugConverter
->expects(self::once())
->method('convert')
->with('image.jpg')
->willReturn('image.jpg');

$normalizedPath = $this->filePathNormalizer->normalizePath(
'4/3/2/234/1/image.jpg',
true,
$tempFile,
);

self::assertSame('4/3/2/234/1/' . $expectedHash . '-image.jpg', $normalizedPath);
} finally {
unlink($tempFile);
}
}

public function testNormalizePathWithRealFilePathIsDeterministic(): void
{
$tempFile = tempnam(sys_get_temp_dir(), 'test_image_');
file_put_contents($tempFile, 'test image content');

try {
$this->slugConverter
->expects(self::exactly(2))
->method('convert')
->with('image.jpg')
->willReturn('image.jpg');

$first = $this->filePathNormalizer->normalizePath('4/3/2/234/1/image.jpg', true, $tempFile);
$second = $this->filePathNormalizer->normalizePath('4/3/2/234/1/image.jpg', true, $tempFile);

self::assertSame($first, $second);
} finally {
unlink($tempFile);
}
}
}
Loading