root / trunk / Pithos.Interfaces / ObjectInfo.cs @ 759bd3c4
History | View | Annotate | Download (10.3 kB)
1 |
#region |
---|---|
2 |
/* ----------------------------------------------------------------------- |
3 |
* <copyright file="ObjectInfo.cs" company="GRNet"> |
4 |
* |
5 |
* Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 |
* |
7 |
* Redistribution and use in source and binary forms, with or |
8 |
* without modification, are permitted provided that the following |
9 |
* conditions are met: |
10 |
* |
11 |
* 1. Redistributions of source code must retain the above |
12 |
* copyright notice, this list of conditions and the following |
13 |
* disclaimer. |
14 |
* |
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. |
19 |
* |
20 |
* |
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. |
33 |
* |
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. |
38 |
* </copyright> |
39 |
* ----------------------------------------------------------------------- |
40 |
*/ |
41 |
#endregion |
42 |
using System; |
43 |
using System.Collections.Generic; |
44 |
using System.Diagnostics; |
45 |
using System.Diagnostics.Contracts; |
46 |
using System.Dynamic; |
47 |
using System.Globalization; |
48 |
using System.IO; |
49 |
using System.Linq; |
50 |
using System.Text; |
51 |
using Newtonsoft.Json; |
52 |
|
53 |
namespace Pithos.Interfaces |
54 |
{ |
55 |
[DebuggerDisplay("Name {Name}")] |
56 |
public class ObjectInfo:DynamicObject |
57 |
{ |
58 |
private readonly List<string> _knownContainers= new List<string>{"trash"}; |
59 |
public string Name { get; set; } |
60 |
|
61 |
|
62 |
public string Hash { get; set; } |
63 |
|
64 |
public string X_Object_Hash { get { return Hash; } set { Hash = value; } } |
65 |
|
66 |
[JsonProperty("x_object_uuid")] |
67 |
public string UUID { get; set; } |
68 |
|
69 |
public long Bytes { get; set; } |
70 |
public string Content_Type { get; set; } |
71 |
public DateTime Last_Modified { get; set; } |
72 |
|
73 |
private Dictionary<string, string> _tags=new Dictionary<string, string>(); |
74 |
public Dictionary<string, string> Tags |
75 |
{ |
76 |
get { return _tags; } |
77 |
set { _tags = value; } |
78 |
} |
79 |
|
80 |
private Dictionary<string, string> _extensions=new Dictionary<string, string>(); |
81 |
public Dictionary<string, string> Extensions |
82 |
{ |
83 |
get { return _extensions; } |
84 |
set |
85 |
{ |
86 |
_extensions = value; |
87 |
ExtractKnownExtensions(); |
88 |
} |
89 |
} |
90 |
|
91 |
|
92 |
private Dictionary<string, string> _permissions=new Dictionary<string, string>(); |
93 |
[JsonProperty("x_object_sharing")] |
94 |
[JsonConverter(typeof(PermissionConverter))] |
95 |
public Dictionary<string, string> Permissions |
96 |
{ |
97 |
get { return _permissions; } |
98 |
set |
99 |
{ |
100 |
_permissions = value; |
101 |
} |
102 |
} |
103 |
|
104 |
/// <summary> |
105 |
/// Version number |
106 |
/// </summary> |
107 |
[JsonProperty("x_object_version")] |
108 |
public long? Version { get; set; } |
109 |
|
110 |
|
111 |
/// <summary> |
112 |
/// Shared object permissions can be Read or Write |
113 |
/// </summary> |
114 |
[JsonProperty("x_object_allowed_to")] |
115 |
public string AllowedTo { get; set; } |
116 |
|
117 |
|
118 |
/// <summary> |
119 |
/// Version timestamp |
120 |
/// </summary> |
121 |
[JsonProperty("X_Object_Version_Timestamp"), JsonConverter(typeof(PithosDateTimeConverter))] |
122 |
public DateTime? VersionTimestamp { get; set; } |
123 |
|
124 |
[JsonProperty("X_Object_Modified_By")] |
125 |
public string ModifiedBy { get; set; } |
126 |
|
127 |
|
128 |
public Stream Stream { get; set; } |
129 |
|
130 |
|
131 |
public Uri StorageUri { get; set; } |
132 |
|
133 |
public string Account { get; set; } |
134 |
|
135 |
public string Container { get; set; } |
136 |
|
137 |
public Uri Uri |
138 |
{ |
139 |
get |
140 |
{ |
141 |
var relativeUrl=String.Format("{0}/{1}/{2}",Account, Container,Name); |
142 |
return new Uri(StorageUri,relativeUrl); |
143 |
} |
144 |
} |
145 |
|
146 |
public string ContendDisposition { get; set; } |
147 |
|
148 |
public string ContentEncoding { get; set; } |
149 |
|
150 |
public string Manifest { get; set; } |
151 |
|
152 |
public bool IsPublic |
153 |
{ |
154 |
get { return !String.IsNullOrWhiteSpace(PublicUrl); } |
155 |
set |
156 |
{ |
157 |
if (!value) |
158 |
PublicUrl = null; |
159 |
else if (String.IsNullOrWhiteSpace(PublicUrl)) |
160 |
PublicUrl="true"; |
161 |
} |
162 |
} |
163 |
|
164 |
[JsonProperty("X_Object_Public")] |
165 |
public string PublicUrl { get; set; } |
166 |
|
167 |
public ObjectInfo() |
168 |
{} |
169 |
|
170 |
public ObjectInfo(string accountPath,string accountName,FileSystemInfo fileInfo) |
171 |
{ |
172 |
if (String.IsNullOrWhiteSpace(accountPath)) |
173 |
throw new ArgumentNullException("accountPath"); |
174 |
if (string.IsNullOrWhiteSpace(accountName)) |
175 |
throw new ArgumentNullException("accountName"); |
176 |
if (fileInfo == null) |
177 |
throw new ArgumentNullException("fileInfo"); |
178 |
Contract.EndContractBlock(); |
179 |
|
180 |
var relativeUrl = fileInfo.WithProperCapitalization().AsRelativeUrlTo(accountPath); |
181 |
//The first part of the URL is the container |
182 |
var slashIndex = relativeUrl.IndexOf('/'); |
183 |
var container = relativeUrl.Substring(0, slashIndex); |
184 |
//The second is the file's url relative to the container |
185 |
var fileUrl = relativeUrl.Substring(slashIndex + 1); |
186 |
|
187 |
Account = accountName; |
188 |
Container = container; |
189 |
Name = fileUrl; |
190 |
} |
191 |
|
192 |
|
193 |
private void ExtractKnownExtensions() |
194 |
{ |
195 |
Version=GetLong(KnownExtensions.X_Object_Version); |
196 |
VersionTimestamp = GetTimestamp(KnownExtensions.X_Object_Version_Timestamp); |
197 |
ModifiedBy = GetString(KnownExtensions.X_Object_Modified_By); |
198 |
} |
199 |
|
200 |
private string GetString(string name) |
201 |
{ |
202 |
string value; |
203 |
_extensions.TryGetValue(name, out value); |
204 |
return value ; |
205 |
} |
206 |
|
207 |
private long? GetLong(string name) |
208 |
{ |
209 |
string version; |
210 |
long value; |
211 |
return _extensions.TryGetValue(name, out version) && long.TryParse(version, out value) |
212 |
? (long?) value |
213 |
: null; |
214 |
} |
215 |
|
216 |
private DateTime? GetTimestamp(string name) |
217 |
{ |
218 |
string version; |
219 |
DateTime value; |
220 |
if (_extensions.TryGetValue(name, out version) && |
221 |
DateTime.TryParse(version,CultureInfo.InvariantCulture,DateTimeStyles.AdjustToUniversal, out value)) |
222 |
{ |
223 |
return value; |
224 |
} |
225 |
return null; |
226 |
} |
227 |
|
228 |
|
229 |
public static ObjectInfo Empty = new ObjectInfo |
230 |
{ |
231 |
Name = String.Empty, |
232 |
Hash = String.Empty, |
233 |
Bytes = 0, |
234 |
Content_Type = String.Empty, |
235 |
Last_Modified = DateTime.MinValue |
236 |
}; |
237 |
|
238 |
|
239 |
|
240 |
public string RelativeUrlToFilePath(string currentAccount) |
241 |
{ |
242 |
if (Name==null) |
243 |
throw new InvalidOperationException("Name can't be null"); |
244 |
if (String.IsNullOrWhiteSpace(currentAccount)) |
245 |
throw new ArgumentNullException("currentAccount"); |
246 |
Contract.EndContractBlock(); |
247 |
|
248 |
if (this == Empty) |
249 |
return String.Empty; |
250 |
|
251 |
var unescaped = Uri.UnescapeDataString(Name); |
252 |
var path = unescaped.Replace("/", "\\"); |
253 |
var pathParts=new Stack<string>(); |
254 |
pathParts.Push(path); |
255 |
if (!String.IsNullOrWhiteSpace(Container) && !_knownContainers.Contains(Container)) |
256 |
pathParts.Push(Container); |
257 |
if (!currentAccount.Equals(Account, StringComparison.InvariantCultureIgnoreCase)) |
258 |
{ |
259 |
if (Account != null) |
260 |
{ |
261 |
pathParts.Push(Account); |
262 |
pathParts.Push(FolderConstants.OthersFolder); |
263 |
} |
264 |
} |
265 |
var finalPath=Path.Combine(pathParts.ToArray()); |
266 |
return finalPath; |
267 |
} |
268 |
|
269 |
public override bool TrySetMember(SetMemberBinder binder, object value) |
270 |
{ |
271 |
if (binder.Name.StartsWith("x_object_meta")) |
272 |
{ |
273 |
Tags[binder.Name] = value.ToString(); |
274 |
} |
275 |
return false; |
276 |
} |
277 |
|
278 |
public string GetPermissionString() |
279 |
{ |
280 |
if (Permissions==null) |
281 |
throw new InvalidOperationException(); |
282 |
Contract.EndContractBlock(); |
283 |
|
284 |
var permissionBuilder = new StringBuilder(); |
285 |
var groupings = Permissions.GroupBy(pair => pair.Value, pair => pair.Key); |
286 |
foreach (var grouping in groupings) |
287 |
{ |
288 |
permissionBuilder.AppendFormat("{0}={1};", grouping.Key, String.Join(",", grouping)); |
289 |
} |
290 |
var permissions = permissionBuilder.ToString().Trim(';'); |
291 |
return permissions; |
292 |
} |
293 |
|
294 |
public void SetPermissions(string permissions) |
295 |
{ |
296 |
if (String.IsNullOrWhiteSpace(permissions)) |
297 |
return; |
298 |
|
299 |
var permDict=new Dictionary<string, string>(); |
300 |
var perms=permissions.Split(';'); |
301 |
foreach (var perm in perms) |
302 |
{ |
303 |
var permPairs=perm.Split('='); |
304 |
var right = permPairs[0]; |
305 |
var users= permPairs[1].Split(new[]{','},StringSplitOptions.RemoveEmptyEntries); |
306 |
foreach (var user in users) |
307 |
{ |
308 |
permDict[user] = right; |
309 |
} |
310 |
} |
311 |
Permissions = permDict; |
312 |
} |
313 |
} |
314 |
} |