PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mojoPortal.Features.UI/SharedFiles/Download.aspx.cs

#
C# | 190 lines | 132 code | 40 blank | 18 comment | 22 complexity | 9f0cf42e714f7927df808dcb745425c6 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause, CPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. /// Author: Joe Audette
  2. /// Created: 2007-01-28
  3. /// Last Modified: 2011-08-20
  4. ///
  5. /// The use and distribution terms for this software are covered by the
  6. /// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  7. /// which can be found in the file CPL.TXT at the root of this distribution.
  8. /// By using this software in any fashion, you are agreeing to be bound by
  9. /// the terms of this license.
  10. ///
  11. /// You must not remove this notice, or any other, from this software.
  12. using System;
  13. using System.IO;
  14. using System.Globalization;
  15. using System.Text;
  16. using System.Web;
  17. using mojoPortal.Business;
  18. using mojoPortal.Business.WebHelpers;
  19. using mojoPortal.FileSystem;
  20. using mojoPortal.Web.Framework;
  21. using log4net;
  22. namespace mojoPortal.Web.SharedFilesUI
  23. {
  24. public partial class SharedFilesDownload : NonCmsBasePage
  25. {
  26. private static readonly ILog log = LogManager.GetLogger(typeof(SharedFilesDownload));
  27. #region OnInit
  28. override protected void OnInit(EventArgs e)
  29. {
  30. this.Load += new EventHandler(this.Page_Load);
  31. base.OnInit(e);
  32. }
  33. #endregion
  34. private IFileSystem fileSystem = null;
  35. private int pageID = -1;
  36. private int moduleID = -1;
  37. private int fileID = -1;
  38. private SharedFile sharedFile = null;
  39. protected void Page_Load(object sender, EventArgs e)
  40. {
  41. SecurityHelper.DisableDownloadCache();
  42. //Server.ScriptTimeout
  43. if (
  44. (LoadAndCheckParams())
  45. && (UserCanViewPage(moduleID, SharedFile.FeatureGuid))
  46. )
  47. {
  48. DownloadFile();
  49. }
  50. else
  51. {
  52. if (!Request.IsAuthenticated)
  53. {
  54. SiteUtils.RedirectToLoginPage(this);
  55. return;
  56. }
  57. else
  58. {
  59. SiteUtils.RedirectToAccessDeniedPage(this);
  60. return;
  61. }
  62. }
  63. }
  64. private void DownloadFile()
  65. {
  66. if (
  67. (CurrentPage != null)
  68. &&(sharedFile != null)
  69. )
  70. {
  71. string virtualPath = "~/Data/Sites/" + this.CurrentPage.SiteId.ToInvariantString() + "/SharedFiles/"
  72. + sharedFile.ServerFileName;
  73. if (fileSystem.FileExists(virtualPath))
  74. {
  75. //FileInfo fileInfo = new System.IO.FileInfo(downloadPath);
  76. WebFile fileInfo = fileSystem.RetrieveFile(virtualPath);
  77. Page.Response.AppendHeader("Content-Length", fileInfo.Size.ToString(CultureInfo.InvariantCulture));
  78. }
  79. else
  80. {
  81. log.Error("Shared File Not Found. User tried to download file " + virtualPath);
  82. return;
  83. }
  84. string fileType = Path.GetExtension(sharedFile.FriendlyName).Replace(".", string.Empty);
  85. string mimeType = SiteUtils.GetMimeType(fileType);
  86. //Page.Response.ContentType = mimeType;
  87. Page.Response.ContentType = "application/" + fileType;
  88. if ((!SharedFilesConfiguration.TreatPdfAsAttachment) && (SiteUtils.IsNonAttacmentFileType(fileType)))
  89. {
  90. //this will display the pdf right in the browser
  91. Page.Response.AddHeader("Content-Disposition", "filename=\"" + HttpUtility.UrlEncode(sharedFile.FriendlyName, Encoding.UTF8) + "\"");
  92. }
  93. else
  94. {
  95. // other files just use file save dialog
  96. Page.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(sharedFile.FriendlyName, Encoding.UTF8) + "\"");
  97. }
  98. //Page.Response.AddHeader("Content-Length", documentFile.DocumentImage.LongLength.ToString());
  99. try
  100. {
  101. Page.Response.Buffer = false;
  102. Page.Response.BufferOutput = false;
  103. if (Page.Response.IsClientConnected)
  104. {
  105. //Page.Response.TransmitFile(downloadPath);
  106. using (System.IO.Stream stream = fileSystem.GetAsStream(virtualPath))
  107. {
  108. stream.CopyTo(Page.Response.OutputStream);
  109. SharedFile.IncrementDownloadCount(sharedFile.ItemId);
  110. }
  111. try
  112. {
  113. Page.Response.End();
  114. }
  115. catch (System.Threading.ThreadAbortException) { }
  116. }
  117. }
  118. catch (HttpException) { }
  119. }
  120. }
  121. private bool LoadAndCheckParams()
  122. {
  123. bool result = true;
  124. pageID = WebUtils.ParseInt32FromQueryString("pageid", -1);
  125. moduleID = WebUtils.ParseInt32FromQueryString("mid", -1);
  126. fileID = WebUtils.ParseInt32FromQueryString("fileid", -1);
  127. if (pageID == -1) result = false;
  128. if (moduleID == -1) result = false;
  129. if (fileID == -1) result = false;
  130. if (result)
  131. {
  132. sharedFile = new SharedFile(moduleID, fileID);
  133. result = (sharedFile.ModuleId == moduleID);
  134. FileSystemProvider p = FileSystemManager.Providers[WebConfigSettings.FileSystemProvider];
  135. if (p == null)
  136. {
  137. log.Error("Could not load file system provider " + WebConfigSettings.FileSystemProvider);
  138. result = false;
  139. }
  140. fileSystem = p.GetFileSystem();
  141. if (fileSystem == null)
  142. {
  143. log.Error("Could not load file system from provider " + WebConfigSettings.FileSystemProvider);
  144. result = false;
  145. }
  146. }
  147. return result;
  148. }
  149. }
  150. }