Esempio n. 1
0
        public override async Task ProcessRequestAsync(HttpContext context)
        {
            HttpRequest  Request  = context.Request;
            HttpResponse Response = context.Response;

            byte[] data      = null;
            string mediaName = "";

            int frameId   = Request.IntOrZero("frame");
            int contentId = Request.IntOrZero("content");

            try
            {
                Video            video = new Video(frameId);
                VideoAlternative va    = new VideoAlternative(video, contentId);

                if (va.ContentId != 0)
                {
                    data = await HttpRuntime.Cache.GetOrAddAbsoluteAsync(
                        va.CacheKey,
                        async (expire) =>
                    {
                        expire.When = DateTime.Now.AddMinutes(video.CacheInterval);

                        Content content = await Content.GetDataAsync(va.ContentId);
                        if (content == null)
                        {
                            return(null);
                        }
                        mediaName = content.Name;
                        return(content.Data);
                    });
                }
            }

            catch (Exception ex)
            {
                throw new HttpException(500, ex.Message);
            }


            if (data == null)
            {
                throw new HttpException(404, "File is empty");
            }

            else
            {
                string mimeType = null;

                try
                {
                    if (null == (mimeType = MimeTypeParser.GetMimeTypeRaw(data)))
                    {
                        if (null == (mimeType = MimeTypeParser.GetMimeTypeFromRegistry(mediaName)))
                        {
                            if (null == (mimeType = MimeTypeParser.GetMimeTypeFromList(mediaName)))
                            {
                                mimeType = "application/octet-stream";
                            }
                        }
                    }
                }

                catch
                {
                    mimeType = "application/octet-stream";
                }

                Response.ContentType = mimeType;
                Response.AddHeader("Content-Disposition", string.Format(
                                       "attachment; filename=\"{0}\"",
                                       mediaName
                                       ));
                Response.AddHeader("Content-Length", data.Length.ToString());
            }

            Response.BinaryWrite(data);
            Response.Flush();
        }
Esempio n. 2
0
        public override async Task ProcessRequestAsync(HttpContext context)
        {
            HttpRequest  request  = context.Request;
            HttpResponse response = context.Response;

            int contentId = request.IntOrZero("content");
            int frameId   = request.IntOrZero("frame");
            int trace     = request.IntOrZero("trace");

            try
            {
                // set headers, prevent client caching, return PNG
                response.Clear();
                response.Cache.SetCacheability(HttpCacheability.NoCache);
                response.Cache.SetSlidingExpiration(true);
                response.Cache.SetNoStore();

                //Response.Headers.Add("Pragma-directive", "no-cache");
                //Response.Headers.Add("Cache-directive", "no-cache");
                //Response.Headers.Add("Cache-control", "no-cache");
                //Response.Headers.Add("Pragma", "no-cache");
                //Response.Headers.Add("Expires", "0");

                response.ContentType = "image/png";

                byte[] data = null;

                // images can either come from:
                // 1) frames, in which case they are constraint to panel dimensions, their own rendering mode and cacheing
                // 2) general content, in which case they will come unprocessed and cache for 60 minutes

                if (frameId > 0)
                {
                    Picture picture = new Picture(frameId);

                    if (picture.PanelId != 0)
                    {
                        Panel panel = new Panel(picture.PanelId);

                        data = await HttpRuntime.Cache.GetOrAddAbsoluteAsync(
                            picture.CacheKey,
                            async (expire) =>
                        {
                            expire.When     = DateTime.Now.AddMinutes(picture.CacheInterval);
                            Content content = await Content.GetDataAsync(picture.ContentId);     // new Content(picture.ContentId);
                            if (content.Data == null)
                            {
                                return(null);
                            }
                            using (MemoryStream trg = new MemoryStream())
                                using (MemoryStream src = new MemoryStream(content.Data))
                                {
                                    Picture.WriteImage(src, trg, panel.Width, panel.Height, picture.Mode);
                                    return(trg.GetBuffer());
                                }
                        });
                    }
                }

                else if (contentId != 0)
                {
                    data = await HttpRuntime.Cache.GetOrAddSlidingAsync(
                        string.Format("image_{0}_{1}x{2}_{3}", contentId, -1, -1, (int)RenderModes.RenderMode_Crop),
                        async (expire) =>
                    {
                        expire.After    = TimeSpan.FromMinutes(60);
                        Content content = await Content.GetDataAsync(contentId);     // new Content(contentId);
                        if (content.Data == null)
                        {
                            return(null);
                        }
                        using (MemoryStream trg = new MemoryStream())
                            using (MemoryStream src = new MemoryStream(content.Data))
                            {
                                await Task.Run(() => Picture.WriteImage(src, trg, -1, -1, RenderModes.RenderMode_Crop));
                                return(trg.GetBuffer());
                            }
                    });
                }

                if (data != null)
                {
                    await response.OutputStream.WriteAsync(data, 0, data.Length);
                }

                else
                {
                    Content missingContent = await Content.GetMissingContentAsync();

                    using (MemoryStream ms = new MemoryStream(missingContent.Data))
                    {
                        await Task.Run(() => Picture.WriteImage(ms, response.OutputStream, -1, -1, RenderModes.RenderMode_Crop));
                    }
                }

                await response.OutputStream.FlushAsync();
            }

            catch (Exception ex)
            {
                Debug.Print(string.Format("getImage error: {0}", ex.Message));
                if (trace == 0)
                {
                    response.Write(ex.Message);
                }
                else
                {
                    response.Write(ex.ToString());
                }
            }

            /*finally
             * {
             *  await Response.OutputStream.FlushAsync();
             * }*/
        }