Ejemplo n.º 1
0
 private static void SendFile(HttpContext context, Rock.CMS.File file)
 {
     context.Response.ContentType = file.MimeType;
     context.Response.AddHeader("content-disposition", "inline;filename=" + file.FileName);
     context.Response.BinaryWrite(file.Data);
     context.Response.Flush();
 }
Ejemplo n.º 2
0
        private static bool FetchFromCache(Rock.CMS.File file, string physFilePath)
        {
            bool cached = false;

            if (System.IO.File.Exists(physFilePath) && file.CreatedDateTime < System.IO.File.GetCreationTime(physFilePath))
            {
                try
                {
                    using (BinaryReader binReader = new BinaryReader(System.IO.File.Open(physFilePath, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read)))
                    {
                        file.Data = System.IO.File.ReadAllBytes(physFilePath);
                    }
                    cached = true;
                }
                catch { /* ok, so we'll just skip using the cache, but TODO: log this */ }
            }
            return(cached);
        }
Ejemplo n.º 3
0
        private static void ResizeAndCache(HttpContext context, Rock.CMS.File file, string physFilePath)
        {
            ResizeSettings settings      = new ResizeSettings(context.Request.QueryString);
            MemoryStream   resizedStream = new MemoryStream();

            ImageBuilder.Current.Build(new MemoryStream(file.Data), resizedStream, settings);
            file.Data = resizedStream.GetBuffer();

            // now cache the file for later use
            try
            {
                using (BinaryWriter binWriter = new BinaryWriter(System.IO.File.Open(physFilePath, FileMode.Create)))
                {
                    binWriter.Write(file.Data);
                }
            }
            catch { /* do nothing, not critical if this fails, although TODO: log */ }
        }
Ejemplo n.º 4
0
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.QueryString == null || context.Request.QueryString.Count == 0)
            {
                return;
            }

            context.Response.Clear();

            FileService fileService = new FileService();
            string      anID        = context.Request.QueryString[0];
            int         id;

            try
            {
                // Fetch the file...
                Rock.CMS.File file = (int.TryParse(anID, out id)) ? fileService.Get(id) : fileService.GetByEncryptedKey(anID);

                // is it cached?
                string cacheName    = Uri.EscapeDataString(context.Request.Url.Query);
                string physFilePath = context.Request.MapPath(string.Format("~/Cache/{0}", cacheName));
                bool   cached       = FetchFromCache(file, physFilePath);

                // Image resizing requested?
                if (!cached && WantsImageResizing(context))
                {
                    ResizeAndCache(context, file, physFilePath);
                }

                // Post process
                SendFile(context, file);
            }
            catch (Exception ex)
            {
                // TODO: log this error
                context.Response.StatusDescription = ex.Message;
                context.Response.StatusCode        = 500;
                context.Response.End();
            }
        }