root / trunk / Pithos.Interfaces / ObjectInfo.cs @ cfed7823
History | View | Annotate | Download (6 kB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Diagnostics.Contracts; |
4 |
using System.Globalization; |
5 |
using System.IO; |
6 |
|
7 |
namespace Pithos.Interfaces |
8 |
{ |
9 |
public class ObjectInfo |
10 |
{ |
11 |
private List<string> _knownContainers= new List<string>{"pithos","trash"}; |
12 |
public string Name { get; set; } |
13 |
public string Hash { get; set; } |
14 |
public long Bytes { get; set; } |
15 |
public string Content_Type { get; set; } |
16 |
public DateTime Last_Modified { get; set; } |
17 |
|
18 |
private Dictionary<string, string> _tags=new Dictionary<string, string>(); |
19 |
public Dictionary<string, string> Tags |
20 |
{ |
21 |
get { return _tags; } |
22 |
set { _tags = value; } |
23 |
} |
24 |
|
25 |
private Dictionary<string, string> _extensions=new Dictionary<string, string>(); |
26 |
public Dictionary<string, string> Extensions |
27 |
{ |
28 |
get { return _extensions; } |
29 |
set |
30 |
{ |
31 |
_extensions = value; |
32 |
ExtractKnownExtensions(); |
33 |
} |
34 |
} |
35 |
|
36 |
private long? _version; |
37 |
public long? Version |
38 |
{ |
39 |
get { return _version; } |
40 |
set { _version = value; } |
41 |
} |
42 |
|
43 |
//Alias for version, for Json deserialization purposes |
44 |
public long? X_Object_Version |
45 |
{ |
46 |
get { return _version; } |
47 |
set { _version = value; } |
48 |
} |
49 |
|
50 |
private string _allowedTo; |
51 |
public string AllowedTo |
52 |
{ |
53 |
get { return _allowedTo; } |
54 |
set { _allowedTo = value; } |
55 |
} |
56 |
|
57 |
//Object permissions for Json deserialization, can be Read or Write |
58 |
|
59 |
public string x_object_allowed_to |
60 |
{ |
61 |
get { return _allowedTo; } |
62 |
set { _allowedTo = value; } |
63 |
} |
64 |
|
65 |
//Object permissions for HEAD deserialization, can be Read or Write |
66 |
public string X_Object_Allowed_To |
67 |
{ |
68 |
get { return _allowedTo; } |
69 |
set { _allowedTo = value; } |
70 |
} |
71 |
|
72 |
//Alias for VersionTimestamp, for Json deserialization purposes |
73 |
//The x_object_version_timestamp is a unix timestamp. |
74 |
public double? X_Object_Version_Timestamp |
75 |
{ |
76 |
get |
77 |
{ |
78 |
if (_versionTimestamp.HasValue) |
79 |
return (_versionTimestamp.Value-_epoch).TotalSeconds; |
80 |
return null; |
81 |
} |
82 |
set |
83 |
{ |
84 |
if (value.HasValue) |
85 |
{ |
86 |
_versionTimestamp = _epoch.AddSeconds(value.Value); |
87 |
} |
88 |
else |
89 |
{ |
90 |
_versionTimestamp = null; |
91 |
} |
92 |
} |
93 |
} |
94 |
|
95 |
private DateTime? _versionTimestamp; |
96 |
public DateTime? VersionTimestamp |
97 |
{ |
98 |
get { return _versionTimestamp; } |
99 |
set { _versionTimestamp = value; } |
100 |
} |
101 |
|
102 |
public string ModifiedBy |
103 |
{ |
104 |
get{ return _modifiedBy; } |
105 |
set{ _modifiedBy = value; } |
106 |
} |
107 |
|
108 |
//Alias for ModifiedBy, for Json deserialization purposes |
109 |
public string X_Object_Modified_By |
110 |
{ |
111 |
get{ return _modifiedBy; } |
112 |
set{ _modifiedBy = value; } |
113 |
} |
114 |
|
115 |
public Stream Stream { get; set; } |
116 |
|
117 |
public string Account { get; set; } |
118 |
|
119 |
public string Container { get; set; } |
120 |
|
121 |
|
122 |
private void ExtractKnownExtensions() |
123 |
{ |
124 |
Version=GetLong(KnownExtensions.X_Object_Version); |
125 |
VersionTimestamp = GetTimestamp(KnownExtensions.X_Object_Version_Timestamp); |
126 |
ModifiedBy = GetString(KnownExtensions.X_Object_Modified_By); |
127 |
} |
128 |
|
129 |
private string GetString(string name) |
130 |
{ |
131 |
var value=String.Empty; |
132 |
_extensions.TryGetValue(name, out value); |
133 |
return value ; |
134 |
} |
135 |
|
136 |
private long? GetLong(string name) |
137 |
{ |
138 |
string version; |
139 |
long value; |
140 |
return _extensions.TryGetValue(name, out version) && long.TryParse(version, out value) |
141 |
? (long?) value |
142 |
: null; |
143 |
} |
144 |
|
145 |
private DateTime? GetTimestamp(string name) |
146 |
{ |
147 |
string version; |
148 |
DateTime value; |
149 |
if (_extensions.TryGetValue(name, out version) && |
150 |
DateTime.TryParse(version,CultureInfo.InvariantCulture,DateTimeStyles.AdjustToUniversal, out value)) |
151 |
{ |
152 |
return value; |
153 |
} |
154 |
return null; |
155 |
} |
156 |
|
157 |
|
158 |
public static ObjectInfo Empty = new ObjectInfo |
159 |
{ |
160 |
Name = String.Empty, |
161 |
Hash = String.Empty, |
162 |
Bytes = 0, |
163 |
Content_Type = String.Empty, |
164 |
Last_Modified = DateTime.MinValue |
165 |
}; |
166 |
|
167 |
private string _modifiedBy; |
168 |
private DateTime _epoch = new DateTime(1970, 1, 1); |
169 |
|
170 |
|
171 |
|
172 |
public string RelativeUrlToFilePath(string currentAccount) |
173 |
{ |
174 |
if (this.Name==null) |
175 |
throw new InvalidOperationException("Name can't be null"); |
176 |
if (String.IsNullOrWhiteSpace(currentAccount)) |
177 |
throw new ArgumentNullException("currentAccount"); |
178 |
Contract.EndContractBlock(); |
179 |
|
180 |
if (this == Empty) |
181 |
return String.Empty; |
182 |
|
183 |
var unescaped = Uri.UnescapeDataString(this.Name); |
184 |
var path = unescaped.Replace("/", "\\"); |
185 |
var pathParts=new Stack<string>(); |
186 |
pathParts.Push(path); |
187 |
if (!String.IsNullOrWhiteSpace(Container) && !_knownContainers.Contains(Container)) |
188 |
pathParts.Push(Container); |
189 |
if (!currentAccount.Equals(Account, StringComparison.InvariantCultureIgnoreCase)) |
190 |
{ |
191 |
if (Account != null) |
192 |
{ |
193 |
pathParts.Push(Account); |
194 |
pathParts.Push("others"); |
195 |
} |
196 |
} |
197 |
var finalPath=Path.Combine(pathParts.ToArray()); |
198 |
return finalPath; |
199 |
} |
200 |
|
201 |
} |
202 |
} |