Added Clear Conflict action to resolver
[pithos-ms-client] / trunk / Pithos.Interfaces / ObjectInfo.cs
index d9f49a7..7301002 100644 (file)
@@ -53,12 +53,13 @@ using Newtonsoft.Json;
 namespace Pithos.Interfaces
 {
     [DebuggerDisplay("Name {Name}")]
-    public class ObjectInfo:DynamicObject 
+    public class ObjectInfo//:DynamicObject 
     {
         private readonly List<string> _knownContainers= new List<string>{"trash"};
         public string Name { get; set; }
-        
-        
+
+        public string ETag { get; set; }
+
         public string Hash { get; set; }
 
         public string X_Object_Hash { get { return Hash; } set { Hash = value; } }
@@ -139,7 +140,9 @@ namespace Pithos.Interfaces
             get
             {
                 var relativeUrl=String.Format("{0}/{1}/{2}",Account, Container,Name);
-                return new Uri(StorageUri,relativeUrl);
+                return StorageUri==null 
+                    ? new Uri(relativeUrl,UriKind.Relative) 
+                    : new Uri(StorageUri, relativeUrl);
             }
         }
 
@@ -164,6 +167,8 @@ namespace Pithos.Interfaces
         [JsonProperty("X_Object_Public")]
         public string PublicUrl { get; set; }
 
+        public string PreviousHash { get; set; }
+
         public ObjectInfo()
         {}
 
@@ -232,10 +237,22 @@ namespace Pithos.Interfaces
             Hash = String.Empty,
             Bytes = 0,
             Content_Type = String.Empty,
-            Last_Modified = DateTime.MinValue
+            Last_Modified = DateTime.MinValue,
+            Exists=false
         };
 
-        
+        private bool _exists=true;
+
+        public bool Exists
+        {
+            get {
+                return _exists;
+            }
+            set {
+                _exists = value;
+            }
+        }
+
 
         public string RelativeUrlToFilePath(string currentAccount)
         {
@@ -266,6 +283,7 @@ namespace Pithos.Interfaces
             return finalPath;
         }
 
+/*
         public override bool TrySetMember(SetMemberBinder binder, object value)
         {
             if (binder.Name.StartsWith("x_object_meta"))
@@ -274,6 +292,7 @@ namespace Pithos.Interfaces
             }
             return false;
         }
+*/
 
         public string GetPermissionString()
         {
@@ -281,13 +300,15 @@ namespace Pithos.Interfaces
                 throw new InvalidOperationException();
             Contract.EndContractBlock();
 
+            if (Permissions.Count == 0)
+                return "~";
             var permissionBuilder = new StringBuilder();
-            var groupings = Permissions.GroupBy(pair => pair.Value, pair => pair.Key);
+            var groupings = Permissions.GroupBy(pair => pair.Value.Trim(), pair => pair.Key.Trim());
             foreach (var grouping in groupings)
             {
                 permissionBuilder.AppendFormat("{0}={1};", grouping.Key, String.Join(",", grouping));
             }
-            var permissions = permissionBuilder.ToString().Trim(';');
+            var permissions = Uri.EscapeDataString(permissionBuilder.ToString().Trim(';'));
             return permissions;
         }
 
@@ -295,20 +316,76 @@ namespace Pithos.Interfaces
         {
             if (String.IsNullOrWhiteSpace(permissions))
                 return;
+            
+            Permissions = PermissionConverter.ParsePermissions(permissions);
+        }
 
-            var permDict=new Dictionary<string, string>();
-            var perms=permissions.Split(';');
-            foreach (var perm in perms)
+        //The previous values that correspond to a NoModification object
+        //have the same account, container and possibly the same folder
+        public bool CorrespondsTo(ObjectInfo other)
+        {
+            return other.Account == this.Account
+                   && other.Container == this.Container
+                   && (this.Name == null || other.Name.StartsWith(this.Name));
+        }
+
+        public bool IsWritable(string account)
+        {
+            //If the Allowed To header has no value, try to determine the Share permissions
+            if (AllowedTo == null)
             {
-                var permPairs=perm.Split('=');
-                var right = permPairs[0];
-                var users= permPairs[1].Split(new[]{','},StringSplitOptions.RemoveEmptyEntries);
-                foreach (var user in users)
-                {
-                    permDict[user] = right;
-                }
+                //If this file has no permissions defined, we can probably write it
+                //This case should occur only when the info comes from a listing of the user's own files
+                if (Permissions == null || Permissions.Count == 0)
+                    return true;
+                string perms;
+
+                //Do we have an explicit write share to this account?
+                return Permissions.TryGetValue(account, out perms) 
+                    && perms.Equals("write",StringComparison.InvariantCultureIgnoreCase);
+                
+            }
+            //Otherwise return the permissions specified by AllowedTo
+            return AllowedTo.Equals("write",StringComparison.InvariantCultureIgnoreCase) ;
+        }
+
+        public ObjectInfo Previous { get; private set; }
+
+        public bool IsDirectory
+        {
+            get
+            {
+                if (Content_Type == null)
+                    return false;
+                if (Content_Type.StartsWith(@"application/directory",StringComparison.InvariantCultureIgnoreCase))
+                    return true;
+                if (Content_Type.StartsWith(@"application/folder",StringComparison.InvariantCultureIgnoreCase))
+                    return true;
+                return false;
+            }
+        }
+
+        public Uri AccountKey
+        {
+            get { return new Uri(StorageUri,"../" + Account); }
+        }
+
+        public ObjectInfo SetPrevious(ObjectInfo previous)
+        {            
+            Previous = previous;
+            PreviousHash = previous.Hash;
+            return this;
+        }
+
+        public bool? IsShared
+        {
+            get
+            {
+                if (Uri == null || StorageUri == null)
+                    return null;
+                var isShared = !Uri.ToString().StartsWith(StorageUri.ToString());
+                return isShared;
             }
-            Permissions = permDict;
         }
     }
 }
\ No newline at end of file