Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using Umbraco.Core;
using Umbraco.Core.Services;
namespace My.Namespace
{
public class EventHandler : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
MediaService.Saving += SetMediaNameToFilename;
}
private void SetMediaNameToFilename(IMediaService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IMedia> e)
{
foreach (var media in e.SavedEntities)
{
var filename = media.GetValue<string>("umbracoFile");
if(string.IsNullOrWhiteSpace(filename))
return;
filename = filename.Substring(filename.LastIndexOf("/", StringComparison.Ordinal) + 1);
media.Name = filename;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.