Skip to content

Commit ca6eafa

Browse files
committed
Added setters for TorrentFile properties
1 parent 1ce8cbe commit ca6eafa

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

BencodeNET.Net45/Objects/TorrentFile.cs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,88 @@ namespace BencodeNET.Objects
88
{
99
public class TorrentFile
1010
{
11+
private readonly DateTime _epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
12+
1113
private readonly BDictionary _data = new BDictionary();
1214

1315
public BDictionary Info
1416
{
15-
get { return (BDictionary) _data["info"]; }
17+
get { return (BDictionary) _data[Fields.Info]; }
18+
set { _data[Fields.Info] = value; }
1619
}
1720

1821
public string Announce
1922
{
2023
get
2124
{
22-
if (!_data.ContainsKey("announce"))
25+
if (!_data.ContainsKey(Fields.Announce))
2326
return null;
24-
return _data["announce"].ToString();
27+
return _data[Fields.Announce].ToString();
2528
}
29+
set { _data[Fields.Announce] = new BString(value); }
2630
}
2731

2832
public BList AnnounceList
2933
{
30-
get { return (BList) _data["announce-list"]; }
34+
get { return (BList) _data[Fields.AnnounceList]; }
35+
set { _data[Fields.AnnounceList] = value; }
3136
}
3237

3338
public DateTime CreationDate
3439
{
3540
get
3641
{
37-
var unixTime = (BNumber) _data["creation date"];
38-
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
39-
return epoch.AddSeconds(unixTime);
42+
var unixTime = (BNumber) _data[Fields.CreationDate];
43+
return _epoch.AddSeconds(unixTime);
44+
}
45+
set
46+
{
47+
var unixTime = value.Subtract(_epoch).TotalSeconds.ToString();
48+
_data[Fields.CreationDate] = new BString(unixTime);
4049
}
4150
}
4251

4352
public string Comment
4453
{
4554
get
4655
{
47-
if (!_data.ContainsKey("comment"))
56+
if (!_data.ContainsKey(Fields.Comment))
4857
return null;
49-
return _data["comment"].ToString();
58+
return _data[Fields.Comment].ToString();
5059
}
60+
set { _data[Fields.Comment] = new BString(value); }
5161
}
5262

5363
public string CreatedBy
5464
{
5565
get
5666
{
57-
if (!_data.ContainsKey("created by"))
67+
if (!_data.ContainsKey(Fields.CreatedBy))
5868
return null;
59-
return _data["created by"].ToString();
69+
return _data[Fields.CreatedBy].ToString();
6070
}
71+
set { _data[Fields.CreatedBy] = new BString(value); }
6172
}
6273

6374
public string Encoding
6475
{
6576
get
6677
{
67-
if (!_data.ContainsKey("encoding"))
78+
if (!_data.ContainsKey(Fields.Encoding))
6879
return null;
69-
return _data["encoding"].ToString();
80+
return _data[Fields.Encoding].ToString();
7081
}
82+
set { _data[Fields.Encoding] = new BString(value); }
7183
}
7284

7385
public string CalculateInfoHash()
7486
{
75-
return CalculateInfoHash((BDictionary)_data["info"]);
87+
return CalculateInfoHash(Info);
7688
}
7789

7890
public byte[] CalculateInfoHashBytes()
7991
{
80-
return CalculateInfoHashBytes((BDictionary)_data["info"]);
92+
return CalculateInfoHashBytes(Info);
8193
}
8294

8395
public static string CalculateInfoHash(BDictionary info)
@@ -102,6 +114,7 @@ public static byte[] CalculateInfoHashBytes(BDictionary info)
102114
public IBObject this[BString key]
103115
{
104116
get { return _data[key]; }
117+
set { _data[key] = value; }
105118
}
106119

107120
public static bool operator ==(TorrentFile first, TorrentFile second)
@@ -150,5 +163,16 @@ public TorrentFile(BDictionary torrent)
150163
{
151164
_data = torrent;
152165
}
166+
167+
private static class Fields
168+
{
169+
public const string Announce = "announce";
170+
public const string AnnounceList = "announce-list";
171+
public const string CreatedBy = "created by";
172+
public const string CreationDate = "creation date";
173+
public const string Comment = "comment";
174+
public const string Encoding = "encoding";
175+
public const string Info = "info";
176+
}
153177
}
154178
}

0 commit comments

Comments
 (0)