Fix for missing directories
[pithos-ms-client] / trunk / Pithos.Network / CloudFilesClient.cs
index e11b4d6..9d4bc06 100644 (file)
@@ -242,7 +242,7 @@ namespace Pithos.Network
             }
 
             Log.InfoFormat("[AUTHENTICATE] End for {0}", UserName);
-            
+            Debug.Assert(_baseClient!=null);
 
             return new AccountInfo {StorageUri = StorageUrl, Token = Token, UserName = UserName,Groups=groups};            
 
@@ -510,10 +510,13 @@ namespace Pithos.Network
                     client.Headers.Add("X-Object-Public", isPublic);
 
 
-                    var uriBuilder = client.GetAddressBuilder(objectInfo.Container, objectInfo.Name);
-                    var uri = uriBuilder.Uri;
-
-                    client.UploadValues(uri,new NameValueCollection());
+                    /*var uriBuilder = client.GetAddressBuilder(objectInfo.Container, objectInfo.Name);
+                    uriBuilder.Query = "update=";
+                    var uri = uriBuilder.Uri.MakeRelativeUri(this.RootAddressUri);*/
+                    var address = String.Format("{0}/{1}?update=",objectInfo.Container, objectInfo.Name);
+                    client.PostWithRetry(address,"application/xml");
+                    
+                    //client.UploadValues(uri,new NameValueCollection());
 
 
                     client.AssertStatusOK("UpdateMetadata failed");
@@ -793,7 +796,8 @@ namespace Pithos.Network
                                                    ContentEncoding=client.GetHeaderValue("Content-Encoding",true),
                                                    ContendDisposition = client.GetHeaderValue("Content-Disposition",true),
                                                    Manifest=client.GetHeaderValue("X-Object-Manifest",true),
-                                                   PublicUrl=client.GetHeaderValue("X-Object-Public",true),                                                   
+                                                   PublicUrl=client.GetHeaderValue("X-Object-Public",true),  
+                                                   StorageUri=this.StorageUrl,
                                                };
                                 info.SetPermissions(permissions);
                                 return info;
@@ -872,6 +876,7 @@ namespace Pithos.Network
                                                 {
                                                     Account=account,
                                                     Name = container,
+                                                    StorageUrl=this.StorageUrl.ToString(),
                                                     Count =
                                                         long.Parse(client.GetHeaderValue("X-Container-Object-Count")),
                                                     Bytes = long.Parse(client.GetHeaderValue("X-Container-Bytes-Used")),
@@ -1055,13 +1060,12 @@ namespace Pithos.Network
                 {
                     var ex = t.Exception.InnerException;
                     var we = ex as WebException;
-                    
                     var response = we.Response as HttpWebResponse;
                     if (response!=null && response.StatusCode==HttpStatusCode.Conflict)
                     {
                         //In case of 409 the missing parts will be in the response content                        
                         using (var stream = response.GetResponseStream())
-                        using(var reader=stream.GetLoggedReader(Log,response.ContentLength))
+                        using(var reader=stream.GetLoggedReader(Log))
                         {
                             //We used to have to cleanup the content before returning it because it contains
                             //error content after the list of hashes
@@ -1080,6 +1084,7 @@ namespace Pithos.Network
                     throw ex;
                     
                 }
+
                 //Any other status code is unexpected but there was no exception. We can probably continue processing
                 Log.WarnFormat("Unexcpected status code when putting map: {0} - {1}",client.StatusCode,client.StatusDescription);                    
                 
@@ -1249,7 +1254,6 @@ namespace Pithos.Network
             if (!File.Exists(fileName) && !Directory.Exists(fileName))
                 throw new FileNotFoundException("The file or directory does not exist",fileName);
 */
-            Contract.EndContractBlock();
             
             try
             {
@@ -1389,6 +1393,39 @@ namespace Pithos.Network
             var directories=this.ListObjects(container.Account, container.Name, "/");
         }
 */
+
+        public bool CanUpload(string account, ObjectInfo cloudFile)
+        {
+            Contract.Requires(!String.IsNullOrWhiteSpace(account));
+            Contract.Requires(cloudFile!=null);
+
+            using (var client = new RestClient(_baseClient))
+            {
+                if (!String.IsNullOrWhiteSpace(account))
+                    client.BaseAddress = GetAccountUrl(account);
+
+
+                var parts = cloudFile.Name.Split('/');
+                var folder = String.Join("/", parts,0,parts.Length-1);
+
+                var fileUrl=String.Format("{0}/{1}/{2}.pithos.ignore",cloudFile.Container,folder,Guid.NewGuid());
+
+                client.Parameters.Clear();
+                try
+                {
+                    client.PutWithRetry(fileUrl, 3, @"application/octet-stream");
+
+                    var expectedCodes = new[] { HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
+                    var result=(expectedCodes.Contains(client.StatusCode));
+                    DeleteObject(account, cloudFile.Container, fileUrl);
+                    return result;
+                }
+                catch
+                {
+                    return false;
+                }
+            }
+        }
     }
 
     public class ShareAccountInfo