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;
}
}
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
is working on a reply...