So just started some work with an Umbraco 7 site. The site has a custom media type. When adding media (including by dragging and dropping) and selecting this custom type the full path/url of the file added does not appear to be stored anywhere.
I've added a listener to the MediaService.Saved event and this is firing but within this method all the information I appear to have available is the id and the name of the file rather than the file itself.
I was expecting the "umbracoFile" property to be automatically populated but that doesn't appear to be the case. [I even tried editing my custom media type to have a property with alias "umbracoFile" but that just causes the Backend to crash].
Is there anyway to get the url/path of the file or to force Umbraco to set the "umbracoFile" property?
Got something working in the end thanks to Robert's answer - it's fairly hacky but appears to work so I'm going to leave it here in case it helps anyone else.
Note that it uses a depreciated event handler and reflection to set private variables so I can't recommend that anyone else use it, but it might give people an idea where to start:
public void MediaService_Creating(IMediaService sender, NewEventArgs<IMedia> e)
{
int i = 0;
Type t = e.Entity.GetType();
FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
FieldInfo field = fields.First(x => x.Name == "_contentType");
MethodInfo findMediaTypeByAlias = ApplicationContext.Current.Services.MediaService
.GetType().GetMethod("FindMediaTypeByAlias", BindingFlags.NonPublic | BindingFlags.Instance);
IMediaType mediaType = (IMediaType)findMediaTypeByAlias.Invoke(
ApplicationContext.Current.Services.MediaService,
new object[] { Constants.Conventions.MediaTypes.Image });
field.SetValue(e.Entity, mediaType);
field = fields.First(x => x.Name == "ContentTypeBase");
field.SetValue(e.Entity, mediaType);
i = e.Entity.ContentTypeId;
}
The basic premise is to change the media type to Image whilst the media type is being created. By changing it in this way any extra properties for the Image media type get added and automatically populated. If a property on the custom media type shares an alias with one of the Image media type properties (such as umbracoFile) then that properties value is automatically populated meaning that it can be used in any Saving/Saved event listeners as required.