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();
}
}
}
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();
}
}
}
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.
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' ...
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..
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.
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.
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.
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 replaceImageUrlProvider
(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. ImplementingIImageUrlProvider
is proving difficult.I am following the documentation for
Plugins
. I have made a class which implementsIImageUrlProvider
:I have also made an associated
Resolver
:Finally I have created a
Manager
: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!
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:
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..
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
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
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 theIFileSystem
class. It was quite helpful.Thanks!
Tony
Hi Tony,
Ah cool, I didn't pick up on that requirement. Sorry I can't help further.
Cheers
James
is working on a reply...