public void AsyncCancel() { var cts = new CancellationTokenSource(); cts.Cancel(); var fileSource = new TorrentFileSource(typeof(TorrentCreatorTests).Assembly.Location); Assert.ThrowsAsync <OperationCanceledException> (() => creator.CreateAsync(fileSource, cts.Token)); }
private void button3_Click(object sender, EventArgs e) { //SemaphoreSlim sS = new SemaphoreSlim(10); //List<Task> tasks = new List<Task>(); if (checkBox2.Checked) { if (Directory.Exists(textBox1.Text)) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { button3.Enabled = false; DirectoryInfo dI = new DirectoryInfo(textBox1.Text); foreach (FileInfo f in dI.GetFiles("*.*", SearchOption.AllDirectories)) { TorrentCreator tc = InitTorrentCreator(); ITorrentFileSource tFS = new TorrentFileSource(f.FullName); if (((KeyValuePair<string, long>)comboBox1.SelectedItem).Value == -1) { tc.PieceLength = CalculateOptimumPieceSize(textBox1.Text); } string a = Path.Combine(folderBrowserDialog1.SelectedPath, Path.GetFileNameWithoutExtension(f.FullName) + ".torrent"); tc.CreateAsync(tFS, new FileStream(Path.Combine(folderBrowserDialog1.SelectedPath, Path.GetFileNameWithoutExtension(f.FullName) + ".torrent"), FileMode.Create)).ContinueWith((t) => TorrentCompleted(true)); } button3.Enabled = true; if (MessageBox.Show("The torrents have been created successfully.", "Torrents created", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK) { progressBar1.Value = 0; } } } else if (File.Exists(textBox1.Text)) { MessageBox.Show("The file source must be a directory whilst in batch mode.", "Invalid file source specified", MessageBoxButtons.OK, MessageBoxIcon.Stop); } else { MessageBox.Show("Please enter a file source.", "No file source specified", MessageBoxButtons.OK, MessageBoxIcon.Stop); } } else { if (File.Exists(textBox1.Text) || Directory.Exists(textBox1.Text)) { saveFileDialog1.FileName = Path.GetFileNameWithoutExtension(textBox1.Text) + ".torrent"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { button3.Enabled = false; TorrentCreator tc = InitTorrentCreator(); ITorrentFileSource tFS = new TorrentFileSource(textBox1.Text); if (((KeyValuePair<string, long>)comboBox1.SelectedItem).Value == -1) { tc.PieceLength = CalculateOptimumPieceSize(textBox1.Text); } tc.CreateAsync(tFS, new FileStream(saveFileDialog1.FileName, FileMode.Create)).ContinueWith((t) => TorrentCompleted(false)); } } else { MessageBox.Show("Please enter a file source.", "No file source specified", MessageBoxButtons.OK, MessageBoxIcon.Stop); } } }
private void createButtonClicked(object sender, RoutedEventArgs e) { var sourcePath = pathTextBox.Text; if (string.IsNullOrEmpty(sourcePath)) { MessageBox.Show("Please select a file or files to add."); return; } if (singleFileRadioButton.IsChecked.Value) { if (!File.Exists(sourcePath)) { MessageBox.Show("The selected file does not exist!"); return; } } if (entireFolderRadioButton.IsChecked.Value) { if (!Directory.Exists(sourcePath)) { MessageBox.Show("The selected folder does not exist!"); return; } } Creator = new TorrentCreator(); var source = new TorrentFileSource(sourcePath, ignoreHiddenFilesCheckBox.IsChecked.Value); var tier = new RawTrackerTier(trackerListBox.Items.Cast<string>()); Creator.Announces.Add(tier); Creator.Comment = commentTextBox.Text; Creator.Private = privateTorrentCheckBox.IsChecked.Value; Creator.CreatedBy = "Patchy BitTorrent Client"; Creator.PieceLength = TorrentCreator.RecommendedPieceSize(source.Files); var dialog = new SaveFileDialog(); dialog.Filter = "Torrent Files (*.torrent)|*.torrent|All Files (*.*)|*.*"; dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); FilePath = sourcePath; if (dialog.ShowDialog().Value) { // Create the torrent Path = dialog.FileName; if (!Path.EndsWith(".torrent")) Path += ".torrent"; pathGrid.IsEnabled = trackerGrid.IsEnabled = optionsGrid.IsEnabled = createButton.IsEnabled = false; Creator.Hashed += Creator_Hashed; Creator.BeginCreate(source, CreationComplete, null); } }
public Stream Create(string name, string sourceDirectoryPath, IEnumerable<string> trackers = null) { // Check sourceDirectoryPath is a directory. if (!Directory.Exists (sourceDirectoryPath)) throw new InvalidSourceDirectoryException ("Was not found or is not a directory."); // Create torrent file mappings. var fileMappings = new TorrentFileSource (sourceDirectoryPath, true).Files.Select (fm => { var info = new FileInfo (fm.Source); return new TorrentFile (fm.Destination, info.Length, fm.Source); }).ToList(); // Make creator. var creator = new TorrentCreator (); creator.PieceLength = TorrentCreator.RecommendedPieceSize (fileMappings); if (trackers != null) creator.Announces.Add (new RawTrackerTier(trackers)); // Make torrent, convert to stream and return. var torrentStream = new MemoryStream (); var torrentRAW = creator.Create (name, fileMappings).Encode (); torrentStream.Write(torrentRAW, 0, torrentRAW.Length); return torrentStream; }