void multiFileTorrent(BDictionary info, TorrentCreationViewModel vm, List <FileInfo> files) { BList filesList = new BList(); foreach (FileInfo file in files) { BDictionary fileDictionary = new BDictionary(); fileDictionary["length"] = new BInteger(file.Length); BList pathList = new BList(); String relativePath = file.FullName.Remove(0, vm.PathRoot.Length); foreach (String elem in relativePath.Split(new char[] { '\\' })) { if (!String.IsNullOrEmpty(elem)) { pathList.Add(elem); } } fileDictionary["path"] = pathList; filesList.Add(fileDictionary); } info["name"] = new BString(vm.TorrentName); info["files"] = filesList; }
public async Task createTorrentAsync(TorrentCreationViewModel vm) { await Task.Run(() => { try { createTorrent(vm); App.Current.Dispatcher.BeginInvoke(new Action(() => { MiscUtils.insertIntoHistoryCollection(Settings.Default.TorrentAnnounceHistory, vm.AnnounceURL); })); } catch (Exception e) { MessageBox.Show("Error creating torrent file\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); Logger.Log.Error("Error creating torrent file", e); } App.Current.Dispatcher.Invoke(() => { OkCommand.IsExecutable = true; CancelCommand.IsExecutable = false; }); }); }
public TorrentCreationView() { InitializeComponent(); DataContext = ViewModel = new TorrentCreationViewModel(); ViewModel.ClosingRequest += ViewModel_ClosingRequest; }
void createTorrent(TorrentCreationViewModel vm) { /* * info: a dictionary that describes the file(s) of the torrent. There are two possible forms: one for the case of a 'single-file' torrent with no directory structure, and one for the case of a 'multi-file' torrent (see below for details) * announce: The announce URL of the tracker (string) * announce-list: (optional) this is an extention to the official specification, offering backwards-compatibility. (list of lists of strings). * The official request for a specification change is here. * creation date: (optional) the creation time of the torrent, in standard UNIX epoch format (integer, seconds since 1-Jan-1970 00:00:00 UTC) * comment: (optional) free-form textual comments of the author (string) * created by: (optional) name and version of the program used to create the .torrent (string) * encoding: (optional) the string encoding format used to generate the pieces part of the info dictionary in the .torrent metafile (string) */ BDictionary info = new BDictionary(); info["pieces"] = new BString(buildPiecesHash(vm.Media)); info["piece length"] = new BInteger(pieceLength); info["private"] = new BInteger(vm.IsPrivate ? 1 : 0); if (vm.Media.Count == 1) { singleFileTorrent(info, new FileInfo(vm.Media.ElementAt(0).Location)); } else { List <FileInfo> fileInfo = new List <FileInfo>(); foreach (MediaFileItem item in vm.Media) { fileInfo.Add(new FileInfo(item.Location)); } multiFileTorrent(info, vm, fileInfo); } BDictionary metaInfo = new BDictionary(); metaInfo["info"] = info; metaInfo["announce"] = new BString(vm.AnnounceURL); metaInfo["creation date"] = new BInteger((long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds); metaInfo["created by"] = new BString(createdBy); if (!String.IsNullOrEmpty(vm.Comment) && !String.IsNullOrWhiteSpace(vm.Comment) && vm.IsCommentEnabled) { metaInfo["comment"] = new BString(vm.Comment); } if (!String.IsNullOrEmpty(encoding) && !String.IsNullOrWhiteSpace(encoding) && vm.IsCommentEnabled) { metaInfo["encoding"] = new BString(encoding); } String torrentName = String.IsNullOrEmpty(vm.TorrentName) ? Path.GetFileNameWithoutExtension(vm.Media.ElementAt(0).Location) : vm.TorrentName; String torrentFullName = vm.OutputPath + "\\" + torrentName + ".torrent"; if (CancellationToken.IsCancellationRequested) { return; } FileStream outputTorrent = null; try { outputTorrent = new FileStream(torrentFullName, FileMode.CreateNew); BinaryWriter bw = new BinaryWriter(outputTorrent); foreach (char c in metaInfo.ToBencodedString()) { bw.Write((byte)c); } InfoMessages.Add("Created torrent file: " + torrentFullName); } finally { if (outputTorrent != null) { outputTorrent.Close(); } } }
void createTorrent(TorrentCreationViewModel vm) { /* info: a dictionary that describes the file(s) of the torrent. There are two possible forms: one for the case of a 'single-file' torrent with no directory structure, and one for the case of a 'multi-file' torrent (see below for details) announce: The announce URL of the tracker (string) announce-list: (optional) this is an extention to the official specification, offering backwards-compatibility. (list of lists of strings). The official request for a specification change is here. creation date: (optional) the creation time of the torrent, in standard UNIX epoch format (integer, seconds since 1-Jan-1970 00:00:00 UTC) comment: (optional) free-form textual comments of the author (string) created by: (optional) name and version of the program used to create the .torrent (string) encoding: (optional) the string encoding format used to generate the pieces part of the info dictionary in the .torrent metafile (string) */ BDictionary info = new BDictionary(); info["pieces"] = new BString(buildPiecesHash(vm.Media)); info["piece length"] = new BInteger(pieceLength); info["private"] = new BInteger(vm.IsPrivate ? 1 : 0); if (vm.Media.Count == 1) { singleFileTorrent(info, new FileInfo(vm.Media.ElementAt(0).Location)); } else { List<FileInfo> fileInfo = new List<FileInfo>(); foreach (MediaFileItem item in vm.Media) { fileInfo.Add(new FileInfo(item.Location)); } multiFileTorrent(info, vm, fileInfo); } BDictionary metaInfo = new BDictionary(); metaInfo["info"] = info; metaInfo["announce"] = new BString(vm.AnnounceURL); metaInfo["creation date"] = new BInteger((long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds); metaInfo["created by"] = new BString(createdBy); if (!String.IsNullOrEmpty(vm.Comment) && !String.IsNullOrWhiteSpace(vm.Comment) && vm.IsCommentEnabled) { metaInfo["comment"] = new BString(vm.Comment); } if (!String.IsNullOrEmpty(encoding) && !String.IsNullOrWhiteSpace(encoding) && vm.IsCommentEnabled) { metaInfo["encoding"] = new BString(encoding); } String torrentName = String.IsNullOrEmpty(vm.TorrentName) ? Path.GetFileNameWithoutExtension(vm.Media.ElementAt(0).Location) : vm.TorrentName; String torrentFullName = vm.OutputPath + "\\" + torrentName + ".torrent"; if (CancellationToken.IsCancellationRequested) return; FileStream outputTorrent = null; try { outputTorrent = new FileStream(torrentFullName, FileMode.CreateNew); BinaryWriter bw = new BinaryWriter(outputTorrent); foreach (char c in metaInfo.ToBencodedString()) { bw.Write((byte)c); } InfoMessages.Add("Created torrent file: " + torrentFullName); } finally { if (outputTorrent != null) { outputTorrent.Close(); } } }
void multiFileTorrent(BDictionary info, TorrentCreationViewModel vm, List<FileInfo> files) { BList filesList = new BList(); foreach (FileInfo file in files) { BDictionary fileDictionary = new BDictionary(); fileDictionary["length"] = new BInteger(file.Length); BList pathList = new BList(); String relativePath = file.FullName.Remove(0, vm.PathRoot.Length); foreach (String elem in relativePath.Split(new char[] { '\\' })) { if (!String.IsNullOrEmpty(elem)) { pathList.Add(elem); } } fileDictionary["path"] = pathList; filesList.Add(fileDictionary); } info["name"] = new BString(vm.TorrentName); info["files"] = filesList; }