Skip to content

Commit b058b78

Browse files
committed
Add DisplayNameUtf8 and DirectoryNameUtf8
#47
1 parent 3b1b05a commit b058b78

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

BencodeNET.Tests/Torrents/TorrentTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,49 @@ public void DisplayName_UnknownFileMode_ThrowsBencodeException()
9292
act.Should().Throw<BencodeException>();
9393
}
9494

95+
[Theory]
96+
[AutoMockedData]
97+
public void DisplayNameUtf8_SingleFile_IsFileName(string fileName)
98+
{
99+
var torrent = new Torrent
100+
{
101+
File = new SingleFileInfo
102+
{
103+
FileNameUtf8 = fileName
104+
}
105+
};
106+
107+
torrent.FileMode.Should().Be(TorrentFileMode.Single);
108+
torrent.DisplayNameUtf8.Should().Be(fileName);
109+
}
110+
111+
[Theory]
112+
[AutoMockedData]
113+
public void DisplayNameUtf8_MultiFile_IsDirectoryName(string directoryNameUtf8)
114+
{
115+
var torrent = new Torrent
116+
{
117+
Files = new MultiFileInfoList(default, directoryNameUtf8)
118+
{
119+
new MultiFileInfo()
120+
}
121+
};
122+
123+
torrent.FileMode.Should().Be(TorrentFileMode.Multi);
124+
torrent.DisplayNameUtf8.Should().Be(directoryNameUtf8);
125+
}
126+
127+
[Fact]
128+
public void DisplayNameUtf8_UnknownFileMode_ThrowsBencodeException()
129+
{
130+
var torrent = new Torrent();
131+
132+
Func<string> act = () => torrent.DisplayNameUtf8;
133+
134+
torrent.FileMode.Should().Be(TorrentFileMode.Unknown);
135+
act.Should().Throw<BencodeException>();
136+
}
137+
95138
[Fact]
96139
public void PiecesAsHexString_Get_ReturnsHexUppercaseWithoutDashes()
97140
{

BencodeNET/Torrents/MultiFileInfoList.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,29 @@ public MultiFileInfoList(string directoryName)
2121
DirectoryName = directoryName;
2222
}
2323

24+
/// <summary></summary>
25+
/// <param name="directoryName">Name of directory to store files in.</param>
26+
/// <param name="directoryNameUtf8">Name of directory to store files in.</param>
27+
public MultiFileInfoList(string directoryName, string directoryNameUtf8)
28+
{
29+
DirectoryName = directoryName;
30+
DirectoryNameUtf8 = directoryNameUtf8;
31+
}
32+
2433
/// <summary>
2534
/// The name of the directory in which to store all the files. This is purely advisory.
2635
/// </summary>
2736
/// <remarks>
2837
/// Corresponds to the 'name' field.
2938
/// </remarks>
3039
public string DirectoryName { get; set; }
40+
41+
/// <summary>
42+
/// The UTF-8 encoded name of the directory in which to store all the files. This is purely advisory.
43+
/// </summary>
44+
/// <remarks>
45+
/// Corresponds to the 'name.utf-8' field.
46+
/// </remarks>
47+
public string DirectoryNameUtf8 { get; set; }
3148
}
3249
}

BencodeNET/Torrents/Torrent.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ public virtual string DisplayName
133133
}
134134
}
135135

136+
public virtual string DisplayNameUtf8
137+
{
138+
get
139+
{
140+
if (FileMode == TorrentFileMode.Single)
141+
return File.FileNameUtf8;
142+
143+
if (FileMode == TorrentFileMode.Multi)
144+
return Files.DirectoryNameUtf8;
145+
146+
throw new BencodeException("Cannot get torrent display name. Unknown torrent file mode.");
147+
}
148+
}
149+
136150
/// <summary>
137151
/// [optional] The creation date of the torrent.
138152
/// </summary>

BencodeNET/Torrents/TorrentParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ protected virtual MultiFileInfoList ParseMultiFileInfo(BDictionary info, Encodin
237237
var list = new MultiFileInfoList
238238
{
239239
DirectoryName = info.Get<BString>(TorrentInfoFields.Name).ToString(encoding),
240+
DirectoryNameUtf8 = info.Get<BString>(TorrentInfoFields.NameUtf8).ToString(encoding)
240241
};
241242

242243
var fileInfos = info.Get<BList>(TorrentInfoFields.Files).Cast<BDictionary>()

0 commit comments

Comments
 (0)