Пример #1
0
 public BinaryFileManager()
 {
     //this is a wrong way of using your DI container. has to be done to support this Depracated class.
     CacheAgent = DependencyResolver.Current.GetService<ICacheAgent>();
     LoggerService = DependencyResolver.Current.GetService<ILogger>();
     BinaryFactory = DependencyResolver.Current.GetService<IBinaryFactory>();
 }
Пример #2
0
 public BinaryFileManager()
 {
     //this is a wrong way of using your DI container. has to be done to support this Depracated class.
     CacheAgent    = DependencyResolver.Current.GetService <ICacheAgent>();
     LoggerService = DependencyResolver.Current.GetService <ILogger>();
     BinaryFactory = DependencyResolver.Current.GetService <IBinaryFactory>();
 }
Пример #3
0
        public DefaultLinkResolver(ILinkFactory linkFactory, ILogger logger, IBinaryFactory binaryFactory, IDD4TConfiguration configuration)
        {
            Contract.ThrowIfNull(linkFactory, nameof(linkFactory));
            Contract.ThrowIfNull(binaryFactory, nameof(binaryFactory));
            Contract.ThrowIfNull(logger, nameof(logger));
            Contract.ThrowIfNull(configuration, nameof(configuration));

            _binaryFactory = binaryFactory;
            _linkFactory   = linkFactory;
            _logger        = logger;
            _configuration = configuration;
        }
Пример #4
0
        public static MvcHtmlString ResolveRichText(this string value)
        {
            XmlDocument         doc   = new XmlDocument();
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

            nsmgr.AddNamespace("xhtml", XhtmlNamespaceUri);
            nsmgr.AddNamespace("xlink", XlinkNamespaceUri);
            var encodeValue = HttpUtility.HtmlEncode(value);

            doc.LoadXml(string.Format("<xhtmlroot>{0}</xhtmlroot>", encodeValue));
            // resolve links which haven't been resolved
            foreach (XmlNode link in doc.SelectNodes("//xhtml:a[@xlink:href[starts-with(string(.),'tcm:')]][@xhtml:href='' or not(@xhtml:href)]", nsmgr))
            {
                string tcmuri  = link.Attributes["xlink:href"].Value;
                string linkUrl = _linkFactory.ResolveLink(tcmuri);

                if (!string.IsNullOrEmpty(linkUrl))
                {
                    // linkUrl = HttpHelper.AdjustUrlToContext(linkUrl);
                    // add href
                    XmlAttribute href = doc.CreateAttribute("xhtml:href");
                    href.Value = linkUrl;
                    link.Attributes.Append(href);

                    // remove all xlink attributes
                    foreach (XmlAttribute xlinkAttr in link.SelectNodes("//@xlink:*", nsmgr))
                    {
                        link.Attributes.Remove(xlinkAttr);
                    }
                }
                else
                {
                    //Try and get a binary url for the tcmUri if one exists
                    IBinaryFactory bf = DependencyResolver.Current.GetService <IBinaryFactory>();
                    linkUrl = bf.GetUrlForUri(tcmuri);
                    if (!string.IsNullOrEmpty(linkUrl))
                    {
                        // linkUrl = HttpHelper.AdjustUrlToContext(linkUrl);
                        // add href
                        XmlAttribute href = doc.CreateAttribute("xhtml:href");
                        href.Value = linkUrl;
                        link.Attributes.Append(href);

                        // remove all xlink attributes
                        foreach (XmlAttribute xlinkAttr in link.SelectNodes("//@xlink:*", nsmgr))
                        {
                            link.Attributes.Remove(xlinkAttr);
                        }
                    }
                    else
                    {
                        // copy child nodes of link so we keep them
                        foreach (XmlNode child in link.ChildNodes)
                        {
                            link.ParentNode.InsertBefore(child.CloneNode(true), link);
                        }

                        // remove link node
                        link.ParentNode.RemoveChild(link);
                    }
                }
            }

            // remove any additional xlink attribute
            foreach (XmlNode node in doc.SelectNodes("//*[@xlink:*]", nsmgr))
            {
                foreach (XmlAttribute attr in node.SelectNodes("//@xlink:*", nsmgr))
                {
                    node.Attributes.Remove(attr);
                }
            }

            // add application context path to images
            foreach (XmlElement img in doc.SelectNodes("//*[@src]", nsmgr))
            {
                //if (img.GetAttributeNode("src") != null)
                //    img.Attributes["src"].Value = HttpHelper.AdjustUrlToContext(img.Attributes["src"].Value);
            }

            // fix empty anchors by placing the id value as a text node and adding a style attribute with position:absolute and visibility:hidden so the value won't show up
            foreach (XmlElement anchor in doc.SelectNodes("//xhtml:a[not(node())]", nsmgr))
            {
                XmlAttribute style = doc.CreateAttribute("style");
                style.Value = "position:absolute;visibility:hidden;";
                anchor.Attributes.Append(style);
                anchor.InnerText = anchor.Attributes["id"] != null ? anchor.Attributes["id"].Value : "empty";
            }

            return(new MvcHtmlString(HttpUtility.HtmlDecode(RemoveNamespaceReferences(doc.DocumentElement.InnerXml))));
        }
        /// <summary>
        /// Gets the cached local file for a given URL path.
        /// </summary>
        /// <param name="urlPath">The URL path.</param>
        /// <param name="localization">The Localization.</param>
        /// <returns>The path to the local file.</returns>
        internal string GetCachedFile(string urlPath, Localization localization)
        {
            string localFilePath = GetFilePathFromUrl(urlPath, localization);

            using (new Tracer(urlPath, localization, localFilePath))
            {
                Dimensions dimensions;
                urlPath = StripDimensions(urlPath, out dimensions);

                string   cacheKey          = GetCacheKey(urlPath);
                DateTime?lastPublishedDate = CacheAgent.Load(cacheKey) as DateTime?;
                if (lastPublishedDate == null)
                {
                    IBinaryFactory binaryFactory = DD4TFactoryCache.GetBinaryFactory(localization);
                    try
                    {
                        DateTime lpb = binaryFactory.FindLastPublishedDate(urlPath);
                        if (lpb != DateTime.MinValue.AddSeconds(1)) // this is the secret code for 'does not exist'
                        {
                            lastPublishedDate = lpb;
                            CacheAgent.Store(cacheKey, "Binary", lastPublishedDate);
                        }
                    }
                    catch (NullReferenceException)
                    {
                        //Binary not found, this should return a min date, but theres a bug in DD4T where it throws a NRE
                        //DO NOTHING - binary removed later
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex);
                        if (File.Exists(localFilePath))
                        {
                            //if theres an error connecting, but we still have a version on disk, serve this
                            return(localFilePath);
                        }
                    }
                }

                if (lastPublishedDate != null)
                {
                    if (File.Exists(localFilePath))
                    {
                        if (localization.LastRefresh.CompareTo(lastPublishedDate) < 0)
                        {
                            //File has been modified since last application start but we don't care
                            Log.Debug("Binary with URL '{0}' is modified, but only since last application restart, so no action required", urlPath);
                            return(localFilePath);
                        }
                        FileInfo fi = new FileInfo(localFilePath);
                        if (fi.Length > 0)
                        {
                            DateTime fileModifiedDate = File.GetLastWriteTime(localFilePath);
                            if (fileModifiedDate.CompareTo(lastPublishedDate) >= 0)
                            {
                                Log.Debug("Binary with URL '{0}' is still up to date, no action required", urlPath);
                                return(localFilePath);
                            }
                        }
                    }
                }

                // the normal situation (where a binary is still in Tridion and it is present on the file system already and it is up to date) is now covered
                // Let's handle the exception situations.
                IBinary binary;
                try
                {
                    IBinaryFactory binaryFactory = DD4TFactoryCache.GetBinaryFactory(localization);
                    binaryFactory.TryFindBinary(urlPath, out binary);
                }
                catch (BinaryNotFoundException)
                {
                    // TryFindBinary throws an Exception if not found ?!
                    binary = null;
                }
                catch (Exception ex)
                {
                    throw new DxaException(string.Format("Error loading binary for URL '{0}'", urlPath), ex);
                }

                //For some reason DD4T sometimes returns a non-null binary with null binary data if it doesnt exist
                if (binary == null || binary.BinaryData == null)
                {
                    // Binary does not exist in Tridion, it should be removed from the local file system too
                    if (File.Exists(localFilePath))
                    {
                        CleanupLocalFile(localFilePath);
                    }
                    throw new DxaItemNotFoundException(urlPath, localization.LocalizationId);
                }

                WriteBinaryToFile(binary, localFilePath, dimensions);
                return(localFilePath);
            }
        }