Skip to content

Commit

Permalink
Merge pull request #5781 from getkirby/v4/fix/5730-same-file-uuid
Browse files Browse the repository at this point in the history
Don't change uuid if exact same file
  • Loading branch information
bastianallgeier authored Jan 22, 2024
2 parents ef2eee0 + 7b47bd1 commit f0249f6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/Cms/FileActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,25 @@ public static function create(array $props, bool $move = false): File
// gather content
$content = $props['content'] ?? [];

// make sure that a UUID gets generated and
// added to content right away
if (Uuids::enabled() === true) {
// make sure that a UUID gets generated
// and added to content right away
if (
Uuids::enabled() === true &&
empty($content['uuid']) === true
) {
// sets the current uuid if it is the exact same file
if ($file->exists() === true) {
$existing = $file->parent()->file($file->filename());

if (
$file->sha1() === $upload->sha1() &&
$file->template() === $existing->template()
) {
// use existing content data if it is the exact same file
$content = $existing->content()->toArray();
}
}

$content['uuid'] ??= Uuid::generate();
}

Expand Down
27 changes: 27 additions & 0 deletions tests/Cms/Files/FileActionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,33 @@ public function testCreate($parent)
$this->assertIsString($result->content()->get('uuid')->value());
}

/**
* @dataProvider parentProvider
*/
public function testCreateDuplicate($parent)
{
$source = static::TMP . '/source.md';

// create the dummy source
F::write($source, '# Test');

$result = File::create([
'filename' => 'test.md',
'source' => $source,
'parent' => $parent
]);

$uuid = $result->content()->get('uuid')->value();

$duplicate = File::create([
'filename' => 'test.md',
'source' => $source,
'parent' => $parent
]);

$this->assertSame($uuid, $duplicate->content()->get('uuid')->value());
}

/**
* @dataProvider parentProvider
*/
Expand Down

0 comments on commit f0249f6

Please sign in to comment.