Exemple #1
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;
        }
Exemple #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("保存路径包含无效字符");
                }
            }
        }