Exemplo n.º 1
0
        private async Task CreateAndAddTorrentAsync(StageTorrentDto torrentDto)
        {
            var tempDir         = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
            var torrentFilePath = Path.Combine(tempDir, torrentDto.Name + ".torrent");
            var torrentDataDir  = Path.Combine(tempDir, torrentDto.Name);

            Directory.CreateDirectory(torrentDataDir);

            var torrentFileMappings = await CreateTorrentFilesAsync(torrentDto, torrentDataDir).ConfigureAwait(false);

            var newTorrentFile = new NewTorrentFile
            {
                Name = torrentDto.Name,
                TrackerAnnounceUrls = new string[] { torrentDto.TrackerAnnounceUrl1, torrentDto.TrackerAnnounceUrl2 }
                .Where(s => !string.IsNullOrWhiteSpace(s))
                .ToList(),
                FileMappings = torrentFileMappings
            };

            await _torrentFileHelper.CreateTorrentAsync(torrentFilePath, newTorrentFile);

            await _torrentClient.AddTorrentAsync(torrentDto.Name, torrentFilePath, torrentDto.Location, newTorrentFile.FileMappings.Count()).ConfigureAwait(false);

            DI.Get <Dictionary <string, string> >("TorrentDataFolders")[torrentDto.Name] = torrentDataDir;
        }
        private static async Task CreateTorrentAsyncTestBodyAsync(TorrentFileHelper sut, string torrentFileLoc, NewTorrentFile newTorrentFile)
        {
            //Act
            await sut.CreateTorrentAsync(torrentFileLoc, newTorrentFile);

            //Assert
            File.Exists(torrentFileLoc).Should().BeTrue();
            var torrentFile = await sut.ReadTorrentAsync(torrentFileLoc);

            torrentFile.Name.Should().Be(newTorrentFile.Name);
            torrentFile.IsPrivate.Should().Be(newTorrentFile.IsPrivate);
            torrentFile.TrackerAnnounceUrl.Should().Be(newTorrentFile.TrackerAnnounceUrls.SingleOrDefault());
            torrentFile.InnerTorrentFiles.Count().Should().Be(newTorrentFile.FileMappings.Count());

            foreach (var fileMapping in newTorrentFile.FileMappings)
            {
                var innerTorrentFile  = torrentFile.InnerTorrentFiles.Single(f => f.FileLocInTorrent == fileMapping.FileLocInTorrent);
                var actualSizeInbytes = new FileInfo(fileMapping.FileLocOnDisk).Length;
                innerTorrentFile.FileSizeInBytes.Should().Be(actualSizeInbytes);
            }
        }