Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@daun
Copy link
Contributor

@daun daun commented Feb 6, 2025

Suggested change

Pass the original filename into the AssetUploaded and AssetReuploaded events.

Use case

This will allow saving the original filename into the asset metadata from a custom listener. Mostly applicable to download buttons or zip features that benefit from keeping the original name including capitalization and any © marks.

{{ assets }}
  <a href="{{ url }}" download="{{ original_filename }}">Download</a>
{{ /assets }}
<a href="image--photographer.jpg" download="Image © Photographer.jpg">Download</a>
class OriginalFilenameSubscriber extends Subscriber
{
    protected $listeners = [
        AssetUploaded::class => 'saveOriginalFilename',
    ];

    public function saveOriginalFilename(AssetUploaded $event)
    {
        $event->asset->set('original_filename', $event->originalFilename);
        $event->asset->saveQuietly();
    }
}

Semver

  • Technically, this is only adding an argument, so it should be non-breaking
  • However, any addons/consumers "faking" those Events will get errors from the now-missing required argument

Alternatives considered

  • Just save the original filename into the asset meta on creation? Wouldn't require updates to the event signatures
  • Make filename sanitization hookable and somehow match the change to the asset? Sounds a bit hacky

Closes

Closes statamic/ideas#1282

@jasonvarga
Copy link
Member

jasonvarga commented Feb 6, 2025

Can't you already do $asset->getOriginal('path')? Sorry I misunderstood what this PR was actually doing. 🤐

@daun
Copy link
Contributor Author

daun commented Feb 6, 2025

The asset is created with the filename already sanitized, so unfortunately no :)

@daun
Copy link
Contributor Author

daun commented Nov 25, 2025

@jasonvarga We have an upcoming project with a hard requirement to store the original, unsanitized filenames of uploaded assets. About to get started on it, in fact.

Is the approach in this PR something you're willing to consider? Or would you prefer a solution that runs a hook during filename sanitization? Third option would be to have a native original_filename property in each asset meta alongside size, width, etc.

If this is something you'd rather not have in the core at all, that's cool too, it'd just be good to know so we can discuss options with the client before getting started :)

@jasonvarga jasonvarga merged commit 4b469b9 into statamic:5.x Nov 30, 2025
22 checks passed
@daun daun deleted the feature/uploaded-asset-original-file branch December 1, 2025 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remember the original filename of uploaded assets

2 participants