public override int Run(string[] remainingArguments) { var sourceDirectoryPath = Path.GetFullPath(SourceDirectory); var torrentFilePath = Path.GetFullPath(Name + ".torrent"); var outputStream = Client.Post <Stream> ("/api/torrents/create", new TorrentCreateRequest { Name = Name, SourceDirectoryPath = sourceDirectoryPath, Trackers = Trackers }); using (var file = File.OpenWrite(torrentFilePath)) StreamHelpers.CopyStream(outputStream, file); if (Add) { new Add(EnvironmentDetails, Client) { OuputDirectoryPath = sourceDirectoryPath, TorrentPath = torrentFilePath, Mirror = false, Wait = false }.Run(new string[] {}); } return(0); }
/// <summary> /// Parses a subtitle file stream. /// We try all the parsers registered in the _subFormatToParser dictionary /// </summary> /// <param name="stream">The subtitle file stream</param> /// <param name="encoding">The stream encoding</param> /// <param name="subFormatDictionary">The dictionary of the subtitles parser (ordered) to try</param> /// <returns>The corresponding list of SubtitleItem, null if parsing failed</returns> public List <SubtitleItem> ParseStream(Stream stream, Encoding encoding, Dictionary <SubtitlesFormat, ISubtitlesParser> subFormatDictionary) { // test if stream if readable if (!stream.CanRead) { throw new ArgumentException("Cannot parse a non-readable stream"); } // copy the stream if not seekable var seekableStream = stream; if (!stream.CanSeek) { seekableStream = StreamHelpers.CopyStream(stream); seekableStream.Seek(0, SeekOrigin.Begin); } // if dictionary is null, use the default one subFormatDictionary = subFormatDictionary ?? _subFormatToParser; foreach (var subtitlesParser in subFormatDictionary) { try { var parser = subtitlesParser.Value; var items = parser.ParseStream(seekableStream, encoding); // safeguard if (items.Any()) { return(items); } else { throw new FormatException(string.Format("Failed to parse as {0}", subtitlesParser.Key)); } } catch (Exception ex) { //Console.WriteLine(ex); } } // all the parsers failed var firstCharsOfFile = LogFirstCharactersOfStream(stream, 500, encoding); var message = string.Format("All the subtitles parsers failed to parse the following stream:{0}", firstCharsOfFile); throw new ArgumentException(message); }
public SubZir ParseStream(Stream stream, Encoding encoding, Dictionary <SubtitlesFormat, ISubtitlesParser> subFormatDictionary) { if (!stream.CanRead) { throw new ArgumentException("Cannot parse a non-readable stream"); } var seekableStream = stream; if (!stream.CanSeek) { seekableStream = StreamHelpers.CopyStream(stream); seekableStream.Seek(0, SeekOrigin.Begin); } subFormatDictionary = subFormatDictionary ?? _subFormatToParser; foreach (var subtitlesParser in subFormatDictionary) { try { var parser = subtitlesParser.Value; var items = parser.ParseStream(seekableStream, encoding); if (items.Any()) { return(items); } else { throw new FormatException(string.Format("Failed to parse as {0}", subtitlesParser.Key)); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } var firstCharsOfFile = LogFirstCharactersOfStream(stream, 500, encoding); var message = string.Format("All the subtitles parsers failed to parse the following stream:{0}", firstCharsOfFile); throw new ArgumentException(message); }
public string Add(Stream torrentFile, string outputDirectoryPath) { // Load the torrent. var torrent = Torrent.Load(torrentFile); // Check if torrent already added. if (Engine.Torrents.Any(t => t.InfoHash.ToString() == torrent.InfoHash.ToString())) { throw new TorrentAlreadyAddedException(); } // Check if outputDirectoryPath is not a file. if (File.Exists(outputDirectoryPath)) { throw new InvalidOutputDirectoryException("Already exists as a file. Can either not exist or must be a directory."); } // Check if output directory already in use. if (Engine.Torrents.Any(t => t.SavePath.StartsWith(outputDirectoryPath, StringComparison.CurrentCultureIgnoreCase))) { throw new OutputDirectoryAlreadyInUseException(); } // Save torrent file. torrentFile.Position = 0; var applicationDataTorrentFilePath = Path.Combine(TorrentFileDirectory, torrent.InfoHash.ToString() + ".torrent"); using (var file = File.OpenWrite(applicationDataTorrentFilePath)) StreamHelpers.CopyStream(torrentFile, file); // Reload the torrent. torrent = Torrent.Load(applicationDataTorrentFilePath); // Create output directory. if (!Directory.Exists(outputDirectoryPath)) { Directory.CreateDirectory(outputDirectoryPath); } // Finally add. return(Add(torrent, outputDirectoryPath)); }