예제 #1
0
        protected virtual RazorSourceDocument CreateSourceDocument(PageActionDescriptor actionDescriptor)
        {
            var item = _project.GetItem(actionDescriptor.RelativePath);

            if (item == null)
            {
                throw new InvalidOperationException($"file {actionDescriptor.RelativePath} was not found");
            }

            return(item.ToSourceDocument());
        }
예제 #2
0
        public Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            var baseDomain = context.Request.Scheme + "://" + context.Request.Host + "/";

            var nodes = new List <SitemapNode>();

            var pages = _actionDescriptorCollectionProvider.ActionDescriptors.Items.Where(x => x is PageActionDescriptor);

            foreach (PageActionDescriptor page in pages)
            {
                if (_options.IgnorePathsEndingInIndex && page.ViewEnginePath.EndsWith("/index"))
                {
                    continue;
                }

                if (_ignoreExpression != null && _ignoreExpression.IsMatch(page.ViewEnginePath))
                {
                    continue;
                }

                var node = new SitemapNode(baseDomain + page.AttributeRouteInfo.Template);

                if (_options.BaseLastModOnLastModifiedTimeOnDisk)
                {
                    if (_razorProject.GetItem(page.RelativePath) is FileProviderRazorProjectItem rpi)
                    {
                        node.LastModified = rpi.FileInfo.LastModified.ToUniversalTime().DateTime;
                    }
                }

                nodes.Add(node);
            }

            var sitemap = new Sitemap()
            {
                Nodes = nodes
            };

            context.Response.ContentType = "application/xml";

            var serializer = new XmlSerializer(typeof(Sitemap));

            serializer.Serialize(context.Response.Body, sitemap);

            return(Task.CompletedTask);
        }
예제 #3
0
        // Validates that we can use an existing precompiled view by comparing checksums with files on
        // disk.
        public static bool IsItemValid(RazorProject project, RazorCompiledItem item)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            var checksums = item.GetChecksumMetadata();

            // The checksum that matches 'Item.Identity' in this list is significant. That represents the main file.
            //
            // We don't really care about the validation unless the main file exists. This is because we expect
            // most sites to have some _ViewImports in common location. That means that in the case you're
            // using views from a 3rd party library, you'll always have **some** conflicts.
            //
            // The presence of the main file with the same content is a very strong signal that you're in a
            // development scenario.
            var primaryChecksum = checksums
                                  .FirstOrDefault(c => string.Equals(item.Identifier, c.Identifier, StringComparison.OrdinalIgnoreCase));

            if (primaryChecksum == null)
            {
                // No primary checksum, assume valid.
                return(true);
            }

            var projectItem = project.GetItem(primaryChecksum.Identifier);

            if (!projectItem.Exists)
            {
                // Main file doesn't exist - assume valid.
                return(true);
            }

            var sourceDocument = RazorSourceDocument.ReadFrom(projectItem);

            if (!string.Equals(sourceDocument.GetChecksumAlgorithm(), primaryChecksum.ChecksumAlgorithm) ||
                !ChecksumsEqual(primaryChecksum.Checksum, sourceDocument.GetChecksum()))
            {
                // Main file exists, but checksums not equal.
                return(false);
            }

            for (var i = 0; i < checksums.Count; i++)
            {
                var checksum = checksums[i];
                if (string.Equals(item.Identifier, checksum.Identifier, StringComparison.OrdinalIgnoreCase))
                {
                    // Ignore primary checksum on this pass.
                    continue;
                }

                var importItem = project.GetItem(checksum.Identifier);
                if (!importItem.Exists)
                {
                    // Import file doesn't exist - assume invalid.
                    return(false);
                }

                sourceDocument = RazorSourceDocument.ReadFrom(importItem);
                if (!string.Equals(sourceDocument.GetChecksumAlgorithm(), checksum.ChecksumAlgorithm) ||
                    !ChecksumsEqual(checksum.Checksum, sourceDocument.GetChecksum()))
                {
                    // Import file exists, but checksums not equal.
                    return(false);
                }
            }

            return(true);
        }