示例#1
0
 internal Torrent(TorrentClient client, BTorrent btorrent)
 {
     _client       = client;
     _btorrent     = btorrent;
     AnnounceTiers = new List <AnnounceTier>();
     foreach (IList <string> trackers in btorrent.Trackers)
     {
         AnnounceTiers.Add(new AnnounceTier(this, trackers.Select(client.GetOrCreateTracker).ToList()));
     }
 }
示例#2
0
        public AddTorrentManual(
            string torrentPath,
            BencodeNET.Torrents.Torrent bencodeTorrent,
            string settingSaveFolder,
            bool skipHashCheck,
            bool startTorrent,
            string category,
            Config.BTClient btClient,
            bool isWindowsPath,
            Dictionary <string, string> actualToVirtualDic = null,
            Transmission.API.RPC.Client transmissionClient = null,
            Form ownerForm = null)
        {
            InitializeComponent();

            if (ownerForm == null)
            {
                this.StartPosition = FormStartPosition.CenterParent;
            }
            else
            {
                this.StartPosition = FormStartPosition.Manual;
                Point topRight = Helper.GetFormRightTopLocation(ownerForm);
                this.Location = new Point(topRight.X - this.Width, topRight.Y);
            }

            this.isAddTorrentSuccess = false;
            this.failedReason        = "用户取消";

            this.torrentPath       = torrentPath;
            this.bencodeTorrent    = bencodeTorrent;
            this.settingSaveFolder = settingSaveFolder;
            if (btClient == Config.BTClient.qBittorrent)
            {
                this.cbSkipHashCheck.Checked = skipHashCheck;
            }
            else if (btClient == Config.BTClient.Transmission)
            {
                this.cbSkipHashCheck.Checked = false;
                this.cbSkipHashCheck.Enabled = false;
            }

            this.labelCategory.Enabled = (btClient == Config.BTClient.qBittorrent);
            this.cbCategory.Enabled    = (btClient == Config.BTClient.qBittorrent);

            this.cbStartTorrent.Checked = startTorrent;
            this.defaultCategory        = category;
            this.btClient           = btClient;
            this.isWindowsPath      = isWindowsPath;
            this.actualToVirtualDic = actualToVirtualDic;
            this.transmissionClient = transmissionClient;
        }
示例#3
0
文件: Helper.cs 项目: livingli/MyQbt
        public static bool CanSkipCheck(
            BencodeNET.Torrents.Torrent torrent,
            string virtualSaveFolder, bool hasRootFolder)
        {
            if (torrent.FileMode == BencodeNET.Torrents.TorrentFileMode.Unknown)
            {
                return(false);
            }
            else if (torrent.FileMode == BencodeNET.Torrents.TorrentFileMode.Single)
            {
                string filePath = Path.Combine(virtualSaveFolder, torrent.File.FileName);
                if (File.Exists(filePath))
                {
                    FileInfo fi = new FileInfo(filePath);
                    if (fi.Length != torrent.File.FileSize)
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                if (hasRootFolder)
                {
                    virtualSaveFolder = Path.Combine(virtualSaveFolder, torrent.DisplayName);
                }

                foreach (var v in torrent.Files)
                {
                    string filePath = Path.Combine(virtualSaveFolder, v.FullPath);
                    if (File.Exists(filePath))
                    {
                        FileInfo fi = new FileInfo(filePath);
                        if (fi.Length != v.FileSize)
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
示例#4
0
        public static DataNode CreateDataNode(
            BencodeNET.Torrents.Torrent torrent)
        {
            if (torrent.FileMode == BencodeNET.Torrents.TorrentFileMode.Unknown)
            {
                return(null);
            }

            DataNode dataNode = new DataNode("", 0, 0, true, true);

            if (torrent.FileMode == BencodeNET.Torrents.TorrentFileMode.Single)
            {
                dataNode.Size         += torrent.File.FileSize;
                dataNode.chileNodeList = new List <DataNode>()
                {
                    new DataNode(torrent.File.FileName,
                                 torrent.File.FileSize, 1, false, false)
                };
                return(dataNode);
            }

            foreach (BencodeNET.Torrents.MultiFileInfo multiFileInfo in torrent.Files)
            {
                DataNode fup = dataNode;
                fup.Size += multiFileInfo.FileSize;

                for (int i = 0; i < multiFileInfo.Path.Count - 1; i++)
                {
                    if (fup.chileNodeList == null)
                    {
                        fup.chileNodeList = new List <DataNode>();
                    }

                    DataNode fcu = fup.FindChildFolderNode(multiFileInfo.Path[i]);
                    if (fcu != null)
                    {
                        fcu.Size += multiFileInfo.FileSize;
                    }
                    else
                    {
                        fcu = new DataNode(multiFileInfo.Path[i],
                                           multiFileInfo.FileSize, i + 1, true, false);
                        fup.chileNodeList.Add(fcu);
                    }
                    fup = fcu;
                }

                if (fup.chileNodeList == null)
                {
                    fup.chileNodeList = new List <DataNode>();
                }

                fup.chileNodeList.Add(new DataNode(
                                          multiFileInfo.FileName,
                                          multiFileInfo.FileSize,
                                          multiFileInfo.Path.Count,
                                          false, false));
            }

            return(dataNode);
        }
示例#5
0
        public static async Task AddTorrent(
            string torrentPath, BencodeNET.Torrents.Torrent bencodeTorrent,
            string saveFolder, bool skipHashCheck,
            bool startTorrent, string category, Config.BTClient btClient,
            bool isWindowsPath,
            Dictionary <string, string> actualToVirtualDic = null,
            Transmission.API.RPC.Client transmissionClient = null)
        {
            if (bencodeTorrent != null)
            {
                string strTitle =
                    bencodeTorrent.FileMode == BencodeNET.Torrents.TorrentFileMode.Single ?
                    Path.GetFileNameWithoutExtension(bencodeTorrent.DisplayName) :
                    bencodeTorrent.DisplayName;

                strTitle = Path.GetFileNameWithoutExtension(torrentPath) +
                           AddDotOrBlank(strTitle) +
                           strTitle;

                string strSaveFolderPath = saveFolder + (isWindowsPath ? "\\" : "/") + strTitle;

                if (Helper.CheckPath(ref strSaveFolderPath, isWindowsPath))
                {
                    if (skipHashCheck)
                    {
                        if (!Helper.CanSkipCheck(
                                bencodeTorrent,
                                Helper.GetVirtualPath(strSaveFolderPath, actualToVirtualDic), false))
                        {
                            throw new Exception("跳过哈希检测失败");
                        }
                    }

                    if (btClient == Config.BTClient.qBittorrent)
                    {
                        await QbtWebAPI.API.DownloadFromDisk(
                            new List <string>() { torrentPath }, strSaveFolderPath,
                            null, string.IsNullOrWhiteSpace(category)?null : category,
                            skipHashCheck, !startTorrent, false, strTitle, null, null, null, null);
                    }
                    else if (btClient == Config.BTClient.Transmission)
                    {
                        Debug.Assert(transmissionClient != null);
                        var addedTorrent = new NewTorrent
                        {
                            Metainfo          = Helper.GetTransmissionTorrentAddMetainfo(torrentPath),
                            DownloadDirectory = (
                                bencodeTorrent.FileMode == BencodeNET.Torrents.TorrentFileMode.Single ?
                                strSaveFolderPath :
                                Helper.GetDirectoryNameDonntChangeDelimiter(strSaveFolderPath)),
                            Paused = (
                                bencodeTorrent.FileMode == BencodeNET.Torrents.TorrentFileMode.Single ?
                                (!startTorrent) : true)
                        };
                        var addedTorrentInfo = transmissionClient.TorrentAdd(addedTorrent);
                        Debug.Assert(addedTorrentInfo != null && addedTorrentInfo.ID != 0);

                        if (bencodeTorrent.FileMode == BencodeNET.Torrents.TorrentFileMode.Multi)
                        {
                            var result = transmissionClient.TorrentRenamePath(
                                addedTorrentInfo.ID,
                                bencodeTorrent.DisplayName,
                                Path.GetFileName(strSaveFolderPath));
                            Debug.Assert(result != null && result.ID != 0);

                            if (startTorrent)
                            {
                                transmissionClient.TorrentStartNow(new object[] { result.ID });
                            }
                        }
                    }
                }
                else
                {
                    throw new Exception("保存路径包含无效字符");
                }
            }
        }