Пример #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 CreatedVirtualPath(object sender, UrlBuilderEventArgs urlBuilderEventArgs)
        {
            var cdnConfiguration = CdnConfigurationSection.GetConfiguration();

            var originalPath = urlBuilderEventArgs.UrlBuilder.Path;

            if (originalPath.StartsWith(cdnConfiguration.AdminPath))
            {
                return;
            }

            var enabled = cdnConfiguration.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 content type is Image or not
            var imageData = DataFactory.Instance.Get <IContent>(routedContentLink) as ImageData;

            if (imageData == null)
            {
                return;
            }

            // Check access rights
            var securable = imageData as ISecurable;

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

            var baseUrl = CdnConfigurationSection.GetConfiguration().Url;

            if (string.IsNullOrWhiteSpace(baseUrl))
            {
                baseUrl = SiteDefinition.Current.SiteUrl.AbsoluteUri;
            }

            urlBuilderEventArgs.UrlBuilder.Uri = new Uri(string.Format("{0}/{1}", baseUrl.TrimEnd('/'), originalPath.TrimStart('/')));
        }
 private void OnCreatedVirtualPath(object sender, UrlBuilderEventArgs e)
 {
     if (IsRequestForMediaInDefaultContext(e))
     {
         var content = GetReadableContentFromRoute(e.RouteValues);
         if (content != null)
         {
             e.UrlBuilder.Uri = _rewriteHelper.GetHashedUri(content, e.UrlBuilder.Path);
         }
     }
 }
        private static void ContentRoute_CreatedVirtualPath(object sender, UrlBuilderEventArgs e)
        {
            if (e.RequestContext.GetContextMode() != ContextMode.Default || !_mediaPaths.Any(p => e.UrlBuilder.Path.StartsWith(p)))
            {
                return;
            }

            var contentLink = e.RouteValues[RoutingConstants.NodeKey] as ContentReference;
            IContent content;
            if (Loader.Service.TryGet(contentLink, out content) && ((ISecurable)content).GetSecurityDescriptor().HasAccess(PrincipalInfo.AnonymousPrincipal, AccessLevel.Read))
            {
                e.UrlBuilder.Uri = new Uri(_cdnUrl + Unique(content) + "/" + e.UrlBuilder.Path, UriKind.RelativeOrAbsolute);
            }
        }
Пример #5
0
        private static void ContentRoute_CreatedVirtualPath(object sender, UrlBuilderEventArgs e)
        {
            if (e.RequestContext.GetContextMode() != ContextMode.Default || !_mediaPaths.Any(p => e.UrlBuilder.Path.StartsWith(p)))
            {
                return;
            }

            var      contentLink = e.RouteValues[RoutingConstants.NodeKey] as ContentReference;
            IContent content;

            if (Loader.Service.TryGet(contentLink, out content) && ((ISecurable)content).GetSecurityDescriptor().HasAccess(PrincipalInfo.AnonymousPrincipal, AccessLevel.Read))
            {
                e.UrlBuilder.Uri = new Uri(_cdnUrl + Unique(content) + "/" + e.UrlBuilder.Path, UriKind.RelativeOrAbsolute);
            }
        }
Пример #6
0
        private static void ContentRoute_CreatedVirtualPath(object sender, UrlBuilderEventArgs e)
        {
            if (e.RequestContext.GetContextMode() != ContextMode.Default || !MediaPaths.Any(p => e.UrlBuilder.Path.StartsWith(p))) { return; }

            var contentLink = e.RouteValues[RoutingConstants.NodeKey] as ContentReference;
            IContent content;

            var loader = ServiceLocator.Current.GetInstance<IContentLoader>();

            if (!loader.TryGet(contentLink, out content)) { return; }

            var imageFile = content as ImageFile;

            if (imageFile == null || imageFile.Protected) { return; }

            var blobLocalPath = imageFile.BinaryData.ID.LocalPath;

            var fullCdnUri = string.Format("{0}/{1}",
                CdnUrl.Value.TrimEnd('/'),
                blobLocalPath.TrimStart('/'));

            e.UrlBuilder.Uri = new Uri(fullCdnUri);
        }
 private bool IsRequestForMediaInDefaultContext(UrlBuilderEventArgs urlBuilderEventArgs)
 {
     return(urlBuilderEventArgs.RequestContext.GetContextMode() == ContextMode.Default && _rewriteHelper.MediaPaths.Any(path => urlBuilderEventArgs.UrlBuilder.Path.StartsWith(path)));
 }