Convert all url usages to use the Uri class instead of raw strings.
[pithos-ms-client] / trunk / Pithos.Interfaces / ObjectInfo.cs
index 766c5c2..03aecc1 100644 (file)
@@ -58,7 +58,18 @@ namespace Pithos.Interfaces
         public const string CONTENT_TYPE_DIRECTORY = @"application/directory";\r
         public const string CONTENT_TYPE_FOLDER = @"application/folder";\r
         private readonly List<string> _knownContainers= new List<string>{"trash"};\r
-        public string Name { get; set; }\r
+\r
+        [JsonConverter(typeof(RelativeUriConverter))]\r
+        public Uri Name\r
+        {\r
+            get { return _name; }\r
+            set\r
+            {\r
+                if (value != null && value.IsAbsoluteUri)\r
+                    throw new ArgumentException("Must be relative Uri", "Name");\r
+                _name = value;\r
+            }\r
+        }\r
 \r
         [JsonProperty("hash")]\r
         public string ETag { get; set; }\r
@@ -140,7 +151,17 @@ namespace Pithos.Interfaces
 \r
         public string Account { get; set; }\r
 \r
-        public string Container { get; set; }\r
+        [JsonConverter(typeof(RelativeUriConverter))]\r
+        public Uri Container\r
+        {\r
+            get { return _container; }\r
+            set\r
+            {\r
+                if (value != null && value.IsAbsoluteUri)\r
+                    throw new ArgumentException("Must be relative Uri", "Container");\r
+                _container = value;\r
+            }\r
+        }\r
 \r
         public Uri Uri\r
         {\r
@@ -149,7 +170,7 @@ namespace Pithos.Interfaces
                 var relativeUrl=String.Format("{0}/{1}/{2}",Account, Container,Name);\r
                 return StorageUri==null \r
                     ? new Uri(relativeUrl,UriKind.Relative) \r
-                    : new Uri(StorageUri, relativeUrl);\r
+                    : StorageUri.Combine(relativeUrl);\r
             }\r
         }\r
 \r
@@ -191,14 +212,14 @@ namespace Pithos.Interfaces
 \r
             var relativeUrl = fileInfo.WithProperCapitalization().AsRelativeUrlTo(accountPath);\r
             //The first part of the URL is the container\r
-            var slashIndex = relativeUrl.IndexOf('/');\r
-            var container = relativeUrl.Substring(0, slashIndex);\r
+            var parts = relativeUrl.ToString().Split(new[]{'/'}, 2);\r
+            var container = parts[0];\r
             //The second is the file's url relative to the container\r
-            var fileUrl = relativeUrl.Substring(slashIndex + 1);\r
+            var fileUrl = parts[1];\r
 \r
             Account = accountName;\r
-            Container = container;\r
-            Name = fileUrl; \r
+            Container = new Uri(container,UriKind.Relative);\r
+            Name = new Uri(fileUrl,UriKind.Relative); \r
         }\r
 \r
 \r
@@ -240,7 +261,8 @@ namespace Pithos.Interfaces
 \r
         public static ObjectInfo Empty = new ObjectInfo\r
         {\r
-            Name = String.Empty,\r
+            Name = _emptyUri,\r
+            Container = _emptyUri,\r
             ETag= String.Empty,\r
             X_Object_Hash= String.Empty,\r
             Bytes = 0,\r
@@ -250,6 +272,9 @@ namespace Pithos.Interfaces
         };\r
 \r
         private bool _exists=true;\r
+        private Uri _container;\r
+        private Uri _name;\r
+        private static Uri _emptyUri = new Uri(String.Empty, UriKind.Relative);\r
 \r
         public bool Exists\r
         {\r
@@ -273,12 +298,14 @@ namespace Pithos.Interfaces
             if (this == Empty)\r
                 return String.Empty;\r
 \r
-            var unescaped = Uri.UnescapeDataString(Name);\r
+            var unescaped = Uri.UnescapeDataString(Name.ToString());\r
             var path = unescaped.Replace("/", "\\");\r
             var pathParts=new Stack<string>();\r
             pathParts.Push(path);\r
-            if (!String.IsNullOrWhiteSpace(Container) && !_knownContainers.Contains(Container))\r
-                pathParts.Push(Container);\r
+\r
+            var container = Container.NullSafe(c=>c.ToString());\r
+            if (!String.IsNullOrWhiteSpace(container) && !_knownContainers.Contains(container))\r
+                pathParts.Push(Uri.UnescapeDataString(container));\r
             if (!currentAccount.Equals(Account, StringComparison.InvariantCultureIgnoreCase))\r
             {\r
                 if (Account != null)\r
@@ -334,7 +361,7 @@ namespace Pithos.Interfaces
         {\r
             return other.Account == this.Account\r
                    && other.Container == this.Container\r
-                   && (this.Name == null || other.Name.StartsWith(this.Name));\r
+                   && (this.Name == null || other.Name.ToString().StartsWith(this.Name.ToString()));\r
         }\r
 \r
         public bool IsWritable(string account)\r
@@ -375,7 +402,7 @@ namespace Pithos.Interfaces
 \r
         public Uri AccountKey\r
         {\r
-            get { return new Uri(StorageUri,"../" + Account); }\r
+            get { return StorageUri.Combine("../" + Account); }\r
         }\r
 \r
         public ObjectInfo SetPrevious(ObjectInfo previous)\r