Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • andrew shearer 513 posts 663 karma points
    Feb 14, 2022 @ 09:17
    andrew shearer
    0

    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

  • Mark Drake 134 posts 458 karma points c-trib
    Feb 14, 2022 @ 18:54
    Mark Drake
    1

    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;
        }
    }
    
  • andrew shearer 513 posts 663 karma points
    Feb 14, 2022 @ 21:06
    andrew shearer
    0

    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!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies