Ejemplo n.º 1
0
        public Download addTorrent(Torrent torrent, bool startTorrent, bool removeOriginal, TorrentSettings savedSettings, string savePath, bool isUrl)
        {
            string   originalPath = torrent.TorrentPath;
            Download manager;

            if (!Directory.Exists(savePath))
            {
                throw new TorrentException("Torrent save path does not exist, " + savePath);
            }

            // Check to see if torrent already exists
            if (engine.Contains(torrent))
            {
                logger.Error("Failed to add torrent, " + torrent.Name + " already exists.");
                throw new TorrentException("Failed to add torrent, " + torrent.Name + " already exists.");
            }

            // Move the .torrent to the local storage folder if it's not there already
            MoveToStorage(ref torrent);

            TorrentSettings settings = savedSettings ?? defaultTorrentSettings.Clone();
            FastResume      resume   = this.fastResume.Find(delegate(FastResume f) { return(f.Infohash == torrent.InfoHash); });

            manager = new Download(savePath, new TorrentManager(torrent, savePath, settings));
            if (resume != null)
            {
                manager.Manager.LoadFastResume(resume);
            }

            engine.Register(manager.Manager);

            if (removeOriginal)
            {
                logger.Info("Removing {0}", originalPath);
                File.Delete(originalPath);
            }

            Event.Raise <DownloadAddedEventArgs> (Added, this, new DownloadAddedEventArgs(manager));
            allTorrents.Add(manager);

            if (startTorrent)
            {
                logger.Info("Auto starting torrent " + manager.Torrent.Name);
                manager.Start();
            }

            logger.Info("Added torrent " + manager.Torrent.Name);

            return(manager);
        }
Ejemplo n.º 2
0
        public TorrentManager addTorrent(string torrentPath, bool startTorrent, bool moveToStorage, bool removeOriginal, TorrentSettings savedSettings, string savePath, bool isUrl)
        {
            Torrent        torrent;
            Torrent        torrentCheck;
            TorrentManager manager;
            string         newPath;

            if (!Directory.Exists(savePath))
            {
                throw new TorrentException("Torrent save path does not exist, " + savePath);
            }

            if (!isUrl && !File.Exists(torrentPath))
            {
                throw new TorrentException("Torrent path does not exist, " + torrentPath);
            }

            // Check to see if Torrent is valid
            if (!isUrl && !Torrent.TryLoad(torrentPath, out torrentCheck))
            {
                logger.Error("Failed to add torrent, " + Path.GetFileName(torrentPath) + " is not valid.");
                throw new TorrentException("Failed to add torrent, " + Path.GetFileName(torrentPath) + " is not valid.");
            }
            else if (isUrl && !Torrent.TryLoad(new System.Uri(torrentPath), Path.Combine(prefSettings.TorrentStorageLocation, Path.GetFileName(torrentPath)), out torrentCheck))
            {
                logger.Error("Failed to add URL, " + torrentPath + " is not valid.");
                throw new TorrentException("Failed to add URL, " + torrentPath + " is not valid.");
            }

            // Check to see if torrent already exists
            if (engine.Contains(torrentCheck))
            {
                logger.Error("Failed to add torrent, " + torrentCheck.Name + " already exists.");
                throw new TorrentException("Failed to add torrent, " + torrentCheck.Name + " already exists.");
            }

            // Move torrent to storage folder
            if (!isUrl && moveToStorage && (prefSettings.TorrentStorageLocation != Directory.GetParent(torrentPath).ToString()))
            {
                newPath = Path.Combine(prefSettings.TorrentStorageLocation, Path.GetFileName(torrentPath));
                logger.Debug("Copying torrent to " + newPath);
                File.Copy(torrentPath, newPath, true);

                if (removeOriginal)
                {
                    logger.Info("Deleting original torrent " + torrentPath);
                    try{
                        File.Delete(torrentPath);
                    } catch {
                        logger.Error("Unable to delete " + Path.GetFileName(torrentPath) + ".");
                        throw new Exception("Unable to delete " + Path.GetFileName(torrentPath) + ".");
                    }
                }
            }
            else
            {
                newPath = torrentPath;
            }



            // Load and register torrent
            if (!isUrl && !Torrent.TryLoad(newPath, out torrent))
            {
                logger.Error("Failed to register " + Path.GetFileName(newPath));
                throw new TorrentException("Failed to register " + Path.GetFileName(newPath));
            }
            else if (isUrl && !Torrent.TryLoad(new System.Uri(newPath), Path.Combine(prefSettings.TorrentStorageLocation, Path.GetFileName(newPath)), out torrent))
            {
                logger.Error("Failed to register " + newPath);
                throw new TorrentException("Failed to register " + newPath);
            }

            if (savedSettings == null)
            {
                manager = new TorrentManager(torrent, savePath, (TorrentSettings)torrentSettings.Clone());
            }
            else
            {
                manager = new TorrentManager(torrent, savePath, savedSettings);
            }

            engine.Register(manager);

            torrents.Add(manager, torrentListStore.AppendValues(manager));
            allTorrents.Add(manager);

            if (startTorrent)
            {
                logger.Info("Auto starting torrent " + manager.Torrent.Name);
                manager.Start();
                // Add to label
                if (manager.State == TorrentState.Downloading)
                {
                    mainWindow.DownloadingLabel.AddTorrent(manager);
                }
                else if (manager.State == TorrentState.Seeding)
                {
                    mainWindow.SeedingLabel.AddTorrent(manager);
                }
            }

            logger.Info("Added torrent " + manager.Torrent.Name);

            manager.TorrentStateChanged         += OnTorrentStateChanged;
            manager.PieceManager.BlockRequested += OnBlockRequested;
            manager.PieceHashed += OnPieceHashed;

            // add to "All" label
            mainWindow.AllLabel.AddTorrent(manager);

            return(manager);
        }