/// <summary> /// Changes a URL’s subpath or subdomain relative to the specified number of URL resolvers.</summary> /// <param name="url"> /// The URL to modify.</param> /// <param name="levels"> /// The number of URL resolvers to rewind.</param> /// <param name="pathOrSubdomain"> /// The new subpath or subdomain, without any slashes or dots.</param> /// <param name="useSubdomain"> /// If <c>true</c>, the subdomain is changed; if <c>false</c> (default), the path is changed.</param> /// <param name="retainQueryParams"> /// If <c>true</c>, the query parameters are retained; if <c>false</c> (default), they are removed.</param> /// <returns> /// The new URL.</returns> public static IHttpUrl WithParents(this IHttpUrl url, int levels, string pathOrSubdomain, bool useSubdomain = false, bool retainQueryParams = false) { pathOrSubdomain = pathOrSubdomain.Length == 0 ? "" : useSubdomain ? pathOrSubdomain + "." : "/" + pathOrSubdomain; for (int i = 0; i < levels; i++) { url = useSubdomain ? url.WithDomainParent() : url.WithPathParent(); } return (useSubdomain ? retainQueryParams ? url.WithDomain(pathOrSubdomain) : url.WithDomain(pathOrSubdomain).WithoutQuery() : retainQueryParams ? url.WithPath(pathOrSubdomain) : url.WithPathOnly(pathOrSubdomain)); }
private static IEnumerable <string> generateDirectoryXml(string localPath, IHttpUrl url, string urlPath) { List <DirectoryInfo> dirs = new List <DirectoryInfo>(); List <FileInfo> files = new List <FileInfo>(); DirectoryInfo dirInfo = new DirectoryInfo(localPath); foreach (var d in dirInfo.GetDirectories()) { dirs.Add(d); } foreach (var f in dirInfo.GetFiles()) { files.Add(f); } dirs.Sort((a, b) => a.Name.CompareTo(b.Name)); files.Sort((a, b) => a.Name.CompareTo(b.Name)); yield return("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); yield return("<?xml-stylesheet href=\"{0}\" type=\"text/xsl\" ?>\n".Fmt(url.WithPathOnly("/$/directory-listing/xsl").ToHref().HtmlEscape())); yield return("<directory url=\"{0}\" unescapedurl=\"{1}\" img=\"{2}\" numdirs=\"{3}\" numfiles=\"{4}\">\n" .Fmt(url.ToHref().HtmlEscape(), url.ToHref().UrlUnescape().HtmlEscape(), url.WithPathOnly("/$/directory-listing/icons/folderbig").ToHref().HtmlEscape(), dirs.Count, files.Count)); foreach (var d in dirs) { yield return(" <dir link=\"{0}/\" img=\"{2}\">{1}</dir>\n".Fmt(d.Name.UrlEscape(), d.Name.HtmlEscape(), url.WithPathOnly("/$/directory-listing/icons/folder").ToHref().HtmlEscape())); } foreach (var f in files) { string extension = f.Name.Contains('.') ? f.Name.Substring(f.Name.LastIndexOf('.') + 1) : ""; yield return(" <file link=\"{0}\" size=\"{1}\" nicesize=\"{2}\" img=\"{3}\">{4}</file>\n" .Fmt(f.Name.UrlEscape(), f.Length, Ut.SizeToString(f.Length), url.WithPathOnly("/$/directory-listing/icons/" + GetDirectoryListingIconStr(extension)).ToHref().HtmlEscape(), f.Name.HtmlEscape())); } yield return("</directory>\n"); }
private static IEnumerable<string> generateDirectoryXml(string localPath, IHttpUrl url, string urlPath) { List<DirectoryInfo> dirs = new List<DirectoryInfo>(); List<FileInfo> files = new List<FileInfo>(); DirectoryInfo dirInfo = new DirectoryInfo(localPath); foreach (var d in dirInfo.GetDirectories()) dirs.Add(d); foreach (var f in dirInfo.GetFiles()) files.Add(f); dirs.Sort((a, b) => a.Name.CompareTo(b.Name)); files.Sort((a, b) => a.Name.CompareTo(b.Name)); yield return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; yield return "<?xml-stylesheet href=\"{0}\" type=\"text/xsl\" ?>\n".Fmt(url.WithPathOnly("/$/directory-listing/xsl").ToHref().HtmlEscape()); yield return "<directory url=\"{0}\" unescapedurl=\"{1}\" img=\"{2}\" numdirs=\"{3}\" numfiles=\"{4}\">\n" .Fmt(url.ToHref().HtmlEscape(), url.ToHref().UrlUnescape().HtmlEscape(), url.WithPathOnly("/$/directory-listing/icons/folderbig").ToHref().HtmlEscape(), dirs.Count, files.Count); foreach (var d in dirs) yield return " <dir link=\"{0}/\" img=\"{2}\">{1}</dir>\n".Fmt(d.Name.UrlEscape(), d.Name.HtmlEscape(), url.WithPathOnly("/$/directory-listing/icons/folder").ToHref().HtmlEscape()); foreach (var f in files) { string extension = f.Name.Contains('.') ? f.Name.Substring(f.Name.LastIndexOf('.') + 1) : ""; yield return " <file link=\"{0}\" size=\"{1}\" nicesize=\"{2}\" img=\"{3}\">{4}</file>\n" .Fmt(f.Name.UrlEscape(), f.Length, Ut.SizeToString(f.Length), url.WithPathOnly("/$/directory-listing/icons/" + GetDirectoryListingIconStr(extension)).ToHref().HtmlEscape(), f.Name.HtmlEscape()); } yield return "</directory>\n"; }