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
30 changes: 9 additions & 21 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ class Asset implements Arrayable, ArrayAccess, AssetContract, Augmentable, Conta
}

protected $container;

protected $path;

protected $meta;

protected $withEvents = true;

protected $shouldHydrate = true;

protected $removedData = [];

public function syncOriginal()
Expand Down Expand Up @@ -745,23 +750,21 @@ public function containerHandle()
*/
public function rename($filename, $unique = false)
{
if ($unique) {
return $this->moveUnique($this->folder(), $filename);
}

return $this->move($this->folder(), $filename);
return $this->move($this->folder(), $filename, $unique);
}

/**
* Move the asset to a different location.
*
* @param string $folder The folder relative to the container.
* @param string|null $filename The new filename, if renaming.
* @param bool $unique Whether to ensure the filename is unique.
* @return $this
*/
public function move($folder, $filename = null)
public function move($folder, $filename = null, $unique = false)
{
$filename = Uploader::getSafeFilename($filename ?: $this->filename());
$filename = $unique ? $this->ensureUniqueFilename($folder, $filename) : $filename;
$oldPath = $this->path();
$oldMetaPath = $this->metaPath();
$newPath = Str::removeLeft(Path::tidy($folder.'/'.$filename.'.'.pathinfo($oldPath, PATHINFO_EXTENSION)), '/');
Expand All @@ -780,21 +783,6 @@ public function move($folder, $filename = null)
return $this;
}

/**
* Move the asset to a different location with a unique filename.
*
* @param string $folder The folder relative to the container.
* @param string|null $filename The new filename, if renaming.
* @return $this
*/
public function moveUnique($folder, $filename = null)
{
$filename = Uploader::getSafeFilename($filename ?: $this->filename());
$filename = $this->ensureUniqueFilename($folder, $filename);

return $this->move($folder, $filename);
}

public function moveQuietly($folder, $filename = null)
{
$this->withEvents = false;
Expand Down
13 changes: 7 additions & 6 deletions tests/Assets/AssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class AssetTest extends TestCase

private $container;

public function setUp(): void
protected function setUp(): void
{
parent::setUp();

Expand Down Expand Up @@ -939,7 +939,7 @@ public function it_saves_quietly()
}

#[Test]
public function when_saving_quietly_the_cached_assets_withEvents_flag_will_be_set_back_to_true()
public function when_saving_quietly_the_cached_assets_with_events_flag_will_be_set_back_to_true()
{
Event::fake();
Storage::fake('test');
Expand Down Expand Up @@ -1261,7 +1261,7 @@ public function it_doesnt_lowercase_moved_files_when_configured()
}

#[Test]
public function it_can_be_moved_uniquely_to_another_folder_when_conflict_exists()
public function it_can_be_moved_to_another_folder_with_a_unique_filename_when_conflict_exists()
{
Storage::fake('local');
$disk = Storage::disk('local');
Expand All @@ -1274,7 +1274,7 @@ public function it_can_be_moved_uniquely_to_another_folder_when_conflict_exists(
$asset = $container->makeAsset('old/asset.txt')->data(['foo' => 'bar']);
$asset->save();

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

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

#[Test]
public function it_can_be_moved_uniquely_to_another_folder_without_renaming_when_no_conflict()
public function it_can_be_moved_to_another_folder_with_a_unique_filename_without_renaming_when_no_conflict()
{
Storage::fake('local');
$disk = Storage::disk('local');
Expand All @@ -1294,7 +1294,7 @@ public function it_can_be_moved_uniquely_to_another_folder_without_renaming_when
$asset = $container->makeAsset('old/asset.txt')->data(['foo' => 'bar']);
$asset->save();

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

$this->assertEquals($asset, $return);
$disk->assertMissing('old/asset.txt');
Expand Down Expand Up @@ -2479,6 +2479,7 @@ public function augment($value)
$relationshipFieldtype = new class extends Fieldtype
{
protected static $handle = 'relationship';

protected $relationship = true;

public function augment($values)
Expand Down
Loading