root / trunk / Pithos.Core / Agents / CloudTransferAction.cs @ ab2f6f79
History | View | Annotate | Download (5.7 kB)
1 |
using System; |
---|---|
2 |
using System.Diagnostics.Contracts; |
3 |
using System.IO; |
4 |
using System.Threading; |
5 |
using Pithos.Interfaces; |
6 |
using Pithos.Network; |
7 |
|
8 |
namespace Pithos.Core.Agents |
9 |
{ |
10 |
public enum CloudActionType |
11 |
{ |
12 |
MustSynch, |
13 |
UploadUnconditional, |
14 |
DownloadUnconditional, |
15 |
DeleteLocal, |
16 |
DeleteCloud, |
17 |
RenameCloud |
18 |
} |
19 |
|
20 |
public class CloudAction |
21 |
{ |
22 |
public AccountInfo AccountInfo { get; set; } |
23 |
public CloudActionType Action { get; set; } |
24 |
public FileInfo LocalFile { get; set; } |
25 |
public ObjectInfo CloudFile { get; set; } |
26 |
public FileState FileState { get; set; } |
27 |
public string Container { get; set; } |
28 |
|
29 |
|
30 |
public Lazy<string> LocalHash { get; protected set; } |
31 |
private Lazy<string> _topHash; |
32 |
public Lazy<string> TopHash |
33 |
{ |
34 |
get { return _topHash; } |
35 |
set { _topHash = value; } |
36 |
} |
37 |
|
38 |
|
39 |
[ContractInvariantMethod] |
40 |
private void Invariants() |
41 |
{ |
42 |
Contract.Invariant(AccountInfo!=null); |
43 |
} |
44 |
|
45 |
protected CloudAction(AccountInfo accountInfo,CloudActionType action) |
46 |
{ |
47 |
if (accountInfo==null) |
48 |
throw new ArgumentNullException("accountInfo"); |
49 |
Contract.EndContractBlock(); |
50 |
|
51 |
Action = action; |
52 |
AccountInfo = accountInfo; |
53 |
} |
54 |
|
55 |
public CloudAction(AccountInfo accountInfo, CloudActionType action, FileInfo localFile, ObjectInfo cloudFile, FileState state, int blockSize, string algorithm) |
56 |
: this(accountInfo,action) |
57 |
{ |
58 |
LocalFile = localFile; |
59 |
CloudFile = cloudFile; |
60 |
FileState = state; |
61 |
if (LocalFile != null) |
62 |
{ |
63 |
LocalHash = new Lazy<string>(() => LocalFile.CalculateHash(blockSize,algorithm), |
64 |
LazyThreadSafetyMode.ExecutionAndPublication); |
65 |
} |
66 |
} |
67 |
|
68 |
//Calculate the download path for the cloud file |
69 |
public string GetDownloadPath() |
70 |
{ |
71 |
if (CloudFile == null) |
72 |
return String.Empty; |
73 |
var filePath = CloudFile.RelativeUrlToFilePath(AccountInfo.UserName); |
74 |
return Path.Combine(AccountInfo.AccountPath, filePath); |
75 |
} |
76 |
|
77 |
public override string ToString() |
78 |
{ |
79 |
return String.Format("{0}:{1}->{2}", this.Action, this.LocalFile.FullName, this.CloudFile.Name); |
80 |
} |
81 |
} |
82 |
|
83 |
public class CloudDownloadAction:CloudAction |
84 |
{ |
85 |
public CloudDownloadAction(AccountInfo accountInfo, ObjectInfo cloudFile) |
86 |
:base(accountInfo,CloudActionType.DownloadUnconditional) |
87 |
{ |
88 |
if (String.IsNullOrWhiteSpace(cloudFile.Container)) |
89 |
throw new ArgumentException("CloudFile.Container","cloudFile"); |
90 |
Contract.EndContractBlock(); |
91 |
|
92 |
CloudFile = cloudFile; |
93 |
} |
94 |
|
95 |
[ContractInvariantMethod] |
96 |
private void Invariants() |
97 |
{ |
98 |
Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container)); |
99 |
} |
100 |
|
101 |
public override string ToString() |
102 |
{ |
103 |
return String.Format("{0}: _ <- {1}", this.Action, this.CloudFile.Name); |
104 |
} |
105 |
|
106 |
} |
107 |
public class CloudDeleteAction:CloudAction |
108 |
{ |
109 |
public CloudDeleteAction(AccountInfo accountInfo,FileInfo fileInfo, FileState fileState) |
110 |
: this(accountInfo,new ObjectInfo(accountInfo.AccountPath,accountInfo.UserName,fileInfo),fileState) |
111 |
{ |
112 |
} |
113 |
|
114 |
public CloudDeleteAction(AccountInfo accountInfo, ObjectInfo cloudFile, FileState fileState) |
115 |
: base(accountInfo,CloudActionType.DeleteCloud) |
116 |
{ |
117 |
CloudFile = cloudFile; |
118 |
FileState = fileState; |
119 |
} |
120 |
|
121 |
public CloudDeleteAction(CloudAction action) |
122 |
: this(action.AccountInfo,action.CloudFile,action.FileState) |
123 |
{} |
124 |
|
125 |
[ContractInvariantMethod] |
126 |
private void Invariants() |
127 |
{ |
128 |
Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container)); |
129 |
} |
130 |
|
131 |
public override string ToString() |
132 |
{ |
133 |
return String.Format("{0}: _ ->{1}", this.Action, this.CloudFile.Name); |
134 |
} |
135 |
|
136 |
} |
137 |
|
138 |
public class CloudUploadAction:CloudAction |
139 |
{ |
140 |
public CloudUploadAction(AccountInfo accountInfo, FileInfo fileInfo, FileState state, int blockSize, string algorithm) |
141 |
: base(accountInfo, CloudActionType.UploadUnconditional, fileInfo, |
142 |
new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName,fileInfo), state, blockSize, algorithm) |
143 |
{ |
144 |
} |
145 |
|
146 |
[ContractInvariantMethod] |
147 |
private void Invariants() |
148 |
{ |
149 |
Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container)); |
150 |
} |
151 |
|
152 |
} |
153 |
|
154 |
public class CloudMoveAction:CloudAction |
155 |
{ |
156 |
public ObjectInfo OldCloudFile { get; set; } |
157 |
|
158 |
public FileInfo OldLocalFile { get; set; } |
159 |
|
160 |
public CloudMoveAction(AccountInfo accountInfo, CloudActionType action, FileInfo oldFile, FileInfo newFile) |
161 |
:base(accountInfo,action) |
162 |
{ |
163 |
LocalFile = newFile; |
164 |
CloudFile = new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName, newFile); |
165 |
|
166 |
OldLocalFile = oldFile; |
167 |
OldCloudFile = new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName, oldFile); |
168 |
|
169 |
//This is a rename operation, a hash will not be used |
170 |
LocalHash = new Lazy<string>(() => String.Empty, LazyThreadSafetyMode.ExecutionAndPublication); |
171 |
} |
172 |
|
173 |
public override string ToString() |
174 |
{ |
175 |
return String.Format("{0}:{1}->{2}", this.Action, OldCloudFile.Name, CloudFile.Name); |
176 |
} |
177 |
|
178 |
} |
179 |
|
180 |
} |