Created
March 10, 2019 22:53
-
-
Save nul800sebastiaan/b9656341c19d254e68aabd3f0692b394 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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