コード例 #1
0
        /// <summary>
        /// If CDN is enabled for this request, replace the outgoing media url with the cdn version
        /// </summary>
        /// <param name="item"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public override string GetMediaUrl(Sitecore.Data.Items.MediaItem item, MediaUrlOptions options)
        {
            string hostname = CDNManager.GetCDNHostName();
            string url      = base.GetMediaUrl(item, options);

            bool shouldReplace = !string.IsNullOrEmpty(hostname) &&           // cdnHostname exists for site
                                 Sitecore.Context.PageMode.IsNormal;          // PageMode is normal

            bool dontReplace = !CDNManager.IsMediaPubliclyAccessible(item) || // media is publicly accessible
                               CDNManager.IsMediaAnalyticsTracked(item);      // media is analytics tracked

            CDNUrlState contextState = CDNUrlSwitcher.CurrentValue;

            if (contextState == CDNUrlState.Enabled)
            {
                shouldReplace = true;
            }
            else if (contextState == CDNUrlState.Disabled)
            {
                UrlString url2 = new UrlString(url);
                url2[CDNManager.StopToken] = "1";
                url           = url2.ToString();
                shouldReplace = false;
            }


            if (shouldReplace && !dontReplace) // media not DMS tracked
            {
                return(CDNManager.ReplaceMediaUrl(url, hostname));
            }
            else
            {
                return(url);
            }
        }
コード例 #2
0
        public override void Process(HttpRequestArgs args)
        {
            Assert.ArgumentNotNull(args, "args");

            if (!CDNSettings.Enabled)
            {
                return;
            }

            bool shouldFilter = (Sitecore.Context.Item != null || CDNManager.ShouldProcessRequest(args.Url.FilePathWithQueryString)) && // if an item is resolved (this is a page request) or file ext is listed in <processRequests>
                                !CDNManager.ShouldExcludeProcessRequest(args.Url.FilePathWithQueryString) &&                            // if the url is not on the excluded list
                                Sitecore.Context.Site != null &&                                                                        // and a site was resolved
                                Sitecore.Context.PageMode.IsNormal &&                                                                   // and the site is not in editing mode
                                !string.IsNullOrEmpty(CDNManager.GetCDNHostName());                                                     // and the current site is not in the excluded sites list

            // querystring cdn=1  to force replacement
            // querystring cdn=0  to force no replacement
            Tristate force = MainUtil.GetTristate(WebUtil.GetQueryString("cdn"), Tristate.Undefined);

            if (force == Tristate.False)
            {
                shouldFilter = false;
            }
            else if (force == Tristate.True)
            {
                shouldFilter = true;
            }

            if (shouldFilter)
            {
                var response = HttpContext.Current.Response;
                if (response != null)
                {
                    // replace the default response filter stream with our replacer filter
                    response.Filter = new MediaUrlFilter(response.Filter);
                }
            }
        }