using System;
-using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Net;
+using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
[Export(typeof(ICloudClient))]
public class CloudFilesClient:ICloudClient
{
+ private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
+
//CloudFilesClient uses *_baseClient* internally to communicate with the server
//RestClient provides a REST-friendly interface over the standard WebClient.
private RestClient _baseClient;
public bool UsePithos { get; set; }
- private static readonly ILog Log = LogManager.GetLogger("CloudFilesClient");
public CloudFilesClient(string userName, string apiKey)
{
}
Log.InfoFormat("[AUTHENTICATE] End for {0}", UserName);
-
+ Debug.Assert(_baseClient!=null);
return new AccountInfo {StorageUri = StorageUrl, Token = Token, UserName = UserName,Groups=groups};
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");
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;
{
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")),
client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
var jsonHash = hash.ToJson();
var uploadTask=client.UploadStringTask(uri, "PUT", jsonHash);
-
+ if (Log.IsDebugEnabled)
+ Log.DebugFormat("Hashes:\r\n{0}", jsonHash);
return uploadTask.ContinueWith(t =>
{
{
//In case of 409 the missing parts will be in the response content
using (var stream = response.GetResponseStream())
- using(var reader=new StreamReader(stream))
+ using(var reader=stream.GetLoggedReader(Log))
{
- Debug.Assert(stream.Position == 0);
//We used to have to cleanup the content before returning it because it contains
//error content after the list of hashes
//
//line by line
var serializer = new JsonSerializer();
- var hashes=(List<string>)serializer.Deserialize(reader, typeof (List<string>));
-
+ serializer.Error += (sender, args) => Log.ErrorFormat("Deserialization error at [{0}] [{1}]", args.ErrorContext.Error, args.ErrorContext.Member);
+ var hashes = (List<string>)serializer.Deserialize(reader, typeof(List<string>));
return hashes;
}
}
//Any other status code is unexpected and the exception should be rethrown
+ Log.LogError(response);
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);
}
+
public Task<byte[]> GetBlock(string account, string container, Uri relativeUrl, long start, long? end)
{
if (String.IsNullOrWhiteSpace(Token))
if (!File.Exists(fileName) && !Directory.Exists(fileName))
throw new FileNotFoundException("The file or directory does not exist",fileName);
*/
- Contract.EndContractBlock();
try
{
client.Headers.Add("X-Move-From", sourceUrl);
client.AllowedStatusCodes.Add(HttpStatusCode.NotFound);
+ Log.InfoFormat("[TRASH] [{0}] to [{1}]",sourceUrl,targetUrl);
client.PutWithRetry(targetUrl, 3);
var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created,HttpStatusCode.NotFound};
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