示例#1
0
        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)))}");
        }
示例#2
0
        public void BinaryOutputTest()
        {
            var testValue = "testerino";
            var encoded   = new BStr(testValue);

            var got = BEnc.EncodeBuffer(encoded);

            Assert.AreEqual((byte)($"{testValue.Length}"[0]), got[0]);
            Assert.AreEqual(':', got[1]);
            Assert.AreEqual(Encoding.UTF8.GetBytes(testValue), got[2..]);
示例#3
0
        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);
        }
示例#4
0
            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);
            }
示例#5
0
        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);