2 using System.Diagnostics.Contracts;
4 using System.Threading;
5 using Pithos.Interfaces;
8 namespace Pithos.Core.Agents
10 public enum CloudActionType
14 DownloadUnconditional,
20 public class CloudAction
22 public AccountInfo AccountInfo { get; set; }
23 public CloudActionType Action { get; set; }
24 public FileSystemInfo LocalFile { get; set; }
25 public ObjectInfo CloudFile { get; set; }
26 public FileState FileState { get; set; }
27 public string Container { get; set; }
29 public readonly DateTime Created = DateTime.Now;
32 public Lazy<string> LocalHash { get; protected set; }
33 private Lazy<string> _topHash;
34 public Lazy<string> TopHash
36 get { return _topHash; }
37 set { _topHash = value; }
41 [ContractInvariantMethod]
42 private void Invariants()
44 Contract.Invariant(AccountInfo!=null);
49 get { return CloudFile!=null && AccountInfo.UserName != CloudFile.Account; }
52 protected CloudAction(AccountInfo accountInfo,CloudActionType action)
54 if (accountInfo==null)
55 throw new ArgumentNullException("accountInfo");
56 Contract.EndContractBlock();
59 AccountInfo = accountInfo;
62 public CloudAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo localFile, ObjectInfo cloudFile, FileState state, int blockSize, string algorithm)
63 : this(accountInfo,action)
65 LocalFile = localFile.WithProperCapitalization();
66 CloudFile = cloudFile;
68 if (LocalFile != null)
71 LocalHash = new Lazy<string>(() => LocalFile.CalculateHash(blockSize,algorithm),
72 LazyThreadSafetyMode.ExecutionAndPublication);
76 //Calculate the download path for the cloud file
77 public string GetDownloadPath()
79 if (CloudFile == null)
81 var filePath = CloudFile.RelativeUrlToFilePath(AccountInfo.UserName);
82 return Path.Combine(AccountInfo.AccountPath, filePath);
85 public override string ToString()
87 return String.Format("{0}:{1}->{2}", this.Action, this.LocalFile.FullName, this.CloudFile.Name);
90 protected static ObjectInfo CreateObjectInfoFor(AccountInfo accountInfo, FileSystemInfo fileInfo)
92 Contract.Requires(accountInfo!=null);
93 Contract.Requires(fileInfo!=null);
94 Contract.Ensures(Contract.Result<ObjectInfo>()!=null);
96 var capitalizedFileInfo = fileInfo.WithProperCapitalization();
97 var fullLocalName = capitalizedFileInfo.FullName;
98 var othersPath = Path.Combine(accountInfo.AccountPath, FolderConstants.OthersFolder);
100 var isShared = fullLocalName.StartsWith(othersPath, StringComparison.InvariantCultureIgnoreCase);
103 var pathRelativeToOther = fullLocalName.Substring(othersPath.Length + 1);
104 var otherParts = pathRelativeToOther.Split('\\');
105 var otherName = otherParts[0];
106 var otherContainer = otherParts[1];
107 return new ObjectInfo
110 Container = otherContainer,
111 Name = String.Join("/", otherParts.Splice(2))
114 return new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName, fileInfo);
118 public class CloudDownloadAction:CloudAction
120 public CloudDownloadAction(AccountInfo accountInfo, ObjectInfo cloudFile)
121 :base(accountInfo,CloudActionType.DownloadUnconditional)
123 if (String.IsNullOrWhiteSpace(cloudFile.Container))
124 throw new ArgumentException("CloudFile.Container","cloudFile");
125 Contract.EndContractBlock();
127 CloudFile = cloudFile;
130 [ContractInvariantMethod]
131 private void Invariants()
133 Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
136 public override string ToString()
138 return String.Format("{0}: _ <- {1}", this.Action, this.CloudFile.Name);
142 public class CloudDeleteAction:CloudAction
144 public CloudDeleteAction(AccountInfo accountInfo,FileSystemInfo fileInfo, FileState fileState)
145 : this(accountInfo,fileInfo,CreateObjectInfoFor(accountInfo, fileInfo),fileState)
149 public CloudDeleteAction(AccountInfo accountInfo, FileSystemInfo fileInfo,ObjectInfo cloudFile, FileState fileState)
150 : base(accountInfo,CloudActionType.DeleteCloud)
152 CloudFile = cloudFile;
153 LocalFile = fileInfo;
154 FileState = fileState;
157 public CloudDeleteAction(CloudAction action)
158 : this(action.AccountInfo,action.LocalFile,action.CloudFile,action.FileState)
161 [ContractInvariantMethod]
162 private void Invariants()
164 Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
167 public override string ToString()
169 return String.Format("{0}: _ ->{1}", this.Action, this.CloudFile.Name);
174 public class CloudUploadAction:CloudAction
176 public CloudUploadAction(AccountInfo accountInfo, FileSystemInfo fileInfo, FileState state, int blockSize, string algorithm)
177 : base(accountInfo, CloudActionType.UploadUnconditional,fileInfo,CreateObjectInfoFor(accountInfo,fileInfo),state,blockSize,algorithm)
181 [ContractInvariantMethod]
182 private void Invariants()
184 Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
189 public class CloudMoveAction:CloudAction
191 public ObjectInfo OldCloudFile { get; set; }
193 public FileSystemInfo OldLocalFile { get; set; }
195 public CloudMoveAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo oldFile, FileSystemInfo newFile)
196 :base(accountInfo,action)
199 CloudFile = CreateObjectInfoFor(accountInfo, newFile);
201 OldLocalFile = oldFile;
202 OldCloudFile = CreateObjectInfoFor(accountInfo, oldFile);
204 //This is a rename operation, a hash will not be used
205 LocalHash = new Lazy<string>(() => String.Empty, LazyThreadSafetyMode.ExecutionAndPublication);
208 public override string ToString()
210 return String.Format("{0}:{1}->{2}", this.Action, OldCloudFile.Name, CloudFile.Name);