Copied to clipboard

Flag this post as spam?

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


  • Ozkan Sayin 6 posts 136 karma points
    Oct 05, 2017 @ 13:31
    Ozkan Sayin
    0

    Custom IImageService (ImageProcessor) with Umbraco

    Hey there,

    I am trying to add a custom IImageService to support serving images from a separate source other than the media library. In short, I want to get a request, something like /logoservice?width=35 and read the logo from somewhere, and return it, using ImageProcessor's capabilities. However, my implementation, which works with a pure MVC project, does not work with Umbraco (7.5.6). I am only getting 404, for the prefix I define. My service looks like this:

    public class LogoService : IImageService
    {
        public LogoService()
        {
            Settings = new Dictionary<string, string>
            {
                {"MaxBytes", "4194304"},
                {"Timeout", "30000"},
                {"Protocol", "https"},
                {"UserAgent", string.Empty}
            };
        }
    
        public string Prefix { get; set; }
    
        public bool IsFileLocalService => false;
    
        public Dictionary<string, string> Settings { get; set; }
    
        public Uri[] WhiteList { get; set; }
    
        public bool IsValidRequest(string path) => true;
    
        public Task<byte[]> GetImage(object id) => Task.Run((Func<byte[]>)GetBytes);
    
        private byte[] GetBytes() => File.ReadAllBytes(@"D:\image.jpg");
    }
    

    In the security.config file, I have added this line to register my service:

    <service name="LogoService" type="MyNameSpace.Logos.LogoService, MyAssembly" prefix="logoservice"/>
    

    So I expect that a call to /logoservice?width=35 would return the logo. I have checked and confirmed that I have the config sections available on the web.config file, along with the registrations of "ImageProcessorModule". I have also moved the module registrations up so that the module works before umbraco catches the request (also tried setting the url in umbracoReservedUrls), did not work.

    It looks like the ImageProcessorModule does not catch the request & serve the response.

    Versions:

    ImageProcessor 2.4.5 (tested updating to 2.5.5, did not work) ImageProcessor.Web 4.6.6

    ImageProcessor.Web.Config 2.3.0.0

    Umbraco version 7.5.6

    Any ideas?

  • Ozkan Sayin 6 posts 136 karma points
    Oct 06, 2017 @ 07:00
    Ozkan Sayin
    100

    Had to dive into the source code to solve this, but finally got it working.

    In short, instead of /logoservice?width=35, I have to use /logoservice?myid?width=35, where myid is being passed into the GetImage method on the service as the id parameter.

    In a bit more detail, it looks like ImageProcessor parses the url manually, and expects query string parameters. If you don't have double question mark (?) in your path, it cannot find any query string parameters, and does not run your service.

    As to why it works fine without double question marks in pure MVC, it might be an update on the logic for the new version. As Umbraco uses an older version, behaviours are a bit different.

    Don't forget to add your path to the umbracoReservedPaths appSetting!

    Hope this helps anyone out there.

Please Sign in or register to post replies

Write your reply to:

Draft