async Task SummarizeTorrent(FileInfo torrent) { var content = await File.ReadAllBytesAsync(torrent.FullName); if (!BEnc.TryParseExpr(content, out var expr, out int read) || read != content.Length || expr == null) { Console.WriteLine($"Something went wrong reading {torrent.FullName} at offset {read}"); return; } var fileInfo = MetaInfoSerializer.Deserialize <TorrentFileDto>(expr); byte[] infoHash = expr["info"] !.Hash(); Console.WriteLine($"Summary for '{fileInfo.Info.Name}'"); Console.WriteLine($"InfoHash: {Convert.ToHexString(infoHash)}"); var files = fileInfo.Info.Files; if (files != null) { var maxNameLen = files.Select(f => string.Join('/', f.Path)).Max(name => name.Length); foreach (var file in files) { var path = string.Join('/', file.Path); Console.WriteLine($"{path.PadRight(maxNameLen + 2)} {Utils.FormatBytesize(file.Length)}"); } } Console.WriteLine($"Trackers:\n{string.Join("\n", fileInfo.AnnounceList.Select(t => string.Join(", ", t)))}"); }
public async Task SampleTorrentFileRoundtripTest() { var content = await File.ReadAllBytesAsync(SAMPLE_TORRENT); Assert.IsTrue(BEnc.TryParseExpr(content.AsSpan(), out var value, out var consumed)); Assert.AreEqual(content.Length, consumed); Assert.IsNotNull(value); Console.WriteLine(value !.ToString()); var recoded = BEnc.EncodeBuffer(value); Assert.AreEqual(content, recoded); }
protected static async Task <(TorrentFileDto, BEnc)> GetTorrentFileDtoAsync(FileInfo torrent) { var content = await File.ReadAllBytesAsync(torrent.FullName); if (!BEnc.TryParseExpr(content, out var expr, out var read) || expr == null || read != content.Length) { throw new ArgumentOutOfRangeException(nameof(torrent)); } var dto = MetaInfoSerializer.Deserialize <TorrentFileDto>(expr); return(dto, expr); }
public async Task DeserializeToDtoTest() { var content = await File.ReadAllBytesAsync(SAMPLE_TORRENT); Assert.IsTrue(BEnc.TryParseExpr(content.AsSpan(), out var value, out var consumed)); Assert.AreEqual(content.Length, consumed); Assert.IsNotNull(value); var file = MetaInfoSerializer.Deserialize <TorrentFileDto>(value !); Assert.IsNotNull(file); var info = file.Info; Assert.IsNotNull(info); Assert.IsNotNull(info.Files); Assert.IsNotEmpty(info.Files); Assert.IsNotNull(info.Files ![0].Path);