Example #1
0
        /// <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);
        }
Example #2
0
        /// <summary>
        /// Returns a yrl object from a path. Returns null if the path cannot be parsed.
        /// (Out of application bounds, or an invalid character). The tilde is an invalid character unless used as the app-relative specifier.
        /// </summary>
        /// <param name="path">A path
        /// Each query paramater will be UrlDecoded.
        /// </param>
        /// <returns></returns>
        public static yrl FromString(string path)
        {
            //crashes on ~$lioFlatFile.rtf
            PathType pathtype = GetPathType(path);

            if (pathtype == PathType.Root)
            {
                return(yrl.Root);
            }

            //OSX Mono uses physical paths in the unix style - fools the comparison. So, to handle physical paths right, we have to try that first.

            yrl test = FromPhysicalString(path);

            if (test != null)
            {
                return(test);
            }

            if (pathtype == PathType.File || pathtype == PathType.PathForwardslashPath)
            {
                return(FromYrlString(path));
            }

            if (pathtype == PathType.ForwardslashPath)
            {
                return(FromYrlString(TrimStartAppFolder(path).TrimStart(new char[] { '/' })));
            }
            if (pathtype == PathType.ServerForwardslashPath)
            {
                int tilde = path.IndexOf('~');
                if (tilde > 0)
                {
                    return(FromYrlString(TrimStartAppFolder(TrimStartServer(path).TrimStart(new char[] { '~', '/' })).TrimStart(new char[] { '/' })));
                }
                else
                {
                    return(FromYrlString(TrimStartAppFolder(TrimStartServer(path)).TrimStart(new char[] { '/' })));
                }
            }
            if (pathtype == PathType.TildeForwardslashPath)
            {
                //return FromYrlString(TrimStartAppFolder(path.TrimStart(new char[] { '~', '/' })).TrimStart(new char[] { '/' }));
                return(FromYrlString(path.TrimStart(new char[] { '~', '/' })));
            }
            if (pathtype == PathType.DriveletterColonBackslashPath)
            {
                return(FromPhysicalString(path));
            }
            if (pathtype == PathType.BackslashPath || pathtype == PathType.PathBackslashPath)
            {
                return(FromRelativePhysicalString(path));
            }
            return(null);
        }
Example #3
0
        /// <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);
        }
Example #4
0
 /// <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);
 }
Example #5
0
        /// <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);
        }
Example #6
0
        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;
        }
Example #7
0
 /// <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('/', '\\')));
 }
Example #8
0
        /// <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;
        }
Example #9
0
 /// <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('/','\\'));
 }
Example #10
0
        /// <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;
        }
Example #11
0
 /// <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;
 }
Example #12
0
 /// <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;
 }