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); }
private void ProcessDirs() { this.torrentsToUpload = new List <string>(); foreach (var dir in this.directories) { var dirName = Helper.ToAlphaNumberc(Helper.GetFolderName(dir)); var torrentFileName = dirName + ".torrent"; var torrentPath = Path.Combine(this.destinationDir, torrentFileName); this.io.Write("Creating torrent: " + torrentFileName, Colors.Default, false); ITorrentFileSource fileSource = new TorrentFileSource(dir); this.torrentCreator.Create(fileSource, torrentPath); this.torrentsToUpload.Add(torrentFileName); this.io.Write(" Done!", Colors.Green); } // Add torrents to the local client foreach (var torrent in this.torrentsToUpload) { this.StartTorrent(torrent); } }
public static byte[] GetBytesFromFile(string PathToFile) { TorrentCreator TC = new TorrentCreator(); TC.Private = false; ITorrentFileSource filePath = new TorrentFileSource(PathToFile); BEncodedDictionary bed = TC.Create(filePath); return(bed.Encode()); }
public static Torrent GetTorrentFromFile(string PathToFile) { TorrentCreator TC = new TorrentCreator(); TC.Private = false; ITorrentFileSource filePath = new TorrentFileSource(PathToFile); BEncodedDictionary bed = TC.Create(filePath); return(Torrent.Load(bed)); }
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); } }
private const int PieceLength = 16384;//64 * 1024; public ITorrent CreateTorrent(string downloadFolder, string relativeFileSourcePath, string destinationFolder) { var creator = new MonoTorrent.Common.TorrentCreator(); var config = Catalog.Factory.Resolve <IConfig>(); creator.Announces.Clear(); // Add one tier which contains a tracker var tier = new RawTrackerTier { string.Format( "http://{0}:{1}/announce", config[BitTorrentSettings.TrackerHost], config[BitTorrentSettings.TrackerPort]) }; creator.Announces.Add(tier); creator.Announce = string.Format( "http://{0}:{1}/announce", config[BitTorrentSettings.TrackerHost], config[BitTorrentSettings.TrackerPort]); creator.CreatedBy = "Monotorrent Client/" + VersionInfo.ClientVersion; creator.Comment = downloadFolder; creator.Publisher = relativeFileSourcePath; creator.PieceLength = PieceLength; creator.PublisherUrl = string.Empty; //not allowing dht, peer exchange creator.Private = true; creator.Hashed += (o, e) => Console.WriteLine("{0} {1}", e.FileSize, e.CurrentFile.First()); var fullSourcePath = Path.Combine(downloadFolder, relativeFileSourcePath); var fileSource = new TorrentFileSource(fullSourcePath); //var randomName = Path.GetTempFileName(); var destFile = Path.Combine(destinationFolder, Path.GetFileNameWithoutExtension(fullSourcePath) + ".torrent"); creator.Create(fileSource, destFile); var torrentFile = new TorrentFile(destFile, relativeFileSourcePath); return(torrentFile); }
//------------------------------------------------------------- public async Task <byte[]> CreateTorrent(string path, string title) //------------------------------------------------------------- { byte[] result = null; string pathTorrent = path + "\\_kobberlan.torrent"; //------------------------------------------------------------- //Check if torrent file exist //------------------------------------------------------------- if (File.Exists(pathTorrent)) { Log.Get().Write("Load torrent from path: " + pathTorrent); try { int fileLength = (int)new System.IO.FileInfo(pathTorrent).Length; result = new byte[fileLength]; FileStream fileStream = File.Open(pathTorrent, FileMode.Open); fileStream.Read(result, 0, fileLength); fileStream.Close(); } catch (Exception ex) { Log.Get().Write("Failed to load torrent file: " + ex, Log.LogType.Error); } } else { //------------------------------------------------------------- //Create new torrent file //------------------------------------------------------------- Log.Get().Write("Creating torrent file for path: " + path); TorrentCreator c = new TorrentCreator(); c.CreatedBy = "Lan"; c.Publisher = "Lan"; c.Comment = title; c.Private = true; //Do not share the torrent with other tracker. (Disable DHT or peer exchange) //------------------------------------------------------------- // Every time a piece has been hashed, this event will fire. //------------------------------------------------------------- c.Hashed += HashedDelegate; //------------------------------------------------------------- //Path to directory //------------------------------------------------------------- ITorrentFileSource fileSource = new TorrentFileSource(path); //------------------------------------------------------------- //Create torrent //------------------------------------------------------------- BEncodedDictionary dict = await c.CreateAsync(fileSource); result = dict.Encode(); Log.Get().Write("Creating torrent file in memory. Size: " + result.Length); //------------------------------------------------------------- //Save torrent to HDD (To increase loadtime next time) //------------------------------------------------------------- string createTorrentFile = ConfigurationManager.AppSettings.Get("Torrent:CreateTorrentFiles"); if (Convert.ToBoolean(createTorrentFile)) { Log.Get().Write("Saving torrent file to path: " + pathTorrent); try { FileStream fileStream = File.Create(pathTorrent); fileStream.Write(result, 0, result.Length); fileStream.Close(); } catch (Exception ex) { Log.Get().Write("Failed to save torrent file: " + ex, Log.LogType.Error); } } } return(result); }