public async Task <IActionResult> GetHashedFileName(string hash, string filePath)
        {
            filePath = System.Net.WebUtility.UrlDecode(filePath);
            if (_md5HashRegex.Matches(hash).Count == 1 && !string.IsNullOrWhiteSpace(filePath) &&
                !filePath.Contains("..") && _filePathRegex.IsMatch(filePath))
            {
                var hashEntry = await ContentHash.GetContentHashEntryForFile(Startup.ContentRootPath, "wwwroot", filePath, Helpers.Html.GetAbsoluteHashPath, true);

                if (hashEntry != null)
                {
                    if (hashEntry.Hash.Equals(hash))
                    {
                        string contentType;
                        new FileExtensionContentTypeProvider().TryGetContentType(hashEntry.FullPath, out contentType);
                        return(this.File(hashEntry.Transformable ? hashEntry.Transformation : System.IO.File.ReadAllBytes(hashEntry.FullPath), contentType));
                    }
                    else
                    {
                        return(RedirectPermanent(Helpers.Html.GetAbsoluteHashPath(hashEntry.Hash, hashEntry.WebPath)));
                    }
                }
                return(NotFound());
            }
            return(BadRequest());
        }
        public async Task <IActionResult> GetHashedFileName(string hash, string filePath)
        {
            Console.WriteLine($"Hash Request Received for {hash}/{filePath}");
            filePath = System.Net.WebUtility.UrlDecode(filePath);
            if (_md5HashRegex.Matches(hash).Count == 1 && !string.IsNullOrWhiteSpace(filePath) &&
                !filePath.Contains("..") && _filePathRegex.IsMatch(filePath))
            {
                var hashEntry = await ContentHash.GetContentHashEntryForFile(Startup.ContentRootPath, "wwwroot", filePath, true);

                if (hashEntry != null)
                {
                    if (hashEntry.Hash.Equals(hash))
                    {
                        string contentType;
                        new FileExtensionContentTypeProvider().TryGetContentType(hashEntry.FullPath, out contentType);
                        return(this.File(System.IO.File.ReadAllBytes(hashEntry.FullPath), contentType));
                    }
                    else
                    {
                        var urlHelper = new UrlHelper(ControllerContext);
                        var hashUrl   = System.Net.WebUtility.UrlDecode(urlHelper.Action("GetHashedFileName", "hash", new {
                            hash     = hashEntry.Hash,
                            filePath = hashEntry.WebPath
                        }));
                        return(RedirectPermanent(hashUrl));
                    }
                }
                return(NotFound());
            }
            return(BadRequest());
        }
Пример #3
0
        public static string MarkupArticle(string secdata, bool forum, bool frontPage = false)
        {
            var pipelineBuilder = new MarkdownPipelineBuilder()
                                  .UseAdvancedExtensions();

            if (forum)
            {
                pipelineBuilder.DisableHtml();
            }
            var pipeline = pipelineBuilder.Build();

            //Parse and Render Markup while applying special optimizations
            //Shamelessly ripped off from https://github.com/lunet-io/markdig/issues/293#issuecomment-456376415
            var document = Markdown.Parse(secdata, pipeline);

            foreach (var descendant in document.Descendants())
            {
                string url     = null;
                bool   isImage = false;
                if (descendant is AutolinkInline autoLink)
                {
                    url = autoLink.Url;
                }
                else if (descendant is LinkInline linkInline)
                {
                    url     = linkInline.Url;
                    isImage = linkInline.IsImage;
                    if (isImage && !Shared.Helpers.IsUrlAbsolute(url))
                    {
                        var imageUrl       = url.TrimStart('/');
                        var imageHashEntry = ContentHash.GetContentHashEntryForFile(SystemInfoHelpers.ContentRootPath, "wwwroot", imageUrl, Html.GetRelativeHashPath, true).Result;
                        if (imageHashEntry != null)
                        {
                            linkInline.Url = url = Html.GetRelativeHashPath(imageHashEntry.Hash, imageHashEntry.WebPath);
                        }
                    }
                }
                if (!isImage && Shared.Helpers.IsUrlAbsolute(url))
                {
                    descendant.GetAttributes().AddPropertyIfNotExist("target", "_blank");
                }
            }
            using (var writer = new StringWriter())
            {
                var renderer = new HtmlRenderer(writer);
                pipeline.Setup(renderer);
                renderer.Render(document);

                //Return finalized HTML
                if (!frontPage || !Settings.Current.SummaryModeFront)
                {
                    return(writer.ToString());
                }
                return(writer.ToString().DataTruncate(-3));
            }
        }
Пример #4
0
        public static HtmlString ContentHashFile(this IHtmlHelper htmlHelper, string webPath)
        {
            var urlHelper = new UrlHelper(htmlHelper.ViewContext);
            var hashEntry = ContentHash.GetContentHashEntryForFile(Startup.ContentRootPath, "wwwroot", webPath).Result;
            var hashUrl   = System.Net.WebUtility.UrlDecode(urlHelper.Action("GetHashedFileName", "hash", new
            {
                hash     = hashEntry.Hash,
                filePath = hashEntry.WebPath
            }));

            return(new HtmlString(hashUrl));
        }
Пример #5
0
        public static HtmlString InlineContent(this IHtmlHelper htmlHelper, string filePath)
        {
            filePath = System.Net.WebUtility.UrlDecode(filePath);
            if (!string.IsNullOrWhiteSpace(filePath))
            {
                var hashEntry = ContentHash.GetContentHashEntryForFile(Startup.ContentRootPath, "wwwroot", filePath, Helpers.Html.GetAbsoluteHashPath, true).Result;

                string contentType;
                new FileExtensionContentTypeProvider().TryGetContentType(hashEntry.FullPath, out contentType);
                return(new HtmlString(Encoding.UTF8.GetString(hashEntry.Transformable ? hashEntry.Transformation : System.IO.File.ReadAllBytes(hashEntry.FullPath))));
            }
            return(new HtmlString(string.Empty));
        }
Пример #6
0
        public static HtmlString ContentHashFile(this IHtmlHelper htmlHelper, string webPath
                                                 , bool checkForExistence = false)
        {
            var    urlHelper = new UrlHelper(htmlHelper.ViewContext);
            var    hashEntry = ContentHash.GetContentHashEntryForFile(Startup.ContentRootPath, "wwwroot", webPath, GetAbsoluteHashPath, checkForExistence).Result;
            string hashUrl   = string.Empty;

            if (!checkForExistence || hashEntry != null)
            {
                hashUrl = GetRelativeHashPath(hashEntry.Hash, hashEntry.WebPath);
            }
            return(new HtmlString(hashUrl.TrimStart('/')));
        }