Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Pass original upload filename into AssetUploaded event #11423

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ public function upload(UploadedFile $file)
->syncOriginal()
->save();

AssetUploaded::dispatch($this);
AssetUploaded::dispatch($this, $file->getClientOriginalName());

AssetCreated::dispatch($this);

Expand All @@ -935,7 +935,7 @@ public function reupload(ReplacementFile $file)
$this->clearCaches();
$this->writeMeta($this->generateMeta());

AssetReuploaded::dispatch($this);
AssetReuploaded::dispatch($this, $file->basename());

return $this;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Assets/ReplacementFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function extension()
return pathinfo($this->path, PATHINFO_EXTENSION);
}

public function basename()
{
return pathinfo($this->path, PATHINFO_BASENAME);
}

public function writeTo(Filesystem $disk, $path)
{
$disk->put(
Expand Down
2 changes: 1 addition & 1 deletion src/Events/AssetReuploaded.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class AssetReuploaded extends Event implements ProvidesCommitMessage
{
public function __construct(public $asset)
public function __construct(public $asset, public $originalFilename)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Events/AssetUploaded.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class AssetUploaded extends Event implements ProvidesCommitMessage
{
public function __construct(public $asset)
public function __construct(public $asset, public $originalFilename)
{
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Assets/AssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,22 @@ public function it_appends_timestamp_to_uploaded_files_filename_if_it_already_ex
});
}

#[Test]
public function it_sanitizes_uploaded_filenames_but_passes_the_original_name_into_the_event()
{
Event::fake();
$asset = $this->container->makeAsset('path/to/Sanitize This Asset © Photographer.JPG');
Facades\AssetContainer::shouldReceive('findByHandle')->with('test_container')->andReturn($this->container);

$asset->upload(UploadedFile::fake()->image('Sanitize This Asset © Photographer.JPG'));

Storage::disk('test')->assertExists('path/to/sanitize-this-asset--photographer.jpg');
$this->assertEquals('path/to/sanitize-this-asset--photographer.jpg', $asset->path());
Event::assertDispatched(AssetUploaded::class, function ($event) use ($asset) {
return $event->asset = $asset && $event->originalFilename === 'Sanitize This Asset © Photographer.JPG';
});
}

#[Test]
public function it_lowercases_uploaded_filenames_by_default()
{
Expand Down
18 changes: 9 additions & 9 deletions tests/Assets/GeneratePresetImageManipulationsOnUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public function it_subscribes()

#[Test]
#[DataProvider('presetProvider')]
public function presets_are_generated_for_images($event, $extension, $shouldGenerate)
public function presets_are_generated_for_images($event, $basename, $shouldGenerate)
{
$generator = Mockery::mock(PresetGenerator::class);
$asset = (new Asset)->path('foo.'.$extension);
$asset = (new Asset)->path($basename);

if ($shouldGenerate) {
$generator->shouldReceive('generate')->once()->with($asset);
Expand All @@ -41,19 +41,19 @@ public function presets_are_generated_for_images($event, $extension, $shouldGene

$listener = new GeneratePresetImageManipulations($generator);

$listener->handle(new $event($asset));
$listener->handle(new $event($asset, $basename));
}

public static function presetProvider()
{
return [
[AssetUploaded::class, 'jpg', true],
[AssetUploaded::class, 'svg', false],
[AssetUploaded::class, 'txt', false],
[AssetUploaded::class, 'foo.jpg', true],
[AssetUploaded::class, 'foo.svg', false],
[AssetUploaded::class, 'foo.txt', false],

[AssetReuploaded::class, 'jpg', true],
[AssetReuploaded::class, 'svg', false],
[AssetReuploaded::class, 'txt', false],
[AssetReuploaded::class, 'foo.jpg', true],
[AssetReuploaded::class, 'foo.svg', false],
[AssetReuploaded::class, 'foo.txt', false],
];
}
}
2 changes: 1 addition & 1 deletion tests/Feature/Assets/ClearAssetGlideCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function it_clears_when_reuploading()
$asset = Mockery::mock(Asset::class);
Glide::shouldReceive('clearAsset')->with($asset)->once();

app(ClearAssetGlideCache::class)->handleReuploaded(new AssetReuploaded($asset));
app(ClearAssetGlideCache::class)->handleReuploaded(new AssetReuploaded($asset, 'foo.jpg'));
}

#[Test]
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Assets/ReuploadAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ public function glide_cache_is_cleared_and_presets_are_regenerated_when_reupload
Glide::shouldReceive('clearAsset')->withArgs(fn ($arg1) => $arg1->id() === $asset->id())->once()->globally()->ordered();
$this->mock(PresetGenerator::class)->shouldReceive('generate')->withArgs(fn ($arg1) => $arg1->id() === $asset->id())->once()->globally()->ordered();

AssetReuploaded::dispatch($asset);
AssetReuploaded::dispatch($asset, 'test.jpg');
}
}
1 change: 1 addition & 0 deletions tests/Git/GitEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ public function it_commits_when_asset_is_reuploaded()

$file = Mockery::mock(ReplacementFile::class);
$file->shouldReceive('extension')->andReturn('txt');
$file->shouldReceive('basename')->andReturn('file.txt');
$file->shouldReceive('writeTo');

$this->makeAsset()->reupload($file);
Expand Down