Copied to clipboard

Flag this post as spam?

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


  • Anthony Feudale 5 posts 96 karma points
    Aug 19, 2016 @ 15:37
    Anthony Feudale
    0

    Implementing IImageUrlProvider

    Hi all,

    I am currently in the process of making an extension for Umbraco that will allow media storage in an asset server. To achieve this I need to create a custom class which implements IImageUrlProvider. This new custom class will replace ImageUrlProvider (the defualt Umbraco class) and provide different functionality. I want to have this class examine the Umbraco environment and use a different root for the file path accordingly. Implementing IImageUrlProvider is proving difficult.

    I am following the documentation for Plugins. I have made a class which implements IImageUrlProvider:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.Core.Media;
    
    namespace UmbracoMediaAPITest.IO
    {
        public class TonyImageUrlProvider : IImageUrlProvider
        {
    
        public const string DefaultName = "umbracoUpload";
    
        public TonyImageUrlProvider()
        {
    
        }
        public string Name
        {
            get
            {
                throw new NotImplementedException();
            }
        }
    
        public string GetImageUrlFromFileName(string specifiedSrc, IDictionary<string, string> parameters)
        {
            throw new NotImplementedException();
        }
    
        public string GetImageUrlFromMedia(int mediaId, IDictionary<string, string> parameters)
        {
            throw new NotImplementedException();
        }
    }
    }
    

    I have also made an associated Resolver:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.Core.ObjectResolution;
    using Umbraco.Core.Logging;
    using Umbraco.Core.Media;
    
    namespace UmbracoMediaAPITest.IO
    {
        public class TonyIImageUrlProvidersResolver : ManyObjectsResolverBase<TonyIImageUrlProvidersResolver, IImageUrlProvider>
        {
    
            internal TonyIImageUrlProvidersResolver()
                : base()
            {
            }
    
            public TonyImageUrlProvider GetProvider(string provider)
            {
                return new TonyImageUrlProvider();
            }
    
            public TonyImageUrlProvider GetProvider()
            {
                return new TonyImageUrlProvider();
            }
        }
    } 
    

    Finally I have created a Manager:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.Core;
    using Umbraco.Core.Logging;
    
    namespace UmbracoMediaAPITest.IO
    {
        public class TonyImageBootManager : IBootManager
        {
            public IBootManager Complete(Action<ApplicationContext> afterComplete)
            {
                throw new NotImplementedException();
            }
    
    
    
            public IBootManager Initialize()
            {
                TonyIImageUrlProvidersResolver.Current = new TonyIImageUrlProvidersResolver();
                return this;
            }
    
            public IBootManager Startup(Action<ApplicationContext> afterStartup)
            {
                throw new NotImplementedException();
            }
        }
    }
    

    I am unsure of how to get my Umbraco project to recognize or use any of this however. Do I need to place these files somewhere particular? At the moment they are all sitting in the same folder in my visual studio project.

    Must I change these files somehow? Do I need other files? Do I need to reference them in a config file? I am unable to tell from the documentation.

    These are all tests and unfinished, I'm less concerned with them doing something useful then I am with just having Umbraco recognize them. I can figure out the details once I have something up and running.

    Thanks!

  • Marc Goodson 2138 posts 14321 karma points MVP 8x c-trib
    Aug 20, 2016 @ 11:11
    Marc Goodson
    100

    Hi Anthony

    There is an ImageUrlProviderResolver class in the core who's job it is to keep Umbraco aware what are the available ImageUrlProvider classes, but it appears to be marked as 'Internal' ...

    https://github.com/umbraco/Umbraco-CMS/blob/75c2b07ad3a093b5b65b6ebd45697687c062f62a/src/Umbraco.Web/Media/ImageUrlProviderResolver.cs

    I've never added an ImageUrlProvider before but I have added (for content) a UrlProvider implementing IUrlProvider, but in this case the UrlProviderResolver is marked as 'public' enabling you to override at startup:

     public class RegisterUrlProvider : ApplicationEventHandler
        {
            public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {                UrlProviderResolver.Current.InsertTypeBefore<DefaultUrlProvider, MyCustomUrlProvider>();
            }    
        }
    

    Sorry, it's not very helpful reply, but I'm not sure why the imageUrlProviderResolver isn't public to enable you to do the same, might be worth raising on the issue tracker, (http://issues.umbraco.org/) to see if it's by design or an oversight..

  • Anthony Feudale 5 posts 96 karma points
    Aug 22, 2016 @ 13:18
    Anthony Feudale
    0

    Hey Marc,

    This is the information I needed. I've posted something up on the issues board about it. Hopefully someone will get back to me about whether the resolver is internal by design or error.

    Thanks, Tony

  • James Jackson-South 489 posts 1747 karma points c-trib
    Aug 22, 2016 @ 01:26
    James Jackson-South
    2

    Hi Anthony,

    Managing the storage location of media with Umbraco is done via implementation of the IFileSystem interface and configuring it via the ~/Config/FileSystemProviders.config file.

    Here's a plugin I wrote that stores all media in Azure blob storage. Feel free to grab whatever code you need from there.

    https://github.com/JimBobSquarePants/UmbracoFileSystemProviders.Azure

    Cheers

    James

  • Anthony Feudale 5 posts 96 karma points
    Aug 22, 2016 @ 13:23
    Anthony Feudale
    1

    Hi James,

    I've actually already written such an implementation. I need to modify the Url returned by each object based on its environment, which is why I wanted to override ImageUrlProvider.

    I actually used your FileSystemProvider as a guide while I was creating my custom implementation of the IFileSystem class. It was quite helpful.

    Thanks!

    Tony

  • James Jackson-South 489 posts 1747 karma points c-trib
    Aug 24, 2016 @ 06:38
    James Jackson-South
    0

    Hi Tony,

    Ah cool, I didn't pick up on that requirement. Sorry I can't help further.

    Cheers

    James

Please Sign in or register to post replies

Write your reply to:

Draft