Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / CloudTransferAction.cs @ 1bfc38f1

History | View | Annotate | Download (4.2 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

    
78
    public class CloudDownloadAction:CloudAction
79
    {
80
        public CloudDownloadAction(AccountInfo accountInfo, ObjectInfo cloudFile)
81
            :base(accountInfo,CloudActionType.DownloadUnconditional)
82
        {            
83
            CloudFile = cloudFile;
84
        }
85
        
86
    }
87
    public class CloudDeleteAction:CloudAction
88
    {
89
        public CloudDeleteAction(AccountInfo accountInfo, string fileName, FileState fileState)
90
            : this(accountInfo,new ObjectInfo { Name = fileName },fileState)
91
        {
92
        }
93

    
94
        public CloudDeleteAction(AccountInfo accountInfo, ObjectInfo cloudFile, FileState fileState) 
95
            : base(accountInfo,CloudActionType.DeleteCloud)
96
        {
97
            CloudFile = cloudFile;
98
            FileState = fileState;
99
        }
100

    
101
        public CloudDeleteAction(CloudAction action)
102
            : this(action.AccountInfo,action.CloudFile,action.FileState)
103
        {}
104
    }
105

    
106
    public class CloudUploadAction:CloudAction
107
    {
108
        public CloudUploadAction(AccountInfo accountInfo, FileInfo fileInfo, FileState state, int blockSize, string algorithm) 
109
            : base(accountInfo,CloudActionType.UploadUnconditional,fileInfo,ObjectInfo.Empty,state,blockSize,algorithm)
110
        {
111
        }
112
    }
113

    
114
    public class CloudMoveAction:CloudAction
115
    {
116
        public string OldFileName { get; set; }
117
        public string OldPath { get; set; }
118
        public string NewFileName { get; set; }
119
        public string NewPath { get; set; }
120

    
121
        public CloudMoveAction(AccountInfo accountInfo, CloudActionType action, string oldPath, string oldFileName, string newFileName, string newPath)
122
            :base(accountInfo,action)
123
        {
124
            OldFileName = oldFileName;
125
            OldPath = oldPath;
126
            NewFileName = newFileName;
127
            NewPath = newPath;
128
            //This is a rename operation, a hash will not be used
129
            LocalHash = new Lazy<string>(() => String.Empty, LazyThreadSafetyMode.ExecutionAndPublication);
130
        }
131

    
132

    
133
    }
134

    
135
}