RadioDld.RssServer.DownloadContent C# (CSharp) Method

DownloadContent() private method

private DownloadContent ( HttpListenerContext context ) : void
context System.Net.HttpListenerContext
return void
        private void DownloadContent(HttpListenerContext context)
        {
            Regex epidPattern = new Regex(@"^\?epid=([0-9]+)$");
            Match match = epidPattern.Match(context.Request.Url.Query);

            int epid;

            if (!int.TryParse(match.Groups[1].Value, out epid))
            {
                // Request doesn't contain a valid episode id
                this.ErrorPage404(context);
                return;
            }

            Model.Download download;

            try
            {
                download = new Model.Download(epid);
            }
            catch (DataNotFoundException)
            {
                // Specified download does not exist
                this.ErrorPage404(context);
                return;
            }

            FileInfo file = new FileInfo(download.DownloadPath);

            if (!file.Exists)
            {
                // File for specified download no-longer exists
                this.ErrorPage404(context);
                return;
            }

            context.Response.ContentType = this.MimeTypeForFile(download.DownloadPath);
            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
            context.Response.ContentLength64 = file.Length;

            using (FileStream fs = File.OpenRead(download.DownloadPath))
            {
                byte[] buffer = new byte[4096];
                int count;

                try
                {
                    while ((count = fs.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        context.Response.OutputStream.Write(buffer, 0, count);
                    }
                }
                catch (HttpListenerException)
                {
                    // Don't worry about dropped connections etc.
                }
            }
        }