private async Task <List <CreateTorrentFileMapping> > CreateTorrentFilesAsync( StageTorrentDto torrentDto, string tempDir) { var torrentFileMappings = new List <CreateTorrentFileMapping>(); var torrentFiles = new[] { new { FilePath = torrentDto.TorrentFile1Path, SizeInBytes = torrentDto.TorrentFile1SizeInKB * 1024 }, new { FilePath = torrentDto.TorrentFile2Path, SizeInBytes = torrentDto.TorrentFile2SizeInKB * 1024 } } .Where(t => !string.IsNullOrWhiteSpace(t.FilePath)) .ToArray(); var torrentFileHelper = DI.Get <TorrentFileHelper>(); foreach (var torrentFile in torrentFiles) { var subDirs = Path.GetDirectoryName(torrentFile.FilePath); if (!string.IsNullOrWhiteSpace(subDirs)) { Directory.CreateDirectory(Path.Combine(tempDir, subDirs)); } var torrentFilePath = Path.Combine(tempDir, torrentFile.FilePath); await torrentFileHelper.CreateTextFileAsync(torrentFilePath, (int)torrentFile.SizeInBytes); torrentFileMappings.Add(new CreateTorrentFileMapping { FileLocOnDisk = torrentFilePath, FileLocInTorrent = torrentFile.FilePath.Replace('/', '\\') }); } return(torrentFileMappings); }
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; }