Copied to clipboard

Flag this post as spam?

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


  • Anton 135 posts 186 karma points
    Sep 23, 2019 @ 22:53
    Anton
    0

    ApplicationContext in ProcessRequest MediaHandler

    I have such code, but applicationContext is null, how I can get in ProcessRequest function current applicationContext ? I need to GetMediaByPath get works

    public class FileHandler : IHttpHandler
    {
    
        public FileHandler()
        {
    
        }
    
        bool IHttpHandler.IsReusable
        {
            get { return false; }
        }
    
        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            int protectedMediaFolderId = 1388;
    
    
            var applicationContext = Umbraco.Core.ApplicationContext.Current;
            IMedia mediaItem =applicationContext.Services.MediaService.GetMediaByPath(context.Request.FilePath);
    
            bool isProtected = mediaItem.Path.Split(',').ToList().Contains(protectedMediaFolderId.ToString());
    
            switch (context.Request.HttpMethod)
            {
                case "GET":
                    {
                        // Is the user logged-in? 
                        if (!context.User.Identity.IsAuthenticated)
                        {
                            System.Web.HttpContext.Current.Response.Redirect("/");
                            return;
                        }
    
                        string requestedFile = context.Server.MapPath(context.Request.FilePath);
    
                        SendContentTypeAndFile(context, requestedFile);
    
                        break;
                    }
            }
        }
    
    
        HttpContext SendContentTypeAndFile(HttpContext context, String strFile)
        {
            context.Response.ContentType = GetContentType(strFile);
            context.Response.TransmitFile(strFile);
            context.Response.End();
    
            return context;
        }
    
        public string GetContentType(string filename)
        {
            // used to set the encoding for the reponse stream 
            string res = null;
            FileInfo fileinfo = new FileInfo(filename);
    
            if (fileinfo.Exists)
            {
                switch (fileinfo.Extension.Remove(0, 1).ToLower())
                {
                    case "pdf":
                        {
                            res = "application/pdf";
                            break;
                        }
                    case "doc":
                        {
                            res = "application/msword";
                            break;
                        }
                    case "docx":
                        {
                            res = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                            break;
                        }
                    case "xls":
                        {
                            res = "application/vnd.ms-excel";
                            break;
                        }
                    case "xlsx":
                        {
                            res = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                            break;
                        }
                    case "mp3":
                        {
                            res = "audio/mpeg";
                            break;
                        }
                }
    
                return res;
            }
    
            return null;
        }
    
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft