Skip to content

Commit 07ff630

Browse files
committed
Use timestamp count instead of index count
1 parent 561b096 commit 07ff630

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/Actions/MoveAsset.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function run($assets, $values)
4646
$folder = $values['folder'];
4747
$strategy = $this->context['conflict'] ?? 'cancel';
4848
$timestamp = now()->timestamp;
49+
$timestampCount = 0;
4950
$ids = [];
5051
$completedMoves = [];
5152

@@ -78,10 +79,11 @@ public function run($assets, $values)
7879
if ($strategy === 'timestamp') {
7980
$filename = $asset->filename().'-'.$timestamp;
8081

81-
if ($index > 0) {
82-
$filename .= '-'.$index;
82+
if ($timestampCount > 0) {
83+
$filename .= '-'.$timestampCount;
8384
}
8485

86+
$timestampCount++;
8587
$oldId = $asset->id();
8688
$newId = $asset->moveUnique($folder, $filename)->id();
8789
$completedMoves[$oldId] = $newId;

tests/Actions/MoveAssetTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,36 @@ public function it_can_keep_both_with_timestamp_strategy(): void
213213
$this->assertEquals('existing', Storage::disk('test')->get('target/logo.svg'));
214214
$this->assertEquals('new', Storage::disk('test')->get('target/logo-1712000000.svg'));
215215
}
216+
217+
#[Test]
218+
public function it_does_not_add_index_suffix_to_first_conflicting_asset_in_batch_with_non_conflicting_assets_before_it(): void
219+
{
220+
Carbon::setTestNow(Carbon::createFromTimestamp(1712000000, config('app.timezone')));
221+
222+
$this->createAsset('source/a.svg', 'a-new');
223+
$this->createAsset('source/logo.svg', 'new');
224+
$this->createAsset('target/logo.svg', 'existing');
225+
226+
$this
227+
->actingAs(tap(User::make()->makeSuper())->save())
228+
->post(cp_route('assets.actions.run'), [
229+
'action' => 'move_asset',
230+
'context' => ['container' => 'test_container', 'conflict' => 'timestamp'],
231+
'selections' => ['test_container::source/a.svg', 'test_container::source/logo.svg'],
232+
'values' => ['folder' => 'target'],
233+
])
234+
->assertOk()
235+
->assertJson([
236+
'success' => true,
237+
]);
238+
239+
Storage::disk('test')->assertMissing('source/a.svg');
240+
Storage::disk('test')->assertMissing('source/logo.svg');
241+
Storage::disk('test')->assertExists('target/a.svg');
242+
Storage::disk('test')->assertExists('target/logo.svg');
243+
Storage::disk('test')->assertExists('target/logo-1712000000.svg');
244+
$this->assertEquals('a-new', Storage::disk('test')->get('target/a.svg'));
245+
$this->assertEquals('existing', Storage::disk('test')->get('target/logo.svg'));
246+
$this->assertEquals('new', Storage::disk('test')->get('target/logo-1712000000.svg'));
247+
}
216248
}

0 commit comments

Comments
 (0)