public static string GetLink(NormalizedPath path, string host, DirectoryPath root, bool hideIndexPages, bool hideExtensions) { // Remove index pages and extensions if a file path FilePath filePath = path as FilePath; if (filePath != null) { if (hideIndexPages && (filePath.FileName.FullPath == "index.htm" || filePath.FileName.FullPath == "index.html")) { path = filePath.Directory; } else if (hideExtensions) { path = filePath.ChangeExtension(null); } } // Collapse the link to a string string link = string.Empty; if(path != null) { link = path.FullPath; if (string.IsNullOrWhiteSpace(link) || link == ".") { link = "/"; } if (!link.StartsWith("/")) { link = "/" + link; } } // Collapse the root and combine string rootLink = root == null ? string.Empty : root.FullPath; if (rootLink.EndsWith("/")) { rootLink = rootLink.Substring(0, rootLink.Length - 1); } // Add the host and convert to URI for escaping UriBuilder builder = new UriBuilder { Path = rootLink + link }; bool hasHost = false; if (!string.IsNullOrWhiteSpace(host)) { builder.Host = host; hasHost = true; } Uri uri = builder.Uri; return hasHost ? uri.AbsoluteUri : uri.AbsolutePath; }
public override bool Equals(object obj) { NormalizedPath other = obj as NormalizedPath; if (other == null) { return(false); } if (IsAbsolute && other.IsAbsolute && Provider != other.Provider) { return(false); } return(FullPath.Equals(other.FullPath)); }
public IFileProvider GetFileProvider(NormalizedPath path) { if (path == null) { throw new ArgumentNullException(nameof(path)); } if (path.IsRelative) { throw new ArgumentException("The path must be absolute"); } if (path.FileProvider == null) { throw new ArgumentException("The path has no provider"); } IFileProvider fileProvider; if (!FileProviders.TryGet(path.FileProvider.Scheme, out fileProvider)) { throw new KeyNotFoundException($"A provider for the scheme {path.FileProvider} could not be found"); } return fileProvider; }
public DirectoryPath GetContainingInputPath(NormalizedPath path) { if (path == null) { throw new ArgumentNullException(nameof(path)); } if (path.IsAbsolute) { return InputPaths .Reverse() .Select(x => RootPath.Combine(x)) .FirstOrDefault(x => x.FileProvider == path.FileProvider && path.FullPath.StartsWith(x.Collapse().FullPath)); } FilePath filePath = path as FilePath; if (filePath != null) { IFile file = GetInputFile(filePath); return file.Exists ? GetContainingInputPath(file.Path) : null; } DirectoryPath directoryPath = path as DirectoryPath; if (directoryPath != null) { return InputPaths .Reverse() .Select(x => new KeyValuePair<DirectoryPath, IDirectory>(x, GetRootDirectory(x.Combine(directoryPath)))) .Where(x => x.Value.Exists) .Select(x => RootPath.Combine(x.Key)) .FirstOrDefault(); } return null; }
internal static string Collapse(NormalizedPath path) { if (path == null) { throw new ArgumentNullException(nameof(path)); } Stack<string> stack = new Stack<string>(); string[] segments = path.FullPath.Split('/', '\\'); foreach (string segment in segments) { if (segment == "..") { if (stack.Count > 1) { stack.Pop(); } continue; } stack.Push(segment); } return string.Join("/", stack.Reverse()); }