示例#1
0
        /// <summary>
        /// Populates the context
        /// </summary>
        /// <param name="context">Context value</param>
        /// <param name="id">ImageGallery Id</param>
        /// <param name="size">Size</param>
        /// <param name="doNotSave">If true, origiginal file will be resized and returned,
        /// and it won't be saved
        /// </param>
        public void PopulateContext(HttpContext context, long imageId, string size, bool doNotSave, bool cropResized)
        {
            FileStream fileStream = null;

            try
            {
                context.Response.Clear();

                FileLibrary.FileInfo fi = _fileLib.GetFileInfo(imageId, size, cropResized);

                if (String.IsNullOrEmpty(fi.Path))
                {
                    return;
                }

                if (!fi.isOnLocal && (string.IsNullOrEmpty(size) || fi.ContentType.IndexOf("image") < 0))
                {
                    context.Response.Redirect(fi.Path);
                    return;
                }

                byte[] bytes;
                //check if the file exists
                context.Response.ContentType = fi.ContentType;
                context.Response.AddHeader("Content-Disposition",
                                           "inline; filename=" + fi.FileName);

                //If the file is not saved on local, and it is an image(checked above)
                //then resize the image, but don't save it
                if (!fi.isOnLocal)
                {
                    HttpWebRequest  req = (HttpWebRequest)WebRequest.Create(fi.Path);
                    HttpWebResponse res = (HttpWebResponse)(req).GetResponse();
                    Stream          sr  = res.GetResponseStream();
                    MemoryStream    mem = new MemoryStream();
                    BinaryWriter    bw  = new BinaryWriter(mem);
                    BinaryReader    br  = new BinaryReader(sr);
                    bw.Write(br.ReadBytes(Convert.ToInt32(res.ContentLength)));

                    bytes = AWAPI_Common.library.ImageLibrary.ResizeImage("", mem, false, 0, 0, size, cropResized);
                    res.Close();
                }
                else if (doNotSave && string.IsNullOrEmpty(size) == false)
                {
                    bytes = AWAPI_Common.library.ImageLibrary.ResizeImage(fi.Path, size, cropResized);
                }
                else if (string.IsNullOrEmpty(size) == false && !File.Exists(fi.PathWithSize))
                {
                    bytes = AWAPI_Common.library.ImageLibrary.ResizeImage(fi.Path, size, cropResized);
                    if (IsImageSizeRegisteredToSave(size))
                    {
                        System.IO.Stream stream = new System.IO.MemoryStream(bytes);
                        AWAPI_BusinessLibrary.library.FileLibrary.SaveFile(fi.SiteId, true, fi.ContentType, fi.PathWithSize, stream, "");
                    }
                }
                else
                {
                    fileStream = new FileStream(fi.PathWithSize, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read);
                    bytes      = new byte[fileStream.Length];
                    //fileStream.Read(bytes, 0, (int)(fileStream.Length - 1));

                    bytes = ReadFile(fi.PathWithSize);

                    //fileStream.Close();
                }

                //write the image to the context
                context.Response.OutputStream.Write(bytes, 0, bytes.Length);
                //context.Response.Flush();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }
        }