Ejemplo n.º 1
0
        private XElement ToImgTag(XElement chartElement, int chartID)
        {
            ChartIdentity chartIdentity = new ChartIdentity(m_eventID, m_templateID, chartID);
            ChartData     chartData     = s_chartLookup.GetOrAdd(chartIdentity, ident => new ChartData(chartElement, m_contentHash));

            // If the cancellation token has been initialized, but we are not able to cancel it,
            // it's possible that the action already executed so we attempt to add it back into the lookup
            if ((object)chartData.CancellationToken != null && chartData.CancellationToken.Cancel())
            {
                chartData = s_chartLookup.GetOrAdd(chartIdentity, chartData);
            }

            // If the content hash of the chart does not match the content hash of the email,
            // then the email has changed and we need to update the chart data in the cache
            if (chartData.ContentHash != m_contentHash)
            {
                chartData = s_chartLookup[chartIdentity] = new ChartData(chartElement, m_contentHash);
            }

            // Create a new cancellation token to remove the chart data from the cache in one minute
            chartData.CancellationToken = new Action(() => s_chartLookup.TryRemove(chartIdentity, out chartData)).DelayAndExecute(60 * 1000);

            string url = $"EmailTemplateHandler.ashx?EventID={m_eventID}&TemplateID={m_templateID}&ChartID={chartID}";

            return(new XElement("img", new XAttribute("src", url)));
        }
Ejemplo n.º 2
0
        private void ProcessChartRequest(HttpRequestMessage request, HttpResponseMessage response)
        {
            ChartData chartData;
            XElement  chartElement;
            string    title;

            NameValueCollection parameters = request.RequestUri.ParseQueryString();
            int           chartID          = Convert.ToInt32(parameters["chartID"]);
            ChartIdentity chartIdentity    = new ChartIdentity(m_eventID, m_templateID, chartID);

            if (s_chartLookup.TryGetValue(chartIdentity, out chartData))
            {
                chartElement = chartData.ChartElement;
            }
            else
            {
                XDocument doc = XDocument.Parse(ApplyTemplate(request), LoadOptions.PreserveWhitespace);
                chartElement = doc.Descendants("chart").Skip(chartID).FirstOrDefault();
            }

            if ((object)chartElement == null)
            {
                response.StatusCode = HttpStatusCode.NotFound;
                return;
            }

            title = (string)chartElement.Attribute("yAxisTitle");

            using (DataContext dataContext = new DataContext())
                using (DbAdapterContainer dbAdapterContainer = new DbAdapterContainer((SqlConnection)dataContext.Connection.Connection))
                {
                    response.Content = new StreamContent(ChartGenerator.ConvertToChartImageStream(dbAdapterContainer, chartElement));
                    response.Content.Headers.ContentDisposition          = new ContentDispositionHeaderValue("attachment");
                    response.Content.Headers.ContentDisposition.FileName = title + ".png";
                    response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
                }
        }