Exemple #1
0
        void SendFileFromCMS(HttpRequest request, HttpResponse response, InternalFileManager fileManager, VLFile file)
        {
            Stream contentStream = null;

            try
            {
                var binaryContent = fileManager.GetFileStream(file);

                contentStream = new MemoryStream(binaryContent, 0, binaryContent.Length, false, false);

                StreamContent(request, response, binaryContent.Length, contentStream);
            }
            catch (Exception ex)
            {
                Logger.Error(HttpContext.Current.Request.RawUrl, ex);
                throw;
            }
            finally
            {
                if (contentStream != null)
                {
                    ((IDisposable)contentStream).Dispose();
                }
            }
        }
Exemple #2
0
        void SendFileFromFS(HttpRequest request, HttpResponse response, InternalFileManager fileManager, VLFile file)
        {
            string acceptEncoding  = request.Headers["Accept-Encoding"];
            bool   isGzipOutput    = (!string.IsNullOrWhiteSpace(acceptEncoding) && acceptEncoding.Contains("gzip"));
            bool   isDeflateOutput = (!string.IsNullOrWhiteSpace(acceptEncoding) && acceptEncoding.Contains("deflate"));


            Stream outputStream  = null;
            bool   disposeStream = false;

            try
            {
                string filepath = fileManager.GetFilePath(file);
                string filename = file.ManagedFileName;

                FileInfo fileInfo = new FileInfo(filepath);
                if (fileInfo.Exists)
                {
                    int len = (int)fileInfo.Length;

                    if (len <= TransmitFileUpperLimit)
                    {
                        response.TransmitFile(filepath);
                    }
                    else
                    {
                        StreamContent(request, response, len, System.IO.File.OpenRead(filepath));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(HttpContext.Current.Request.RawUrl, ex);
                throw;
            }
            finally
            {
                if (outputStream != null && disposeStream)
                {
                    ((IDisposable)outputStream).Dispose();
                }
            }
        }
Exemple #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            InternalFileManager manager = new InternalFileManager();

            try
            {
                Guid?fileId = TryParseGuid(context, "fileid", required: false, defValue: null);

                if (fileId.HasValue == false)
                {
                    return;
                }

                //τραβάμε τα στοιχεία του αρχείου απο το σύστημα
                var file = manager.GetFileById(fileId.Value);
                if (file == null)
                {
                    return;
                }

                SendFile(context.Request, context.Response, manager, file);
            }
            catch (ArgumentException ex)
            {
                #region HTTP Error 400 Bad Request
                SendException(context, ex, HttpStatusCode.BadRequest);
                #endregion
            }
            catch (ThreadAbortException)
            {
                //catching the ThreadAbortExceptions from HttpResponse.End() calls...
            }
            catch (Exception ex)
            {
                #region HTTP Error 500 Internal Server Error
                SendException(context, ex, HttpStatusCode.InternalServerError);
                #endregion
            }
        }
Exemple #4
0
        void SendFile(HttpRequest request, HttpResponse response, InternalFileManager fileManager, VLFile file)
        {
            try
            {
                response.ClearHeaders();
                response.Clear();
                response.Cookies.Clear();
                response.Cache.SetCacheability(HttpCacheability.NoCache);
                response.Charset = System.Text.UTF8Encoding.UTF8.WebName;

                String userAgent = HttpContext.Current.Request.Headers.Get("User-Agent");
                if (userAgent.Contains("MSIE"))
                {
                    response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.OriginalFileName.Replace(" ", "_"), System.Text.Encoding.UTF8));
                }
                else
                {
                    response.AppendHeader("Content-Disposition", "attachment; filename=" + file.OriginalFileName.Replace(" ", "_"));
                }

                response.ContentType = GetMimeType(file.OriginalFileName);


                if (!file.IsPhysicalFile || file.IsCompressed || file.IsEncrypted)
                {
                    SendFileFromCMS(request, response, fileManager, file);
                }
                else
                {
                    SendFileFromFS(request, response, fileManager, file);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(HttpContext.Current.Request.RawUrl, ex);
                throw;
            }
        }