Skip to content

Commit

Permalink
Merge pull request #568 from creative-commoners/pulls/1.13/unique-fil…
Browse files Browse the repository at this point in the history
…ename

FIX Ensure filenames are not duplicated when moving
  • Loading branch information
GuySartorelli authored Aug 15, 2023
2 parents dcdb6a1 + 4c04531 commit 0512add
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ protected function onBeforeWrite()
$currentname = $name = $this->getField('Name');
$title = $this->getField('Title');

$changed = $this->isChanged('Name');
$changed = $this->isChanged('Name') || $this->isChanged('ParentID');

// Name can't be blank, default to Title or singular name
if (!$name) {
Expand Down
23 changes: 23 additions & 0 deletions tests/php/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1227,4 +1227,27 @@ public function testArchivingModifiedKeepArchivedBothPhysicalFilesWithDifferentF
$this->assertTrue($store->exists('file-changed.txt', $secondHash));
$this->assertSame(AssetStore::VISIBILITY_PROTECTED, $store->getVisibility('file-changed.txt', $secondHash));
}

public function testMoveFileRenamesDuplicateFilename()
{
$folder1 = $this->objFromFixture(Folder::class, 'folder1');
$folder2 = $this->objFromFixture(Folder::class, 'folder2');
$file1 = $this->objFromFixture(File::class, 'pdf');
$file1->ParentID = $folder1->ID;
$file1->write();
$file2 = File::create([
'FileFilename' => $file1->FileFilename,
'FileHash' => $file1->FileHash,
'Name' => $file1->Name,
'ParentID' => $folder2->ID,
]);
$file2->write();
$this->assertTrue(strpos($file1->getFilename(), 'FileTest.pdf') !== false);
$this->assertTrue(strpos($file2->getFilename(), 'FileTest.pdf') !== false);
// Move file1 to folder2 and ensure it gets renamed as it would have a duplicate filename
$file1->ParentID = $folder2->ID;
$file1->write();
$this->assertTrue(strpos($file1->getFilename(), 'FileTest-v2.pdf') !== false);
$this->assertTrue(strpos($file2->getFilename(), 'FileTest.pdf') !== false);
}
}
2 changes: 1 addition & 1 deletion tests/php/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function testGetTagWithoutTitle()
Config::modify()->set(DBFile::class, 'force_resample', false);

$image = $this->objFromFixture(Image::class, 'imageWithoutTitle');
$expected = '<img width="300" height="300" alt="test image" src="/assets/ImageTest/folder/test-image.png" loading="lazy" />';
$expected = '<img width="300" height="300" alt="test image without title" src="/assets/ImageTest/folder/test-image-without-title.png" loading="lazy" />';
$actual = trim($image->getTag() ?? '');

$this->assertEquals($expected, $actual);
Expand Down
8 changes: 4 additions & 4 deletions tests/php/ImageTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ SilverStripe\Assets\Image:
Parent: =>SilverStripe\Assets\Folder.folder1
Name: test-image.png
imageWithoutTitle:
FileFilename: folder/test-image.png
FileFilename: folder/test-image-without-title.png
FileHash: 444065542b5dd5187166d8e1cd684e0d724c5a97
Parent: =>SilverStripe\Assets\Folder.folder1
Name: test-image.png
Name: test-image-without-title.png
imageWithoutTitleContainingDots:
FileFilename: folder/test.image.with.dots.png
FileHash: 46affab7043cfd9f1ded919dd24affd08e926eca
Parent: =>SilverStripe\Assets\Folder.folder1
Name: test.image.with.dots.png
imageWithMetacharacters:
Title: This is a/an image Title
FileFilename: folder/test-image.png
FileFilename: folder/test-image-metacharacters.png
FileHash: 444065542b5dd5187166d8e1cd684e0d724c5a97
Parent: =>SilverStripe\Assets\Folder.folder1
Name: test-image.png
Name: test-image-metacharacters.png
lowQualityJPEG:
Title: This is a low quality JPEG
FileFilename: folder/test-image-low-quality.jpg
Expand Down
Binary file added tests/php/ImageTest/test-image-metacharacters.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/php/ImageTest/test-image-without-title.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0512add

Please sign in to comment.