private string ResolveHref(string pathToFile, string originalPathToFile, FileModel model, IDocumentBuildContext context, string propertyName)
        {
            if (!Utility.IsSupportedRelativeHref(pathToFile))
            {
                return(pathToFile);
            }

            var index = pathToFile.IndexOfAny(QueryStringOrAnchor);

            if (index == 0)
            {
                var message = $"Invalid toc link for {propertyName}: {originalPathToFile}.";
                Logger.LogError(message, code: ErrorCodes.Toc.InvalidTocLink);
                throw new DocumentException(message);
            }

            var path     = UriUtility.GetPath(pathToFile);
            var segments = UriUtility.GetQueryStringAndFragment(pathToFile);

            var fli  = FileLinkInfo.Create(model.LocalPathFromRoot, model.File, path, context);
            var href = context.HrefGenerator?.GenerateHref(fli);

            if (fli.ToFileInDest == null && href == null)
            {
                // original path to file can be null for files generated by docfx in PreBuild
                var displayFilePath = string.IsNullOrEmpty(originalPathToFile) ? pathToFile : originalPathToFile;
                Logger.LogInfo($"Unable to find file \"{displayFilePath}\" for {propertyName} referenced by TOC file \"{model.LocalPathFromRoot}\"");
                return(originalPathToFile);
            }

            // fragment and query in original href takes precedence over the one from hrefGenerator
            return(href == null ? fli.Href + segments : UriUtility.MergeHref(href, segments));
        }
 public static bool Create(FileLinkInfo fileLinkInfo)
 {
     if (!Platform.IsWindows)
     {
         return(false);
     }
     return(CreateInWindows(fileLinkInfo));
 }
Exemple #3
0
        /// <summary>
        /// Creates the specified file link.
        /// </summary>
        /// <param name="fileLinkInfo">The file link information.</param>
        /// <returns><c>true</c> if creating the specified file link successfully, <c>false</c> otherwise.</returns>
        public bool Create(FileLinkInfo fileLinkInfo)
        {
            if (fileLinkInfo == null)
            {
                return(false);
            }

            var result = false;

            try
            {
                result = OnCreate(fileLinkInfo);
            }
            catch (Exception e)
            {
                Logger.GetInstance(typeof(LinkManager)).Error(e.ToString());
            }
            return(result);
        }
        public void TestFileLinkInfo_EncodedWorkspaceCharacter()
        {
            string fromFileInSource = "articles/vpn-gateway/vpn-gateway-verify-connection-resource-manager.md";
            string fromFileInDest   = "vpn-gateway/vpn-gateway-verify-connection-resource-manager.html";
            string href             = "%7E/includes/media/vpn-gateway-verify-connection-portal-rm-include/connectionsucceeded.png";
            var    context          = new Build.Engine.DocumentBuildContext("_output");

            var expected = new FileLinkInfo
            {
                FileLinkInDest   = null,
                FileLinkInSource = "~/includes/media/vpn-gateway-verify-connection-portal-rm-include/connectionsucceeded.png",
                FromFileInDest   = "vpn-gateway/vpn-gateway-verify-connection-resource-manager.html",
                FromFileInSource = "articles/vpn-gateway/vpn-gateway-verify-connection-resource-manager.md",
                GroupInfo        = null,
                Href             = "../../includes/media/vpn-gateway-verify-connection-portal-rm-include/connectionsucceeded.png",
                ToFileInDest     = null,
                ToFileInSource   = "includes/media/vpn-gateway-verify-connection-portal-rm-include/connectionsucceeded.png"
            };

            var result = FileLinkInfo.Create(fromFileInSource, fromFileInDest, href, context);

            Assert.Equal(result, expected);
        }
Exemple #5
0
        /// <inheritdoc />
        protected override bool OnCreate(FileLinkInfo fileLinkInfo)
        {
            if (!Platform.IsWindows)
            {
                return(false);
            }

            var sourcePath = fileLinkInfo.SourcePath;

            if (sourcePath == null || !File.Exists(sourcePath.FullName))
            {
                return(false);
            }

            var targetPath = fileLinkInfo.TargetPath;

            if (targetPath == null)
            {
                return(false);
            }
            var targetPathDir = targetPath.Directory;

            if (targetPathDir == null)
            {
                return(false);
            }
            if (!targetPathDir.Exists)
            {
                targetPathDir.Create();
            }

            var targetIconPath  = fileLinkInfo.TargetIconPath;
            var targetIconIndex = fileLinkInfo.TargetIconIndex;

            var guid = new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8");
            var type = Type.GetTypeFromCLSID(guid);

            if (type == null)
            {
                Logger.GetInstance(typeof(DefaultLinkManager)).Error($"Can not find type class from system with CLSID: {guid}");
                return(false);
            }

            object wshShell = null;

            try
            {
                wshShell = Activator.CreateInstance(type);
            }
            catch (Exception e)
            {
                Logger.GetInstance(typeof(DefaultLinkManager)).Error($"Can not create wshShell class from system with CLSID: {guid}, {e.Message}");
            }
            if (wshShell == null)
            {
                return(false);
            }

            object wshShortcut;

            try
            {
                wshShortcut = type.InvokeMember(
                    "CreateShortcut",
                    BindingFlags.InvokeMethod,
                    null,
                    wshShell,
                    new object[] { $"{targetPath.FullName}.lnk" }
                    );
            }
            catch (Exception e)
            {
                Logger.GetInstance(typeof(DefaultLinkManager)).Error($"Can not create wshShortcut class from wshShell: {e.Message}");
                return(false);
            }
            finally
            {
#pragma warning disable CA1416
                Marshal.FinalReleaseComObject(wshShell);
#pragma warning restore CA1416
            }

            try
            {
                type.InvokeMember(
                    "TargetPath",
                    BindingFlags.SetProperty,
                    null,
                    wshShortcut,
                    new object[] { sourcePath.FullName }
                    );
                type.InvokeMember(
                    "IconLocation",
                    BindingFlags.SetProperty,
                    null,
                    wshShortcut,
                    new object[] { targetIconPath.FullName + ", " + targetIconIndex }
                    );
                type.InvokeMember(
                    "Save",
                    BindingFlags.InvokeMethod,
                    null,
                    wshShortcut,
                    null
                    );
                return(true);
            }
            catch (Exception e)
            {
                Logger.GetInstance(typeof(DefaultLinkManager)).Error($"Can not create shortcut: {e.Message}");
            }
            finally
            {
#pragma warning disable CA1416
                Marshal.FinalReleaseComObject(wshShortcut);
#pragma warning restore CA1416
            }
            return(false);
        }
        private static bool CreateInWindows(FileLinkInfo fileLinkInfo)
        {
            if (fileLinkInfo == null)
            {
                return(false);
            }

            var sourcePath      = fileLinkInfo.SourcePath;
            var targetPath      = fileLinkInfo.TargetPath;
            var targetIconPath  = fileLinkInfo.TargetIconPath;
            var targetIconIndex = fileLinkInfo.TargetIconIndex;

            if (sourcePath == null || !sourcePath.Exists || targetPath == null)
            {
                return(false);
            }

            var targetPathDir = targetPath.Directory;

            if (targetPathDir == null)
            {
                return(false);
            }
            if (!targetPathDir.Exists)
            {
                targetPathDir.Create();
            }

            var guid = new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8");
            var type = Type.GetTypeFromCLSID(guid);

            if (type == null)
            {
                Logger.GetInstance(typeof(ShellLink)).Error("Can not find type class from system with CLSID: " + guid);
                return(false);
            }

            object wshShell = null;

            try
            {
                wshShell = Activator.CreateInstance(type);
            }
            catch (Exception e)
            {
                Logger.GetInstance(typeof(ShellLink)).Error("Can not create wshShell class from system with CLSID: " + guid + ", " + e.Message);
            }
            if (wshShell == null)
            {
                return(false);
            }

            object wshShortcut = null;
            var    success     = false;

            try
            {
                wshShortcut = type.InvokeMember(
                    "CreateShortcut",
                    BindingFlags.InvokeMethod,
                    null,
                    wshShell,
                    new object[] { targetPath.FullName + ".lnk" }
                    );
                success = true;
            }
            catch (Exception e)
            {
                Logger.GetInstance(typeof(ShellLink)).Error("Can not create wshShortcut class from wshShell: " + e.Message);
            }
            finally
            {
                Marshal.FinalReleaseComObject(wshShell);
            }
            if (!success)
            {
                return(false);
            }

            success = false;
            try
            {
                type.InvokeMember(
                    "TargetPath",
                    BindingFlags.SetProperty,
                    null,
                    wshShortcut,
                    new object[] { sourcePath.FullName }
                    );
                type.InvokeMember(
                    "IconLocation",
                    BindingFlags.SetProperty,
                    null,
                    wshShortcut,
                    new object[] { targetIconPath.FullName + ", " + targetIconIndex }
                    );
                type.InvokeMember(
                    "Save",
                    BindingFlags.InvokeMethod,
                    null,
                    wshShortcut,
                    null
                    );
                success = true;
            }
            catch (Exception e)
            {
                Logger.GetInstance(typeof(ShellLink)).Error("Can not create shortcut: " + e.Message);
            }
            finally
            {
                Marshal.FinalReleaseComObject(wshShortcut);
            }
            return(success);
        }
Exemple #7
0
 /// <summary>
 /// Called when creating the specified file link.
 /// </summary>
 /// <param name="fileLinkInfo">The file link information.</param>
 /// <returns><c>true</c> if creating the specified file link successfully, <c>false</c> otherwise.</returns>
 protected abstract bool OnCreate(FileLinkInfo fileLinkInfo);