예제 #1
0
        /// <summary>
        /// Handle the media file for http handling of image files.
        /// </summary>
        /// <param name="context"></param>
        public static void HandleMediaFile(HttpContext context)
        {
            string fileName = context.Request.Url.AbsolutePath;

            try
            {
                if (_enableLogging)
                {
                    Logging.Logger.Debug("HandleMediaFile: " + fileName);
                }
                MediaFile file = GetMediaFileContents(fileName);
                if (file != null)
                {
                    if (_enableLogging)
                    {
                        Logging.Logger.Debug("HandleMediaFile: file retrieved : name: " + file.Name + ", id: " + file.Id);
                    }
                    string extension   = file.Extension.Substring(1);
                    string contentType = string.Empty;

                    if (string.Compare(extension, "JPG") == 0)
                    {
                        contentType = "image/jpeg";
                    }
                    else
                    {
                        contentType = "image/" + extension;
                    }

                    int  hashCode    = file.FullName.GetHashCode();
                    bool isThumbnail = fileName.Contains("thumbnail");
                    HandlerHelper.SetHeaders(context, contentType, null, null, hashCode);
                    byte[] buffer = isThumbnail ? file.ContentsForThumbNail : file.Contents;
                    context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                }
                else
                {
                    if (_enableLogging)
                    {
                        Logging.Logger.Info("HandleMediaFile: file NOT found : " + fileName);
                    }
                    context.Response.Redirect("/NotFound");
                }
            }
            catch (Exception ex)
            {
                if (_enableLogging)
                {
                    Logging.Logger.Error("Error handling media file", ex);
                }
                context.Response.Redirect("/NotFound");
            }
        }