Ejemplo n.º 1
0
        public void MakeTorrentData()
        {
            if (AnnounceURL.Equals("")) { return; }

            DirectoryInfo directoryInfo = null;
            MultipleFilesStream filesMFS = null;

            try
            {
                if (Directory.Exists(DirectoryName))
                {
                    directoryInfo = new DirectoryInfo(DirectoryName);
                    torrentSaved.Info.Name = directoryInfo.Name;
                    if (TorrentFileName.Equals(""))
                    {
                        torrentSaved.FileName = directoryInfo.FullName + '\\' + torrentSaved.Info.Name + ".torrent";
                    }
                    else
                    {
                        torrentSaved.FileName = directoryInfo.FullName + '\\' + TorrentFileName;
                    }

                    if (MovieOnly)
                    {
                        filesFilter = new string[] { "*.mkv", "*.mp4", "*.avi" };
                    }
                    filesMFS = new MultipleFilesStream(directoryInfo, filesFilter);
                }
                else if (File.Exists(FileName))
                {
                    FileInfo fileInfo = new FileInfo(FileName);
                    directoryInfo = new DirectoryInfo(fileInfo.DirectoryName);
                    torrentSaved.Info.Name = fileInfo.Name;
                    if (TorrentFileName.Equals(""))
                    {
                        torrentSaved.FileName = directoryInfo.FullName + '\\' + torrentSaved.Info.Name + ".torrent";
                    }
                    else
                    {
                        torrentSaved.FileName = directoryInfo.FullName + '\\' + TorrentFileName;
                    }
                    List<string> files = new List<string>();
                    files.Add(fileInfo.FullName);
                    filesMFS = new MultipleFilesStream(files);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw new ArgumentException(messages.GetMessage("FilesNotFound"));
            }

            if (PiecesLength <= 0)
            {
                torrentSaved.Info.PieceLength = GetPiecesLength(filesMFS.Length);
            }
            else
            {
                torrentSaved.Info.PieceLength = (long)Math.Pow(2, PiecesLength);
            }
            Console.WriteLine("{0}: {1}", messages.GetMessage("PieceLength"), torrentSaved.Info.PieceLength);

            Console.WriteLine("\n{0}: {1}", messages.GetMessage("FilesFound"), filesMFS.GetFilesInfo().Count());
            if (filesMFS.GetFilesInfo().Count() > 0)
            {
                int i = 0;
                torrentSaved.Info.torrentFiles = new TorrentFileStruct[filesMFS.GetFilesInfo().Count()];
                foreach (FileInfo fileInfo in filesMFS.GetFilesInfo())
                {
                    if (directoryInfo.FullName == fileInfo.DirectoryName)
                    {
                        torrentSaved.Info.torrentFiles[i].Path = fileInfo.Name;
                    }
                    else
                    {
                        torrentSaved.Info.torrentFiles[i].Path =
                            fileInfo.DirectoryName.Replace(directoryInfo.FullName + "\\", "") + "\\" + fileInfo.Name;
                    }
                    torrentSaved.Info.torrentFiles[i].Length = fileInfo.Length;
                    Console.WriteLine("{0} [{1}]", torrentSaved.Info.torrentFiles[i].Path, fileInfo.Length);
                    i++;
                }
            }

            using (MemoryStream piecesMemoryStream = new MemoryStream())
            {
                using (BinaryReader inputStreamReader = new BinaryReader(filesMFS))
                {
                    byte[] readBuffer = new Byte[torrentSaved.Info.PieceLength];
                    long readCounter, totalReadCounter = 0, toRead = filesMFS.Length;
                    while ((readCounter = inputStreamReader.Read(readBuffer, 0, readBuffer.Length)) > 0)
                    {
                        byte[] lastBuffer = new byte[readCounter];
                        if (readCounter < readBuffer.Length)
                        {
                            Array.Copy(readBuffer, 0, lastBuffer, 0, readCounter);
                        }
                        else
                        {
                            lastBuffer = readBuffer;
                        }
                        byte[] sha1Hash = (new SHA1CryptoServiceProvider()).ComputeHash(lastBuffer);
                        piecesMemoryStream.Write(sha1Hash, 0, sha1Hash.Length);
                        totalReadCounter += readCounter;

                        Console.Write("\r{0}... {1} / {2} [{3:p}]", messages.GetMessage("Progress"), totalReadCounter,
                            toRead, (double)totalReadCounter / toRead);
                    }
                }
                torrentSaved.Info.Pieces = piecesMemoryStream.ToArray();
            }

            if (PrivateTorrent)
                torrentSaved.Info.PrivateTorrent = 1;

            torrentSaved.Announce = AnnounceURL;

            TorrentPrepared = true;
        }
Ejemplo n.º 2
0
        public void MakeTorrentData()
        {
            if (AnnounceURL.Equals(""))
            {
                return;
            }

            DirectoryInfo       directoryInfo = null;
            MultipleFilesStream filesMFS      = null;

            try
            {
                if (Directory.Exists(DirectoryName))
                {
                    directoryInfo          = new DirectoryInfo(DirectoryName);
                    torrentSaved.Info.Name = directoryInfo.Name;
                    if (TorrentFileName.Equals(""))
                    {
                        torrentSaved.FileName = directoryInfo.FullName + '\\' + torrentSaved.Info.Name + ".torrent";
                    }
                    else
                    {
                        torrentSaved.FileName = directoryInfo.FullName + '\\' + TorrentFileName;
                    }

                    if (MovieOnly)
                    {
                        filesFilter = new string[] { "*.mkv", "*.mp4", "*.avi" };
                    }
                    filesMFS = new MultipleFilesStream(directoryInfo, filesFilter);
                }
                else if (File.Exists(FileName))
                {
                    FileInfo fileInfo = new FileInfo(FileName);
                    directoryInfo          = new DirectoryInfo(fileInfo.DirectoryName);
                    torrentSaved.Info.Name = fileInfo.Name;
                    if (TorrentFileName.Equals(""))
                    {
                        torrentSaved.FileName = directoryInfo.FullName + '\\' + torrentSaved.Info.Name + ".torrent";
                    }
                    else
                    {
                        torrentSaved.FileName = directoryInfo.FullName + '\\' + TorrentFileName;
                    }
                    List <string> files = new List <string>();
                    files.Add(fileInfo.FullName);
                    filesMFS = new MultipleFilesStream(files);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw new ArgumentException(messages.GetMessage("FilesNotFound"));
            }

            if (PiecesLength <= 0)
            {
                torrentSaved.Info.PieceLength = GetPiecesLength(filesMFS.Length);
            }
            else
            {
                torrentSaved.Info.PieceLength = (long)Math.Pow(2, PiecesLength);
            }
            Console.WriteLine("{0}: {1}", messages.GetMessage("PieceLength"), torrentSaved.Info.PieceLength);

            Console.WriteLine("\n{0}: {1}", messages.GetMessage("FilesFound"), filesMFS.GetFilesInfo().Count());
            if (filesMFS.GetFilesInfo().Count() > 0)
            {
                int i = 0;
                torrentSaved.Info.torrentFiles = new TorrentFileStruct[filesMFS.GetFilesInfo().Count()];
                foreach (FileInfo fileInfo in filesMFS.GetFilesInfo())
                {
                    if (directoryInfo.FullName == fileInfo.DirectoryName)
                    {
                        torrentSaved.Info.torrentFiles[i].Path = fileInfo.Name;
                    }
                    else
                    {
                        torrentSaved.Info.torrentFiles[i].Path =
                            fileInfo.DirectoryName.Replace(directoryInfo.FullName + "\\", "") + "\\" + fileInfo.Name;
                    }
                    torrentSaved.Info.torrentFiles[i].Length = fileInfo.Length;
                    Console.WriteLine("{0} [{1}]", torrentSaved.Info.torrentFiles[i].Path, fileInfo.Length);
                    i++;
                }
            }

            using (MemoryStream piecesMemoryStream = new MemoryStream())
            {
                using (BinaryReader inputStreamReader = new BinaryReader(filesMFS))
                {
                    byte[] readBuffer = new Byte[torrentSaved.Info.PieceLength];
                    long   readCounter, totalReadCounter = 0, toRead = filesMFS.Length;
                    while ((readCounter = inputStreamReader.Read(readBuffer, 0, readBuffer.Length)) > 0)
                    {
                        byte[] lastBuffer = new byte[readCounter];
                        if (readCounter < readBuffer.Length)
                        {
                            Array.Copy(readBuffer, 0, lastBuffer, 0, readCounter);
                        }
                        else
                        {
                            lastBuffer = readBuffer;
                        }
                        byte[] sha1Hash = (new SHA1CryptoServiceProvider()).ComputeHash(lastBuffer);
                        piecesMemoryStream.Write(sha1Hash, 0, sha1Hash.Length);
                        totalReadCounter += readCounter;

                        Console.Write("\r{0}... {1} / {2} [{3:p}]", messages.GetMessage("Progress"), totalReadCounter,
                                      toRead, (double)totalReadCounter / toRead);
                    }
                }
                torrentSaved.Info.Pieces = piecesMemoryStream.ToArray();
            }

            if (PrivateTorrent)
            {
                torrentSaved.Info.PrivateTorrent = 1;
            }

            torrentSaved.Announce = AnnounceURL;

            TorrentPrepared = true;
        }