From 3998fad05f5244da655096eddbef5af1d52aa7fe Mon Sep 17 00:00:00 2001 From: Ahmet Bora Date: Wed, 11 Oct 2023 15:40:09 +0300 Subject: [PATCH 1/2] Don't change uuid if exact same file #5730 --- src/Cms/FileActions.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Cms/FileActions.php b/src/Cms/FileActions.php index 3068a11800..da657dd09f 100644 --- a/src/Cms/FileActions.php +++ b/src/Cms/FileActions.php @@ -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(); } From 7b47bd13b73ae9c3bc8f10b627c70b911d1dfbad Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Mon, 22 Jan 2024 16:14:18 +0100 Subject: [PATCH 2/2] Unit test for file action --- tests/Cms/Files/FileActionsTest.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/Cms/Files/FileActionsTest.php b/tests/Cms/Files/FileActionsTest.php index 83220c2b72..ae189fc8e5 100644 --- a/tests/Cms/Files/FileActionsTest.php +++ b/tests/Cms/Files/FileActionsTest.php @@ -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 */