root / trunk / Pithos.Core / Agents / CloudTransferAction.cs @ 8f44fd3a
History | View | Annotate | Download (9.5 kB)
1 | 255f5f86 | Panagiotis Kanavos | #region |
---|---|---|---|
2 | 255f5f86 | Panagiotis Kanavos | /* ----------------------------------------------------------------------- |
3 | 255f5f86 | Panagiotis Kanavos | * <copyright file="CloudTransferAction.cs" company="GRNet"> |
4 | 255f5f86 | Panagiotis Kanavos | * |
5 | 255f5f86 | Panagiotis Kanavos | * Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 | 255f5f86 | Panagiotis Kanavos | * |
7 | 255f5f86 | Panagiotis Kanavos | * Redistribution and use in source and binary forms, with or |
8 | 255f5f86 | Panagiotis Kanavos | * without modification, are permitted provided that the following |
9 | 255f5f86 | Panagiotis Kanavos | * conditions are met: |
10 | 255f5f86 | Panagiotis Kanavos | * |
11 | 255f5f86 | Panagiotis Kanavos | * 1. Redistributions of source code must retain the above |
12 | 255f5f86 | Panagiotis Kanavos | * copyright notice, this list of conditions and the following |
13 | 255f5f86 | Panagiotis Kanavos | * disclaimer. |
14 | 255f5f86 | Panagiotis Kanavos | * |
15 | 255f5f86 | Panagiotis Kanavos | * 2. Redistributions in binary form must reproduce the above |
16 | 255f5f86 | Panagiotis Kanavos | * copyright notice, this list of conditions and the following |
17 | 255f5f86 | Panagiotis Kanavos | * disclaimer in the documentation and/or other materials |
18 | 255f5f86 | Panagiotis Kanavos | * provided with the distribution. |
19 | 255f5f86 | Panagiotis Kanavos | * |
20 | 255f5f86 | Panagiotis Kanavos | * |
21 | 255f5f86 | Panagiotis Kanavos | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
22 | 255f5f86 | Panagiotis Kanavos | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | 255f5f86 | Panagiotis Kanavos | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
24 | 255f5f86 | Panagiotis Kanavos | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
25 | 255f5f86 | Panagiotis Kanavos | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 | 255f5f86 | Panagiotis Kanavos | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 | 255f5f86 | Panagiotis Kanavos | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
28 | 255f5f86 | Panagiotis Kanavos | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
29 | 255f5f86 | Panagiotis Kanavos | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | 255f5f86 | Panagiotis Kanavos | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
31 | 255f5f86 | Panagiotis Kanavos | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 | 255f5f86 | Panagiotis Kanavos | * POSSIBILITY OF SUCH DAMAGE. |
33 | 255f5f86 | Panagiotis Kanavos | * |
34 | 255f5f86 | Panagiotis Kanavos | * The views and conclusions contained in the software and |
35 | 255f5f86 | Panagiotis Kanavos | * documentation are those of the authors and should not be |
36 | 255f5f86 | Panagiotis Kanavos | * interpreted as representing official policies, either expressed |
37 | 255f5f86 | Panagiotis Kanavos | * or implied, of GRNET S.A. |
38 | 255f5f86 | Panagiotis Kanavos | * </copyright> |
39 | 255f5f86 | Panagiotis Kanavos | * ----------------------------------------------------------------------- |
40 | 255f5f86 | Panagiotis Kanavos | */ |
41 | 255f5f86 | Panagiotis Kanavos | #endregion |
42 | 1caef52e | Panagiotis Kanavos | using System; |
43 | c53aa229 | Panagiotis Kanavos | using System.Diagnostics.Contracts; |
44 | 1caef52e | Panagiotis Kanavos | using System.IO; |
45 | 1caef52e | Panagiotis Kanavos | using System.Threading; |
46 | 1caef52e | Panagiotis Kanavos | using Pithos.Interfaces; |
47 | 1caef52e | Panagiotis Kanavos | using Pithos.Network; |
48 | 1caef52e | Panagiotis Kanavos | |
49 | 1caef52e | Panagiotis Kanavos | namespace Pithos.Core.Agents |
50 | 1caef52e | Panagiotis Kanavos | { |
51 | 1caef52e | Panagiotis Kanavos | public enum CloudActionType |
52 | 1caef52e | Panagiotis Kanavos | { |
53 | 1caef52e | Panagiotis Kanavos | MustSynch, |
54 | 1caef52e | Panagiotis Kanavos | UploadUnconditional, |
55 | 1caef52e | Panagiotis Kanavos | DownloadUnconditional, |
56 | 1caef52e | Panagiotis Kanavos | DeleteLocal, |
57 | 1caef52e | Panagiotis Kanavos | DeleteCloud, |
58 | b666b39a | Panagiotis Kanavos | RenameCloud, |
59 | b666b39a | Panagiotis Kanavos | RenameLocal |
60 | 1caef52e | Panagiotis Kanavos | } |
61 | 1caef52e | Panagiotis Kanavos | |
62 | 1caef52e | Panagiotis Kanavos | public class CloudAction |
63 | 1caef52e | Panagiotis Kanavos | { |
64 | c53aa229 | Panagiotis Kanavos | public AccountInfo AccountInfo { get; set; } |
65 | 1caef52e | Panagiotis Kanavos | public CloudActionType Action { get; set; } |
66 | 4f6d51d4 | Panagiotis Kanavos | public FileSystemInfo LocalFile { get; set; } |
67 | 1caef52e | Panagiotis Kanavos | public ObjectInfo CloudFile { get; set; } |
68 | bfc13ed8 | Panagiotis Kanavos | public FileState FileState { get; set; } |
69 | cfed7823 | Panagiotis Kanavos | public string Container { get; set; } |
70 | 1caef52e | Panagiotis Kanavos | |
71 | 039a89e5 | Panagiotis Kanavos | public readonly DateTime Created = DateTime.Now; |
72 | 039a89e5 | Panagiotis Kanavos | |
73 | 1caef52e | Panagiotis Kanavos | |
74 | 174bbb6e | Panagiotis Kanavos | public Lazy<TreeHash> TreeHash { get; protected set; } |
75 | 174bbb6e | Panagiotis Kanavos | //public Lazy<string> TopHash { get; set; } |
76 | 1caef52e | Panagiotis Kanavos | |
77 | bfc13ed8 | Panagiotis Kanavos | |
78 | c53aa229 | Panagiotis Kanavos | [ContractInvariantMethod] |
79 | c53aa229 | Panagiotis Kanavos | private void Invariants() |
80 | 1caef52e | Panagiotis Kanavos | { |
81 | c53aa229 | Panagiotis Kanavos | Contract.Invariant(AccountInfo!=null); |
82 | c53aa229 | Panagiotis Kanavos | } |
83 | c53aa229 | Panagiotis Kanavos | |
84 | 2edb4807 | Panagiotis Kanavos | public bool IsShared |
85 | 2edb4807 | Panagiotis Kanavos | { |
86 | 2edb4807 | Panagiotis Kanavos | get { return CloudFile!=null && AccountInfo.UserName != CloudFile.Account; } |
87 | 2edb4807 | Panagiotis Kanavos | } |
88 | 2edb4807 | Panagiotis Kanavos | |
89 | c53aa229 | Panagiotis Kanavos | protected CloudAction(AccountInfo accountInfo,CloudActionType action) |
90 | c53aa229 | Panagiotis Kanavos | { |
91 | c53aa229 | Panagiotis Kanavos | if (accountInfo==null) |
92 | c53aa229 | Panagiotis Kanavos | throw new ArgumentNullException("accountInfo"); |
93 | c53aa229 | Panagiotis Kanavos | Contract.EndContractBlock(); |
94 | c53aa229 | Panagiotis Kanavos | |
95 | 1caef52e | Panagiotis Kanavos | Action = action; |
96 | c53aa229 | Panagiotis Kanavos | AccountInfo = accountInfo; |
97 | 1caef52e | Panagiotis Kanavos | } |
98 | 1caef52e | Panagiotis Kanavos | |
99 | 4f6d51d4 | Panagiotis Kanavos | public CloudAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo localFile, ObjectInfo cloudFile, FileState state, int blockSize, string algorithm) |
100 | c53aa229 | Panagiotis Kanavos | : this(accountInfo,action) |
101 | 1caef52e | Panagiotis Kanavos | { |
102 | e394ef0f | pkanavos | if(blockSize<=0) |
103 | e394ef0f | pkanavos | throw new ArgumentOutOfRangeException("blockSize"); |
104 | e394ef0f | pkanavos | Contract.EndContractBlock(); |
105 | f3d080df | Panagiotis Kanavos | LocalFile = localFile.WithProperCapitalization(); |
106 | 1caef52e | Panagiotis Kanavos | CloudFile = cloudFile; |
107 | bfc13ed8 | Panagiotis Kanavos | FileState = state; |
108 | 174bbb6e | Panagiotis Kanavos | |
109 | 174bbb6e | Panagiotis Kanavos | if (LocalFile == null) |
110 | 174bbb6e | Panagiotis Kanavos | return; |
111 | 4f6d51d4 | Panagiotis Kanavos | |
112 | 174bbb6e | Panagiotis Kanavos | TreeHash = new Lazy<TreeHash>(() => Signature.CalculateTreeHash(LocalFile, blockSize,algorithm), |
113 | 174bbb6e | Panagiotis Kanavos | LazyThreadSafetyMode.ExecutionAndPublication); |
114 | bfc13ed8 | Panagiotis Kanavos | } |
115 | bfc13ed8 | Panagiotis Kanavos | |
116 | 1bfc38f1 | Panagiotis Kanavos | //Calculate the download path for the cloud file |
117 | 1bfc38f1 | Panagiotis Kanavos | public string GetDownloadPath() |
118 | 1bfc38f1 | Panagiotis Kanavos | { |
119 | 1bfc38f1 | Panagiotis Kanavos | if (CloudFile == null) |
120 | 1bfc38f1 | Panagiotis Kanavos | return String.Empty; |
121 | 1bfc38f1 | Panagiotis Kanavos | var filePath = CloudFile.RelativeUrlToFilePath(AccountInfo.UserName); |
122 | 1bfc38f1 | Panagiotis Kanavos | return Path.Combine(AccountInfo.AccountPath, filePath); |
123 | 1bfc38f1 | Panagiotis Kanavos | } |
124 | 27361404 | Panagiotis Kanavos | |
125 | 27361404 | Panagiotis Kanavos | public override string ToString() |
126 | 27361404 | Panagiotis Kanavos | { |
127 | ec1a1baf | Panagiotis Kanavos | return String.Format("{0}:{1}->{2}", Action, LocalFile.FullName, CloudFile.Name); |
128 | 27361404 | Panagiotis Kanavos | } |
129 | 70f12b36 | Panagiotis Kanavos | |
130 | 70f12b36 | Panagiotis Kanavos | protected static ObjectInfo CreateObjectInfoFor(AccountInfo accountInfo, FileSystemInfo fileInfo) |
131 | 70f12b36 | Panagiotis Kanavos | { |
132 | d78d765c | pkanavos | if(accountInfo==null) |
133 | d78d765c | pkanavos | throw new ArgumentNullException("accountInfo"); |
134 | d78d765c | pkanavos | if(fileInfo==null) |
135 | d78d765c | pkanavos | throw new ArgumentNullException("fileInfo"); |
136 | 70f12b36 | Panagiotis Kanavos | Contract.Ensures(Contract.Result<ObjectInfo>()!=null); |
137 | d78d765c | pkanavos | Contract.EndContractBlock(); |
138 | 70f12b36 | Panagiotis Kanavos | |
139 | 70f12b36 | Panagiotis Kanavos | var capitalizedFileInfo = fileInfo.WithProperCapitalization(); |
140 | 70f12b36 | Panagiotis Kanavos | var fullLocalName = capitalizedFileInfo.FullName; |
141 | 70f12b36 | Panagiotis Kanavos | var othersPath = Path.Combine(accountInfo.AccountPath, FolderConstants.OthersFolder); |
142 | 8f44fd3a | pkanavos | |
143 | 8f44fd3a | pkanavos | ObjectInfo objectInfo; |
144 | 8f44fd3a | pkanavos | |
145 | 70f12b36 | Panagiotis Kanavos | var isShared = fullLocalName.StartsWith(othersPath, StringComparison.InvariantCultureIgnoreCase); |
146 | 70f12b36 | Panagiotis Kanavos | if (isShared) |
147 | 70f12b36 | Panagiotis Kanavos | { |
148 | 70f12b36 | Panagiotis Kanavos | var pathRelativeToOther = fullLocalName.Substring(othersPath.Length + 1); |
149 | 70f12b36 | Panagiotis Kanavos | var otherParts = pathRelativeToOther.Split('\\'); |
150 | 70f12b36 | Panagiotis Kanavos | var otherName = otherParts[0]; |
151 | 70f12b36 | Panagiotis Kanavos | var otherContainer = otherParts[1]; |
152 | 8f44fd3a | pkanavos | objectInfo=new ObjectInfo |
153 | 70f12b36 | Panagiotis Kanavos | { |
154 | 70f12b36 | Panagiotis Kanavos | Account = otherName, |
155 | 70f12b36 | Panagiotis Kanavos | Container = otherContainer, |
156 | 70f12b36 | Panagiotis Kanavos | Name = String.Join("/", otherParts.Splice(2)) |
157 | 70f12b36 | Panagiotis Kanavos | }; |
158 | 70f12b36 | Panagiotis Kanavos | } |
159 | 8f44fd3a | pkanavos | else |
160 | 8f44fd3a | pkanavos | objectInfo=new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName, fileInfo); |
161 | 8f44fd3a | pkanavos | |
162 | 8f44fd3a | pkanavos | objectInfo.Content_Type= (fileInfo is DirectoryInfo) |
163 | 8f44fd3a | pkanavos | ? "appication/directory" |
164 | 8f44fd3a | pkanavos | : "application/octet-stream"; |
165 | 8f44fd3a | pkanavos | return objectInfo; |
166 | 70f12b36 | Panagiotis Kanavos | } |
167 | 5120f3cb | Panagiotis Kanavos | } |
168 | 5120f3cb | Panagiotis Kanavos | |
169 | 5120f3cb | Panagiotis Kanavos | public class CloudDownloadAction:CloudAction |
170 | 5120f3cb | Panagiotis Kanavos | { |
171 | c53aa229 | Panagiotis Kanavos | public CloudDownloadAction(AccountInfo accountInfo, ObjectInfo cloudFile) |
172 | c53aa229 | Panagiotis Kanavos | :base(accountInfo,CloudActionType.DownloadUnconditional) |
173 | 5120f3cb | Panagiotis Kanavos | { |
174 | 27361404 | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(cloudFile.Container)) |
175 | 27361404 | Panagiotis Kanavos | throw new ArgumentException("CloudFile.Container","cloudFile"); |
176 | 27361404 | Panagiotis Kanavos | Contract.EndContractBlock(); |
177 | 27361404 | Panagiotis Kanavos | |
178 | 5120f3cb | Panagiotis Kanavos | CloudFile = cloudFile; |
179 | 5120f3cb | Panagiotis Kanavos | } |
180 | 27361404 | Panagiotis Kanavos | |
181 | 27361404 | Panagiotis Kanavos | [ContractInvariantMethod] |
182 | 27361404 | Panagiotis Kanavos | private void Invariants() |
183 | 27361404 | Panagiotis Kanavos | { |
184 | 27361404 | Panagiotis Kanavos | Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container)); |
185 | 27361404 | Panagiotis Kanavos | } |
186 | 27361404 | Panagiotis Kanavos | |
187 | 27361404 | Panagiotis Kanavos | public override string ToString() |
188 | 27361404 | Panagiotis Kanavos | { |
189 | 8f44fd3a | pkanavos | return String.Format("{0}: _ <- {1}", Action, CloudFile.Name); |
190 | 27361404 | Panagiotis Kanavos | } |
191 | 5120f3cb | Panagiotis Kanavos | |
192 | 5120f3cb | Panagiotis Kanavos | } |
193 | 5120f3cb | Panagiotis Kanavos | public class CloudDeleteAction:CloudAction |
194 | 5120f3cb | Panagiotis Kanavos | { |
195 | 4f6d51d4 | Panagiotis Kanavos | public CloudDeleteAction(AccountInfo accountInfo,FileSystemInfo fileInfo, FileState fileState) |
196 | 70f12b36 | Panagiotis Kanavos | : this(accountInfo,fileInfo,CreateObjectInfoFor(accountInfo, fileInfo),fileState) |
197 | e81dd1f6 | Panagiotis Kanavos | { |
198 | 5120f3cb | Panagiotis Kanavos | } |
199 | 5120f3cb | Panagiotis Kanavos | |
200 | e81dd1f6 | Panagiotis Kanavos | public CloudDeleteAction(AccountInfo accountInfo, FileSystemInfo fileInfo,ObjectInfo cloudFile, FileState fileState) |
201 | c53aa229 | Panagiotis Kanavos | : base(accountInfo,CloudActionType.DeleteCloud) |
202 | bfc13ed8 | Panagiotis Kanavos | { |
203 | bfc13ed8 | Panagiotis Kanavos | CloudFile = cloudFile; |
204 | e81dd1f6 | Panagiotis Kanavos | LocalFile = fileInfo; |
205 | 5120f3cb | Panagiotis Kanavos | FileState = fileState; |
206 | 1caef52e | Panagiotis Kanavos | } |
207 | 1bfc38f1 | Panagiotis Kanavos | |
208 | 1bfc38f1 | Panagiotis Kanavos | public CloudDeleteAction(CloudAction action) |
209 | e81dd1f6 | Panagiotis Kanavos | : this(action.AccountInfo,action.LocalFile,action.CloudFile,action.FileState) |
210 | 1bfc38f1 | Panagiotis Kanavos | {} |
211 | 27361404 | Panagiotis Kanavos | |
212 | 27361404 | Panagiotis Kanavos | [ContractInvariantMethod] |
213 | 27361404 | Panagiotis Kanavos | private void Invariants() |
214 | 27361404 | Panagiotis Kanavos | { |
215 | 27361404 | Panagiotis Kanavos | Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container)); |
216 | 27361404 | Panagiotis Kanavos | } |
217 | 27361404 | Panagiotis Kanavos | |
218 | 27361404 | Panagiotis Kanavos | public override string ToString() |
219 | 27361404 | Panagiotis Kanavos | { |
220 | ec1a1baf | Panagiotis Kanavos | return String.Format("{0}: _ ->{1}", Action, CloudFile.Name); |
221 | 27361404 | Panagiotis Kanavos | } |
222 | 27361404 | Panagiotis Kanavos | |
223 | 5120f3cb | Panagiotis Kanavos | } |
224 | 1caef52e | Panagiotis Kanavos | |
225 | 5120f3cb | Panagiotis Kanavos | public class CloudUploadAction:CloudAction |
226 | 5120f3cb | Panagiotis Kanavos | { |
227 | 4f6d51d4 | Panagiotis Kanavos | public CloudUploadAction(AccountInfo accountInfo, FileSystemInfo fileInfo, FileState state, int blockSize, string algorithm) |
228 | 70f12b36 | Panagiotis Kanavos | : base(accountInfo, CloudActionType.UploadUnconditional,fileInfo,CreateObjectInfoFor(accountInfo,fileInfo),state,blockSize,algorithm) |
229 | 5120f3cb | Panagiotis Kanavos | { |
230 | 27361404 | Panagiotis Kanavos | } |
231 | ec6f3895 | Panagiotis Kanavos | |
232 | 27361404 | Panagiotis Kanavos | [ContractInvariantMethod] |
233 | 27361404 | Panagiotis Kanavos | private void Invariants() |
234 | 27361404 | Panagiotis Kanavos | { |
235 | 27361404 | Panagiotis Kanavos | Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container)); |
236 | 5120f3cb | Panagiotis Kanavos | } |
237 | 27361404 | Panagiotis Kanavos | |
238 | 5120f3cb | Panagiotis Kanavos | } |
239 | cfed7823 | Panagiotis Kanavos | |
240 | cfed7823 | Panagiotis Kanavos | public class CloudMoveAction:CloudAction |
241 | cfed7823 | Panagiotis Kanavos | { |
242 | 27361404 | Panagiotis Kanavos | public ObjectInfo OldCloudFile { get; set; } |
243 | 27361404 | Panagiotis Kanavos | |
244 | 4f6d51d4 | Panagiotis Kanavos | public FileSystemInfo OldLocalFile { get; set; } |
245 | cfed7823 | Panagiotis Kanavos | |
246 | 4f6d51d4 | Panagiotis Kanavos | public CloudMoveAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo oldFile, FileSystemInfo newFile) |
247 | c53aa229 | Panagiotis Kanavos | :base(accountInfo,action) |
248 | cfed7823 | Panagiotis Kanavos | { |
249 | 27361404 | Panagiotis Kanavos | LocalFile = newFile; |
250 | 70f12b36 | Panagiotis Kanavos | CloudFile = CreateObjectInfoFor(accountInfo, newFile); |
251 | 27361404 | Panagiotis Kanavos | |
252 | 27361404 | Panagiotis Kanavos | OldLocalFile = oldFile; |
253 | 70f12b36 | Panagiotis Kanavos | OldCloudFile = CreateObjectInfoFor(accountInfo, oldFile); |
254 | 27361404 | Panagiotis Kanavos | |
255 | cfed7823 | Panagiotis Kanavos | //This is a rename operation, a hash will not be used |
256 | 174bbb6e | Panagiotis Kanavos | TreeHash = new Lazy<TreeHash>(() => Network.TreeHash.Empty, LazyThreadSafetyMode.ExecutionAndPublication); |
257 | cfed7823 | Panagiotis Kanavos | } |
258 | cfed7823 | Panagiotis Kanavos | |
259 | 27361404 | Panagiotis Kanavos | public override string ToString() |
260 | 27361404 | Panagiotis Kanavos | { |
261 | ec1a1baf | Panagiotis Kanavos | return String.Format("{0}:{1}->{2}", Action, OldCloudFile.Name, CloudFile.Name); |
262 | 27361404 | Panagiotis Kanavos | } |
263 | cfed7823 | Panagiotis Kanavos | |
264 | 1caef52e | Panagiotis Kanavos | } |
265 | cfed7823 | Panagiotis Kanavos | |
266 | 1caef52e | Panagiotis Kanavos | } |