コード例 #1
0
        private void CreatedVirtualPath(object sender, UrlBuilderEventArgs urlBuilderEventArgs)
        {
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                return;
            }

            var enabled = CdnConfigurationSection.GetConfiguration().Enabled;

            if (!enabled)
            {
                return;
            }

            object contentReferenceObject;

            if (!urlBuilderEventArgs.RouteValues.TryGetValue(RoutingConstants.NodeKey, out contentReferenceObject))
            {
                return;
            }

            var routedContentLink = contentReferenceObject as ContentReference;

            if (ContentReference.IsNullOrEmpty(routedContentLink))
            {
                return;
            }

            // Check that the link is to a image
            var imageData = DataFactory.Instance.Get <IContent>(routedContentLink) as ImageData;

            if (imageData == null)
            {
                return;
            }

            // Check that everyone has read access to the image
            var securable = imageData as ISecurable;

            if ((securable.GetSecurityDescriptor().GetAccessLevel(PrincipalInfo.AnonymousPrincipal) & AccessLevel.Read) != AccessLevel.Read)
            {
                return;
            }

            // Generate timestamp
            var hash = imageData.Saved.Ticks.ToString("X").Substring(2, 8).ToLower();

            // Generate new url
            var baseUrl = CdnConfigurationSection.GetConfiguration().Url;

            if (string.IsNullOrWhiteSpace(baseUrl))
            {
                baseUrl = SiteDefinition.Current.SiteUrl.AbsoluteUri;
            }
            var cdnPath      = "cdn-" + hash;
            var originalPath = urlBuilderEventArgs.UrlBuilder.Path;
            var cdnUri       = new Uri(baseUrl).Append(cdnPath, originalPath);

            urlBuilderEventArgs.UrlBuilder.Uri = cdnUri;
        }
コード例 #2
0
        private void OnBeginRequest(object sender, EventArgs e)
        {
            var regex = new Regex("^/cdn-[a-z0-9]{8}/.+", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
            var url   = HttpContext.Current.Request.Path;

            if (!regex.IsMatch(url))
            {
                return;
            }
            HttpContext.Current.Items[CdnConfigurationSection.GetConfiguration().ItemKey] = true;
            HttpContext.Current.RewritePath(url.Substring(13), String.Empty, String.Empty, true);
        }
コード例 #3
0
        protected override void SetCachePolicy(HttpContextBase context, DateTime fileChangedDate)
        {
            var isCdnRequest = HttpContext.Current.Items[CdnConfigurationSection.GetConfiguration().ItemKey];

            if (isCdnRequest == null || !(bool)isCdnRequest)
            {
                base.SetCachePolicy(context, fileChangedDate);
                return;
            }

            fileChangedDate = fileChangedDate > DateTime.UtcNow ? DateTime.UtcNow : fileChangedDate;

            context.Response.Cache.SetLastModified(fileChangedDate);
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.Cache.SetExpires(DateTime.UtcNow.AddYears(1));
            context.Response.Cache.SetValidUntilExpires(true);
            context.Response.Cache.VaryByParams.IgnoreParams = true;
            context.Response.Cache.SetOmitVaryStar(true);
            context.Response.Cache.SetNoServerCaching();
        }