예제 #1
0
        private async void BtnOK_Click(object sender, EventArgs e)
        {
            string s1 = this.tbSaveFolder.Text;

            if (!Helper.CheckPath(ref s1, this.isWindowsPath))
            {
                return;
            }

            string s2 = Path.GetFileName(s1);

            try
            {
                if (this.cbSkipHashCheck.Checked)
                {
                    if (!Helper.CanSkipCheck(
                            this.bencodeTorrent,
                            Helper.GetVirtualPath(s1, this.actualToVirtualDic), false))
                    {
                        this.isAddTorrentSuccess = false;
                        this.failedReason        = "跳过哈希检测失败";
                        return;
                    }
                }

                if (this.btClient == Config.BTClient.qBittorrent)
                {
                    await QbtWebAPI.API.DownloadFromDisk(
                        new List <string>() { torrentPath },
                        s1, null,
                        string.IsNullOrWhiteSpace(this.cbCategory.Text)?
                        null : this.cbCategory.Text,
                        this.cbSkipHashCheck.Checked, !this.cbStartTorrent.Checked,
                        false, s2, null, null, null, null);
                }
                else if (this.btClient == Config.BTClient.Transmission)
                {
                    Debug.Assert(transmissionClient != null);
                    var addedTorrent = new NewTorrent
                    {
                        Metainfo          = Helper.GetTransmissionTorrentAddMetainfo(torrentPath),
                        DownloadDirectory = (
                            bencodeTorrent.FileMode == BencodeNET.Torrents.TorrentFileMode.Single ?
                            s1 :
                            Helper.GetDirectoryNameDonntChangeDelimiter(s1)),
                        Paused = (
                            bencodeTorrent.FileMode == BencodeNET.Torrents.TorrentFileMode.Single ?
                            (!this.cbStartTorrent.Checked) : 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(s1));
                        Debug.Assert(result != null && result.ID != 0);

                        if (this.cbStartTorrent.Checked)
                        {
                            transmissionClient.TorrentStartNow(new object[] { result.ID });
                        }
                    }
                }

                this.isAddTorrentSuccess = true;
                this.failedReason        = "";
            }
            catch (Exception ex)
            {
                this.failedReason = ex.Message;
            }
            finally
            {
                this.Close();
            }
        }
예제 #2
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("保存路径包含无效字符");
                }
            }
        }