Uploading and downloading with hashes
[pithos-ms-client] / trunk / Pithos.Interfaces / ObjectInfo.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Globalization;
4 using System.IO;
5
6 namespace Pithos.Interfaces
7 {
8     public class ObjectInfo
9     {
10         public string Name { get; set; }
11         public string Hash { get; set; }
12         public long Bytes { get; set; }
13         public string Content_Type { get; set; }
14         public DateTime Last_Modified { get; set; }
15
16         private Dictionary<string, string> _tags=new Dictionary<string, string>();
17         public Dictionary<string, string> Tags
18         {
19             get { return _tags; }
20             set { _tags = value; }
21         }
22
23         private Dictionary<string, string> _extensions=new Dictionary<string, string>();
24         public Dictionary<string, string> Extensions
25         {
26             get { return _extensions; }
27             set
28             {
29                 _extensions = value;
30                 ExtractKnownExtensions();
31             }
32         }
33
34         private long? _version;
35         public long? Version
36         {
37             get { return _version; }
38             set { _version = value; }
39         }
40         
41         //Alias for version, for Json deserialization purposes
42         public long? X_Object_Version
43         {
44             get { return _version; }
45             set { _version = value; }
46         }
47         
48         //Alias for VersionTimestamp, for Json deserialization purposes
49         //The x_object_version_timestamp returned by GET on a container is
50         //a float, probably due to a bug.
51         public double? X_Object_Version_Timestamp
52         {
53             get
54             {
55                 if (_versionTimestamp.HasValue)
56                     return (_versionTimestamp.Value-_epoch).TotalSeconds;
57                 return null;
58             }
59             set
60             {                
61                 if (value.HasValue)
62                 {
63                     _versionTimestamp = _epoch.AddSeconds(value.Value);
64                 }
65                 else
66                 {
67                     _versionTimestamp = null;
68                 }
69             }
70         }
71
72         private DateTime? _versionTimestamp;
73         public DateTime? VersionTimestamp
74         {
75             get { return _versionTimestamp; }
76             set { _versionTimestamp = value; }
77         }
78
79         public string ModifiedBy
80         {
81             get{ return _modifiedBy;  }
82             set{ _modifiedBy = value; }
83         }
84
85         //Alias for ModifiedBy, for Json deserialization purposes
86         public string X_Object_Modified_By
87         {
88             get{ return _modifiedBy;  }
89             set{ _modifiedBy = value; }
90         }
91
92         public Stream Stream { get; set; }
93
94
95         private void ExtractKnownExtensions()
96         {
97             Version=GetLong(KnownExtensions.X_Object_Version);
98             VersionTimestamp = GetTimestamp(KnownExtensions.X_Object_Version_Timestamp);
99             ModifiedBy = GetString(KnownExtensions.X_Object_Modified_By);
100         }
101
102         private string GetString(string name)
103         {            
104             var value=String.Empty;
105             _extensions.TryGetValue(name, out value);
106             return value ;                        
107         }
108
109         private long? GetLong(string name)
110         {
111             string version;
112             long value;
113             return _extensions.TryGetValue(name, out version) && long.TryParse(version, out value)
114                        ? (long?) value
115                        : null;
116         }
117
118         private DateTime? GetTimestamp(string name)
119         {
120             string version;
121             DateTime value;
122             if (_extensions.TryGetValue(name, out version) && 
123                 DateTime.TryParse(version,CultureInfo.InvariantCulture,DateTimeStyles.AdjustToUniversal, out value))
124             {
125                 return value;
126             }
127             return null;
128         }
129
130
131         public static ObjectInfo Empty = new ObjectInfo
132         {
133             Name = String.Empty,
134             Hash = String.Empty,
135             Bytes = 0,
136             Content_Type = String.Empty,
137             Last_Modified = DateTime.MinValue
138         };
139
140         private string _modifiedBy;
141         private DateTime _epoch = new DateTime(1970, 1, 1);
142     }
143 }