/// <summary> /// Returns the next unused filename similar to the current one,by incrementing (i) /// </summary> /// <returns></returns> public yrl GetNextAvailableDerivitive() { yrl newfilename = this.Copy(); if (newfilename.FileExists) { //This section here just looks for the next unused filname in the form // oldfilename (1).oldextension // This way, if the same file is uploaded 5 times it will appear as // file.txt // file (1).txt // file (2).txt // file (3).txt // file (4).txt string minusext = newfilename.Parent.Local + System.IO.Path.DirectorySeparatorChar + newfilename.NameWithoutExtension; for (int i = 1; i < 100; i++) { if (!System.IO.File.Exists(minusext + " (" + i.ToString() + ")" + newfilename.Extension)) { newfilename = new yrl(minusext + " (" + i.ToString() + ")" + newfilename.Extension); break; } } } return(newfilename); }
/// <summary> /// Creates a deep copy of the yrl /// </summary> /// <returns></returns> public yrl Copy() { yrl temp = new yrl(); temp.BaseFile = string.Copy(this.BaseFile); foreach (string s in QueryString.Keys) { string value = QueryString[s]; temp.QueryString.Add(string.Copy(s), string.Copy(value)); } return(temp); }
/// <summary> /// Returns true if the yrl is null or empty (site root) /// </summary> /// <param name="path"></param> /// <returns></returns> public static bool IsNullOrEmpty(yrl path) { if (path == null) { return(true); } if (path.IsEmpty) { return(true); } return(false); }
/// <summary> /// Creates an instance from a yrl-syntax string (virtual, but without ~/). Use FromString if you're not sure what type of syntax is used. /// </summary> /// <param name="path"></param> /// <returns></returns> public static yrl FromYrlString(string path) { if (path.Length == 0) { return(yrl.Root); } yrl temp = new yrl(); int firstdelimiter = path.IndexOfAny(new char[] { '?', '&' }); if (firstdelimiter < 0) { firstdelimiter = path.Length; } if (path.IndexOfAny(new char[] { '/', '\\', '~', ':' }) == 0) { return(null); } //throw new Exception("The specified path \"" + path + "\" starts with an invalid character! yrl paths cannot begin with any sort of slash, tilde, or colon!"); temp.BaseFile = path.Substring(0, firstdelimiter); string querystring = ""; if (firstdelimiter < path.Length) { querystring = path.Substring(firstdelimiter, path.Length - firstdelimiter); } if (querystring.Length > 0) { string[] pairs = querystring.Split(new char[] { '?', '&' }, StringSplitOptions.RemoveEmptyEntries); foreach (string s in pairs) { string[] namevalue = s.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); if (namevalue.Length == 2) { temp.QueryString[HttpUtility.UrlDecode(namevalue[0])] = HttpUtility.UrlDecode(namevalue[1]); } else { //No value, so we set a blank value //Setting a null value would be confusing, as that is how we determine //whether a certain paramater exists temp.QueryString[HttpUtility.UrlDecode(namevalue[0])] = ""; //throw new Exception("The specified path \"" + path + "\" contains a query paramater pair \"" + s + "\" that does not parse!"); } } } return(temp); }
public yrl(string path) { if (path == null) { throw new ArgumentNullException("path", "Cannot create a yrl from a null string"); } yrl temp = FromString(path); if (temp == null) { throw new ArgumentException("The specified path (" + path + ") could not be parsed! It may be outside the bounds of the application, or it may contain an invalid character, such as a tilde in the middle of the path.", "path"); } this.BaseFile = temp.BaseFile; this.QueryString = temp.QueryString; }
/// <summary> /// Joins two yrls. Querystrings on either are discarded. /// </summary> /// <param name="folder"></param> /// <param name="filenameOrSubdir"></param> /// <returns></returns> public static yrl Combine(yrl folder, yrl filenameOrSubdir) { return(new yrl(folder.Local + System.IO.Path.DirectorySeparatorChar + filenameOrSubdir.BaseFile.Replace('/', '\\'))); }