Umbraco 9 Media Notifications: how to obtain actual Image?
Hello
In Umbraco 9 I want to use the MediaSavingNotification publishing event to set an additional property on my Image (determine a dominant colour via some custom code plugged into the notification event). The trouble I’m having is that the media entity only has a “umbracoFile” property containing the relative url of the image in the src. I would like to obtain either the absolute path of the image or the byte array of the image. Ive been going down a rabbit hole trying to use either MediaFileManager or IHostingEnvironment successfully.
Does anyone have a convenient way of getting either the stream/byte array of the image or the full path (UNC) during the a media notification event?
I'm not a backend developer. Does this get you anywhere closer?
internal class MediaSaving : INotificationAsyncHandler<MediaSavingNotification>
{
private readonly MediaFileManager _mediaFileManager;
/// <summary>
/// Find the correct implementation for IFileSystem via MediaFileManager.
/// </summary>
/// <param name="mediaFileManager"></param>
public MediaSaving(MediaFileManager mediaFileManager)
{
_mediaFileManager = mediaFileManager;
}
/// <summary>
/// Called prior to media being saved in the backoffice.
/// </summary>
/// <param name="notification"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task HandleAsync(MediaSavingNotification notification, CancellationToken cancellationToken)
{
foreach (var node in notification.SavedEntities)
{
using (Stream stream = _mediaFileManager.GetFile(node, out string mediaFilePath))
{
// Should have access to the stream for your image now.
}
}
return Task.CompletedTask;
}
}
Umbraco 9 Media Notifications: how to obtain actual Image?
Hello
In Umbraco 9 I want to use the MediaSavingNotification publishing event to set an additional property on my Image (determine a dominant colour via some custom code plugged into the notification event). The trouble I’m having is that the media entity only has a “umbracoFile” property containing the relative url of the image in the src. I would like to obtain either the absolute path of the image or the byte array of the image. Ive been going down a rabbit hole trying to use either MediaFileManager or IHostingEnvironment successfully.
Does anyone have a convenient way of getting either the stream/byte array of the image or the full path (UNC) during the a media notification event?
Any tips appreciated 😊
https://our.umbraco.com/documentation/reference/Notifications/MediaService-Notifications
I'm not a backend developer. Does this get you anywhere closer?
That was a great help Mark - cheers!
I saw that GetFile method but didn't equate its IContentBase signature with the IMedia item, but makes sense now that i see an example.
Might be a good reference for anyone else coming across this thread
thanks!
is working on a reply...