PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Web/HttpHandlers/FileHandler.cs

#
C# | 162 lines | 88 code | 21 blank | 53 comment | 10 complexity | fa50ea20385162526a2fcb1b687b9c5a MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace BlogEngine.Core.Web.HttpHandlers
  2. {
  3. using System;
  4. using System.IO;
  5. using System.Web;
  6. using BlogEngine.Core.Providers;
  7. /// <summary>
  8. /// The FileHandler serves all files that is uploaded from
  9. /// the admin pages except for images.
  10. /// </summary>
  11. /// <remarks>
  12. /// By using a HttpHandler to serve files, it is very easy
  13. /// to add the capability to stop bandwidth leeching or
  14. /// to create a statistics analysis feature upon it.
  15. /// </remarks>
  16. public class FileHandler : IHttpHandler
  17. {
  18. #region Events
  19. /// <summary>
  20. /// Occurs when the requested file does not exist;
  21. /// </summary>
  22. public static event EventHandler<EventArgs> BadRequest;
  23. /// <summary>
  24. /// Occurs when a file is served;
  25. /// </summary>
  26. public static event EventHandler<EventArgs> Served;
  27. /// <summary>
  28. /// Occurs when the requested file does not exist;
  29. /// </summary>
  30. public static event EventHandler<EventArgs> Serving;
  31. #endregion
  32. #region Properties
  33. /// <summary>
  34. /// Gets a value indicating whether another request can use the <see cref = "T:System.Web.IHttpHandler"></see> instance.
  35. /// </summary>
  36. /// <value></value>
  37. /// <returns>true if the <see cref = "T:System.Web.IHttpHandler"></see> instance is reusable; otherwise, false.</returns>
  38. public bool IsReusable
  39. {
  40. get
  41. {
  42. return false;
  43. }
  44. }
  45. #endregion
  46. #region Implemented Interfaces
  47. #region IHttpHandler
  48. /// <summary>
  49. /// Enables processing of HTTP Web requests by a custom HttpHandler that
  50. /// implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
  51. /// </summary>
  52. /// <param name="context">
  53. /// An <see cref="T:System.Web.HttpContext"></see>
  54. /// object that provides references to the intrinsic server objects
  55. /// (for example, Request, Response, Session, and Server) used to service HTTP requests.
  56. /// </param>
  57. public void ProcessRequest(HttpContext context)
  58. {
  59. if (!string.IsNullOrEmpty(context.Request.QueryString["file"]))
  60. {
  61. var fileName = context.Request.QueryString["file"];
  62. OnServing(fileName);
  63. fileName = !fileName.StartsWith("/") ? string.Format("/{0}", fileName) : fileName;
  64. try
  65. {
  66. var file = BlogService.GetFile(string.Format("{0}files{1}",Blog.CurrentInstance.StorageLocation, fileName));
  67. if (file != null)
  68. {
  69. context.Response.AppendHeader("Content-Disposition", string.Format("inline; filename=\"{0}\"", file.Name));
  70. SetContentType(context, file.Name);
  71. if (Utils.SetConditionalGetHeaders(file.DateCreated.ToUniversalTime()))
  72. return;
  73. context.Response.BinaryWrite(file.FileContents);
  74. OnServed(fileName);
  75. }
  76. else
  77. {
  78. OnBadRequest(fileName);
  79. context.Response.Redirect(string.Format("{0}error404.aspx", Utils.AbsoluteWebRoot));
  80. }
  81. }
  82. catch (Exception)
  83. {
  84. OnBadRequest(fileName);
  85. context.Response.Redirect(string.Format("{0}error404.aspx", Utils.AbsoluteWebRoot));
  86. }
  87. }
  88. }
  89. #endregion
  90. #endregion
  91. #region Methods
  92. /// <summary>
  93. /// Called when [bad request].
  94. /// </summary>
  95. /// <param name="file">The file name.</param>
  96. private static void OnBadRequest(string file)
  97. {
  98. if (BadRequest != null)
  99. {
  100. BadRequest(file, EventArgs.Empty);
  101. }
  102. }
  103. /// <summary>
  104. /// Called when [served].
  105. /// </summary>
  106. /// <param name="file">The file name.</param>
  107. private static void OnServed(string file)
  108. {
  109. if (Served != null)
  110. {
  111. Served(file, EventArgs.Empty);
  112. }
  113. }
  114. /// <summary>
  115. /// Called when [serving].
  116. /// </summary>
  117. /// <param name="file">The file name.</param>
  118. private static void OnServing(string file)
  119. {
  120. if (Serving != null)
  121. {
  122. Serving(file, EventArgs.Empty);
  123. }
  124. }
  125. /// <summary>
  126. /// Sets the content type depending on the filename's extension.
  127. /// </summary>
  128. /// <param name="context">
  129. /// The context.
  130. /// </param>
  131. /// <param name="fileName">
  132. /// The file Name.
  133. /// </param>
  134. private static void SetContentType(HttpContext context, string fileName)
  135. {
  136. context.Response.AddHeader(
  137. "Content-Type", fileName.EndsWith(".pdf") ? "application/pdf" : "application/octet-stream");
  138. }
  139. #endregion
  140. }
  141. }