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; }
30 public Lazy<string> LocalHash { get; protected set; }
31 private Lazy<string> _topHash;
32 public Lazy<string> TopHash
34 get { return _topHash; }
35 set { _topHash = value; }
39 [ContractInvariantMethod]
40 private void Invariants()
42 Contract.Invariant(AccountInfo!=null);
45 protected CloudAction(AccountInfo accountInfo,CloudActionType action)
47 if (accountInfo==null)
48 throw new ArgumentNullException("accountInfo");
49 Contract.EndContractBlock();
52 AccountInfo = accountInfo;
55 public CloudAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo localFile, ObjectInfo cloudFile, FileState state, int blockSize, string algorithm)
56 : this(accountInfo,action)
58 LocalFile = localFile.WithProperCapitalization();
59 CloudFile = cloudFile;
61 if (LocalFile != null)
64 LocalHash = new Lazy<string>(() => LocalFile.CalculateHash(blockSize,algorithm),
65 LazyThreadSafetyMode.ExecutionAndPublication);
69 //Calculate the download path for the cloud file
70 public string GetDownloadPath()
72 if (CloudFile == null)
74 var filePath = CloudFile.RelativeUrlToFilePath(AccountInfo.UserName);
75 return Path.Combine(AccountInfo.AccountPath, filePath);
78 public override string ToString()
80 return String.Format("{0}:{1}->{2}", this.Action, this.LocalFile.FullName, this.CloudFile.Name);
84 public class CloudDownloadAction:CloudAction
86 public CloudDownloadAction(AccountInfo accountInfo, ObjectInfo cloudFile)
87 :base(accountInfo,CloudActionType.DownloadUnconditional)
89 if (String.IsNullOrWhiteSpace(cloudFile.Container))
90 throw new ArgumentException("CloudFile.Container","cloudFile");
91 Contract.EndContractBlock();
93 CloudFile = cloudFile;
96 [ContractInvariantMethod]
97 private void Invariants()
99 Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
102 public override string ToString()
104 return String.Format("{0}: _ <- {1}", this.Action, this.CloudFile.Name);
108 public class CloudDeleteAction:CloudAction
110 public CloudDeleteAction(AccountInfo accountInfo,FileSystemInfo fileInfo, FileState fileState)
111 : this(accountInfo,new ObjectInfo(accountInfo.AccountPath,accountInfo.UserName,fileInfo),fileState)
115 public CloudDeleteAction(AccountInfo accountInfo, ObjectInfo cloudFile, FileState fileState)
116 : base(accountInfo,CloudActionType.DeleteCloud)
118 CloudFile = cloudFile;
119 FileState = fileState;
122 public CloudDeleteAction(CloudAction action)
123 : this(action.AccountInfo,action.CloudFile,action.FileState)
126 [ContractInvariantMethod]
127 private void Invariants()
129 Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
132 public override string ToString()
134 return String.Format("{0}: _ ->{1}", this.Action, this.CloudFile.Name);
139 public class CloudUploadAction:CloudAction
141 public CloudUploadAction(AccountInfo accountInfo, FileSystemInfo fileInfo, FileState state, int blockSize, string algorithm)
142 : base(accountInfo, CloudActionType.UploadUnconditional, fileInfo,
143 new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName,fileInfo), state, blockSize, algorithm)
147 [ContractInvariantMethod]
148 private void Invariants()
150 Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
155 public class CloudMoveAction:CloudAction
157 public ObjectInfo OldCloudFile { get; set; }
159 public FileSystemInfo OldLocalFile { get; set; }
161 public CloudMoveAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo oldFile, FileSystemInfo newFile)
162 :base(accountInfo,action)
165 CloudFile = new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName, newFile);
167 OldLocalFile = oldFile;
168 OldCloudFile = new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName, oldFile);
170 //This is a rename operation, a hash will not be used
171 LocalHash = new Lazy<string>(() => String.Empty, LazyThreadSafetyMode.ExecutionAndPublication);
174 public override string ToString()
176 return String.Format("{0}:{1}->{2}", this.Action, OldCloudFile.Name, CloudFile.Name);