2 /* -----------------------------------------------------------------------
3 * <copyright file="CloudTransferAction.cs" company="GRNet">
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * 1. Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * The views and conclusions contained in the software and
35 * documentation are those of the authors and should not be
36 * interpreted as representing official policies, either expressed
37 * or implied, of GRNET S.A.
39 * -----------------------------------------------------------------------
43 using System.Diagnostics.Contracts;
45 using System.Threading;
46 using Pithos.Interfaces;
49 namespace Pithos.Core.Agents
51 public enum CloudActionType
55 DownloadUnconditional,
62 public class CloudAction
65 public object Originator { get; set; }
66 public AccountInfo AccountInfo { get; set; }
67 public CloudActionType Action { get; set; }
68 public FileSystemInfo LocalFile { get; set; }
69 public ObjectInfo CloudFile { get; set; }
70 public FileState FileState { get; set; }
71 public string Container { get; set; }
73 public readonly DateTime Created = DateTime.Now;
76 public Lazy<TreeHash> TreeHash { get; protected set; }
77 //public Lazy<string> TopHash { get; set; }
80 [ContractInvariantMethod]
81 private void Invariants()
83 Contract.Invariant(AccountInfo!=null);
88 get { return CloudFile!=null && AccountInfo.UserName != CloudFile.Account; }
91 protected CloudAction(AccountInfo accountInfo,CloudActionType action,object originator)
93 if (accountInfo==null)
94 throw new ArgumentNullException("accountInfo");
95 Contract.EndContractBlock();
98 AccountInfo = accountInfo;
99 Originator = originator;
102 public CloudAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo localFile, ObjectInfo cloudFile, FileState state, int blockSize, string algorithm,object originator)
103 : this(accountInfo,action,originator)
106 throw new ArgumentOutOfRangeException("blockSize");
107 Contract.EndContractBlock();
108 LocalFile = localFile.WithProperCapitalization();
109 CloudFile = cloudFile;
112 if (LocalFile == null)
115 TreeHash = new Lazy<TreeHash>(() => Signature.CalculateTreeHash(LocalFile, blockSize,algorithm),
116 LazyThreadSafetyMode.ExecutionAndPublication);
119 //Calculate the download path for the cloud file
120 public string GetDownloadPath()
122 if (CloudFile == null)
124 var filePath = CloudFile.RelativeUrlToFilePath(AccountInfo.UserName);
125 return Path.Combine(AccountInfo.AccountPath, filePath);
128 public override string ToString()
130 return String.Format("{0}:{1}->{2}", Action, LocalFile.FullName, CloudFile.Name);
133 protected static ObjectInfo CreateObjectInfoFor(AccountInfo accountInfo, FileSystemInfo fileInfo)
135 if(accountInfo==null)
136 throw new ArgumentNullException("accountInfo");
138 throw new ArgumentNullException("fileInfo");
139 Contract.Ensures(Contract.Result<ObjectInfo>()!=null);
140 Contract.EndContractBlock();
142 var capitalizedFileInfo = fileInfo.WithProperCapitalization();
143 var fullLocalName = capitalizedFileInfo.FullName;
144 var othersPath = Path.Combine(accountInfo.AccountPath, FolderConstants.OthersFolder);
146 ObjectInfo objectInfo;
148 var isShared = fullLocalName.StartsWith(othersPath, StringComparison.InvariantCultureIgnoreCase);
151 var pathRelativeToOther = fullLocalName.Substring(othersPath.Length + 1);
152 var otherParts = pathRelativeToOther.Split('\\');
153 var otherName = otherParts[0];
154 var otherContainer = otherParts[1];
155 objectInfo=new ObjectInfo
158 Container = otherContainer,
159 Name = String.Join("/", otherParts.Splice(2))
163 objectInfo=new ObjectInfo(accountInfo.AccountPath, accountInfo.UserName, fileInfo);
165 objectInfo.Content_Type= (fileInfo is DirectoryInfo)
166 ? "appication/directory"
167 : "application/octet-stream";
172 public class CloudDownloadAction:CloudAction
174 public CloudDownloadAction(AccountInfo accountInfo, ObjectInfo cloudFile,object originator)
175 :base(accountInfo,CloudActionType.DownloadUnconditional,originator)
177 if (String.IsNullOrWhiteSpace(cloudFile.Container))
178 throw new ArgumentException("CloudFile.Container","cloudFile");
179 Contract.EndContractBlock();
181 CloudFile = cloudFile;
184 [ContractInvariantMethod]
185 private void Invariants()
187 Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
190 public override string ToString()
192 return String.Format("{0}: _ <- {1}", Action, CloudFile.Name);
196 public class CloudDeleteAction:CloudAction
198 public CloudDeleteAction(AccountInfo accountInfo,FileSystemInfo fileInfo, FileState fileState,object originator)
199 : this(accountInfo,fileInfo,CreateObjectInfoFor(accountInfo, fileInfo),fileState,originator)
203 public CloudDeleteAction(AccountInfo accountInfo, FileSystemInfo fileInfo,ObjectInfo cloudFile, FileState fileState,object originator)
204 : base(accountInfo,CloudActionType.DeleteCloud,originator)
206 CloudFile = cloudFile;
207 LocalFile = fileInfo;
208 FileState = fileState;
211 public CloudDeleteAction(CloudAction action)
212 : this(action.AccountInfo,action.LocalFile,action.CloudFile,action.FileState,action)
215 [ContractInvariantMethod]
216 private void Invariants()
218 Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
221 public override string ToString()
223 return String.Format("{0}: _ ->{1}", Action, CloudFile.Name);
228 public class CloudUploadAction:CloudAction
230 public CloudUploadAction(AccountInfo accountInfo, FileSystemInfo fileInfo, FileState state, int blockSize, string algorithm,object originator)
231 : base(accountInfo, CloudActionType.UploadUnconditional,fileInfo,CreateObjectInfoFor(accountInfo,fileInfo),state,blockSize,algorithm,originator)
235 [ContractInvariantMethod]
236 private void Invariants()
238 Contract.Invariant(!String.IsNullOrWhiteSpace(CloudFile.Container));
243 public class CloudMoveAction:CloudAction
245 public ObjectInfo OldCloudFile { get; set; }
247 public FileSystemInfo OldLocalFile { get; set; }
249 public CloudMoveAction(AccountInfo accountInfo, CloudActionType action, FileSystemInfo oldFile, FileSystemInfo newFile,object originator)
250 :base(accountInfo,action,originator)
253 CloudFile = CreateObjectInfoFor(accountInfo, newFile);
255 OldLocalFile = oldFile;
256 OldCloudFile = CreateObjectInfoFor(accountInfo, oldFile);
258 //This is a rename operation, a hash will not be used
259 TreeHash = new Lazy<TreeHash>(() => Network.TreeHash.Empty, LazyThreadSafetyMode.ExecutionAndPublication);
262 public override string ToString()
264 return String.Format("{0}:{1}->{2}", Action, OldCloudFile.Name, CloudFile.Name);