Skip to content

Commit 8c478d0

Browse files
authored
[7.x] Add $unique argument to Asset::move() (#14364)
1 parent 6f7d937 commit 8c478d0

2 files changed

Lines changed: 9 additions & 26 deletions

File tree

src/Assets/Asset.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -745,23 +745,21 @@ public function containerHandle()
745745
*/
746746
public function rename($filename, $unique = false)
747747
{
748-
if ($unique) {
749-
return $this->moveUnique($this->folder(), $filename);
750-
}
751-
752-
return $this->move($this->folder(), $filename);
748+
return $this->move($this->folder(), $filename, $unique);
753749
}
754750

755751
/**
756752
* Move the asset to a different location.
757753
*
758754
* @param string $folder The folder relative to the container.
759755
* @param string|null $filename The new filename, if renaming.
756+
* @param bool $unique Whether to ensure the filename is unique.
760757
* @return $this
761758
*/
762-
public function move($folder, $filename = null)
759+
public function move($folder, $filename = null, $unique = false)
763760
{
764761
$filename = Uploader::getSafeFilename($filename ?: $this->filename());
762+
$filename = $unique ? $this->ensureUniqueFilename($folder, $filename) : $filename;
765763
$oldPath = $this->path();
766764
$oldMetaPath = $this->metaPath();
767765
$newPath = Str::removeLeft(Path::tidy($folder.'/'.$filename.'.'.pathinfo($oldPath, PATHINFO_EXTENSION)), '/');
@@ -780,22 +778,7 @@ public function move($folder, $filename = null)
780778
return $this;
781779
}
782780

783-
/**
784-
* Move the asset to a different location with a unique filename.
785-
*
786-
* @param string $folder The folder relative to the container.
787-
* @param string|null $filename The new filename, if renaming.
788-
* @return $this
789-
*/
790-
public function moveUnique($folder, $filename = null)
791-
{
792-
$filename = Uploader::getSafeFilename($filename ?: $this->filename());
793-
$filename = $this->ensureUniqueFilename($folder, $filename);
794-
795-
return $this->move($folder, $filename);
796-
}
797-
798-
public function moveQuietly($folder, $filename = null)
781+
public function moveQuietly($folder, $filename = null, $unique = false)
799782
{
800783
$this->withEvents = false;
801784

tests/Assets/AssetTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ public function it_doesnt_lowercase_moved_files_when_configured()
12611261
}
12621262

12631263
#[Test]
1264-
public function it_can_be_moved_uniquely_to_another_folder_when_conflict_exists()
1264+
public function it_can_be_moved_to_another_folder_with_a_unique_filename_when_conflict_exists()
12651265
{
12661266
Storage::fake('local');
12671267
$disk = Storage::disk('local');
@@ -1274,7 +1274,7 @@ public function it_can_be_moved_uniquely_to_another_folder_when_conflict_exists(
12741274
$asset = $container->makeAsset('old/asset.txt')->data(['foo' => 'bar']);
12751275
$asset->save();
12761276

1277-
$return = $asset->moveUnique('new');
1277+
$return = $asset->move('new', null, true);
12781278

12791279
$this->assertEquals($asset, $return);
12801280
$disk->assertMissing('old/asset.txt');
@@ -1283,7 +1283,7 @@ public function it_can_be_moved_uniquely_to_another_folder_when_conflict_exists(
12831283
}
12841284

12851285
#[Test]
1286-
public function it_can_be_moved_uniquely_to_another_folder_without_renaming_when_no_conflict()
1286+
public function it_can_be_moved_to_another_folder_with_a_unique_filename_without_renaming_when_no_conflict()
12871287
{
12881288
Storage::fake('local');
12891289
$disk = Storage::disk('local');
@@ -1294,7 +1294,7 @@ public function it_can_be_moved_uniquely_to_another_folder_without_renaming_when
12941294
$asset = $container->makeAsset('old/asset.txt')->data(['foo' => 'bar']);
12951295
$asset->save();
12961296

1297-
$return = $asset->moveUnique('new');
1297+
$return = $asset->move('new', null, true);
12981298

12991299
$this->assertEquals($asset, $return);
13001300
$disk->assertMissing('old/asset.txt');

0 commit comments

Comments
 (0)