Search in sources :

Example 6 with TorrentImpl

use of com.biglybt.pifimpl.local.torrent.TorrentImpl in project BiglyBT by BiglySoftware.

the class SubscriptionSchedulerImpl method download.

@Override
public void download(final Subscription subs, final SubscriptionResult original_result) {
    String download_link = original_result.getDownloadLink();
    if (download_link == null) {
        log(subs.getName() + ": can't download " + original_result.getID() + " as no direct download link available");
        return;
    }
    final String key = subs.getID() + ":" + original_result.getID();
    final String dl = download_link;
    synchronized (active_result_downloaders) {
        if (active_result_downloaders.contains(key)) {
            return;
        }
        log(subs.getName() + ": queued result for download - " + original_result.getID() + "/" + download_link);
        active_result_downloaders.add(key);
        result_downloader.run(new AERunnable() {

            @Override
            public void runSupport() {
                // need to fix up to the latest history due to the lazy nature of things :(
                SubscriptionResult result = subs.getHistory().getResult(original_result.getID());
                boolean success = false;
                try {
                    if (result == null) {
                        log(subs.getName() + ": result has been deleted - " + original_result.getID());
                        success = true;
                    } else if (result.getRead()) {
                        log(subs.getName() + ": result already marked as read, skipping - " + result.getID());
                        success = true;
                    } else {
                        boolean retry = true;
                        boolean use_ref = subs.getHistory().getDownloadWithReferer();
                        boolean tried_ref_switch = false;
                        while (retry) {
                            retry = false;
                            try {
                                TorrentUtils.setTLSDescription("Subscription: " + subs.getName());
                                URL original_url = new URL(dl);
                                PluginProxy plugin_proxy = null;
                                if (dl.startsWith("tor:")) {
                                    String target_resource = dl.substring(4);
                                    original_url = new URL(target_resource);
                                    Map<String, Object> options = new HashMap<>();
                                    options.put(AEProxyFactory.PO_PEER_NETWORKS, new String[] { AENetworkClassifier.AT_TOR });
                                    plugin_proxy = AEProxyFactory.getPluginProxy("Subscription result download of '" + target_resource + "'", original_url, options, true);
                                    if (plugin_proxy == null) {
                                        throw (new Exception("No Tor plugin proxy available for '" + dl + "'"));
                                    }
                                }
                                URL current_url = plugin_proxy == null ? original_url : plugin_proxy.getURL();
                                Torrent torrent = null;
                                try {
                                    while (true) {
                                        try {
                                            ResourceDownloaderFactory rdf = StaticUtilities.getResourceDownloaderFactory();
                                            ResourceDownloader url_rd = rdf.create(current_url, plugin_proxy == null ? null : plugin_proxy.getProxy());
                                            if (plugin_proxy != null) {
                                                url_rd.setProperty("URL_HOST", plugin_proxy.getURLHostRewrite() + (current_url.getPort() == -1 ? "" : (":" + current_url.getPort())));
                                            }
                                            String referer = use_ref ? subs.getReferer() : null;
                                            UrlUtils.setBrowserHeaders(url_rd, referer);
                                            Engine engine = subs.getEngine();
                                            if (engine instanceof WebEngine) {
                                                WebEngine we = (WebEngine) engine;
                                                if (we.isNeedsAuth()) {
                                                    String cookies = we.getCookies();
                                                    if (cookies != null && cookies.length() > 0) {
                                                        url_rd.setProperty("URL_Cookie", cookies);
                                                    }
                                                }
                                            }
                                            ResourceDownloader mr_rd = rdf.getMetaRefreshDownloader(url_rd);
                                            InputStream is = mr_rd.download();
                                            torrent = new TorrentImpl(TOTorrentFactory.deserialiseFromBEncodedInputStream(is));
                                            break;
                                        } catch (Throwable e) {
                                            if (plugin_proxy == null) {
                                                plugin_proxy = AEProxyFactory.getPluginProxy("Subscription result download", original_url);
                                                if (plugin_proxy != null) {
                                                    current_url = plugin_proxy.getURL();
                                                    continue;
                                                }
                                            }
                                            throw (e);
                                        }
                                    }
                                } finally {
                                    if (plugin_proxy != null) {
                                        plugin_proxy.setOK(torrent != null);
                                    }
                                }
                                byte[] hash = torrent.getHash();
                                // PlatformTorrentUtils.setContentTitle(torrent, torr );
                                DownloadManager dm = PluginInitializer.getDefaultInterface().getDownloadManager();
                                Download download;
                                // if we're assigning a tag/networks then we need to add it stopped in case the tag has any pre-start actions (e.g. set initial save location)
                                // this is because the assignments are done in SubscriptionManagerImpl on the download(willbe)added event
                                boolean stop_override = subs.getTagID() >= 0 || subs.getHistory().getDownloadNetworks() != null;
                                boolean auto_start = manager.shouldAutoStart(torrent);
                                manager.addPrepareTrigger(hash, new Subscription[] { subs }, new SubscriptionResult[] { result });
                                try {
                                    if (auto_start && !stop_override) {
                                        download = dm.addDownload(torrent);
                                    } else {
                                        download = dm.addDownloadStopped(torrent, null, null);
                                    }
                                } finally {
                                    manager.removePrepareTrigger(hash);
                                }
                                log(subs.getName() + ": added download " + download.getName() + ": auto-start=" + auto_start);
                                // maybe remove this as should be actioned in the trigger?
                                manager.prepareDownload(download, new Subscription[] { subs }, new SubscriptionResult[] { result });
                                subs.addAssociation(hash);
                                if (auto_start && stop_override) {
                                    download.restart();
                                }
                                result.setRead(true);
                                success = true;
                                if (tried_ref_switch) {
                                    subs.getHistory().setDownloadWithReferer(use_ref);
                                }
                            } catch (Throwable e) {
                                log(subs.getName() + ": Failed to download result " + dl, e);
                                if (e instanceof TOTorrentException && !tried_ref_switch) {
                                    use_ref = !use_ref;
                                    tried_ref_switch = true;
                                    retry = true;
                                    log(subs.getName() + ": Retrying " + (use_ref ? "with referer" : "without referer"));
                                }
                            } finally {
                                TorrentUtils.setTLSDescription(null);
                            }
                        }
                    }
                } finally {
                    try {
                        if (!success) {
                            if (dl.startsWith("azplug:") || dl.startsWith("chat:")) {
                                // whatever the outcome these have been handled async
                                result.setRead(true);
                            } else {
                                int rad = manager.getAutoDownloadMarkReadAfterDays();
                                if (rad > 0) {
                                    long rad_millis = rad * 24 * 60 * 60 * 1000L;
                                    long time_found = result.getTimeFound();
                                    if (time_found > 0 && time_found + rad_millis < SystemTime.getCurrentTime()) {
                                        log(subs.getName() + ": result expired, marking as read - " + result.getID());
                                        result.setRead(true);
                                    }
                                }
                            }
                        }
                    } catch (Throwable e) {
                        Debug.out(e);
                    }
                    synchronized (active_result_downloaders) {
                        active_result_downloaders.remove(key);
                    }
                    calculateSchedule();
                }
            }
        });
    }
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) TorrentImpl(com.biglybt.pifimpl.local.torrent.TorrentImpl) InputStream(java.io.InputStream) PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) DownloadManager(com.biglybt.pif.download.DownloadManager) URL(java.net.URL) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) ResourceDownloaderFactory(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory) Download(com.biglybt.pif.download.Download) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) Engine(com.biglybt.core.metasearch.Engine)

Example 7 with TorrentImpl

use of com.biglybt.pifimpl.local.torrent.TorrentImpl in project BiglyBT by BiglySoftware.

the class ResourceDownloaderTorrentImpl method downloadTorrent.

protected void downloadTorrent() {
    try {
        String name = new String(torrent_holder[0].getName(), Constants.DEFAULT_ENCODING);
        informActivity(getLogIndent() + "Downloading: " + name);
        // we *don't* want this temporary file to be deleted automatically as we're
        // going to use it across the client restarts to hold the download data and
        // to seed it afterwards. Therefore we don't use AETemporaryFileHandler.createTempFile!!!!
        final File torrent_file = AETemporaryFileHandler.createSemiTempFile(name + ".torrent");
        if (download_dir != null && !download_dir.exists()) {
            FileUtil.mkdirs(download_dir);
        }
        final File data_dir = download_dir == null ? torrent_file.getParentFile() : download_dir;
        final TOTorrent torrent = torrent_holder[0];
        TorrentUtils.setFlag(torrent, TorrentUtils.TORRENT_FLAG_LOW_NOISE, true);
        boolean anon = isAnonymous();
        torrent.serialiseToBEncodedFile(torrent_file);
        try {
            Download existing = download_manager.getDownload(torrent.getHash());
            if (existing != null) {
                int existing_state = existing.getState();
                if (existing_state == Download.ST_ERROR || existing_state == Download.ST_STOPPED) {
                    informActivity(getLogIndent() + "Deleting existing stopped/error state download for " + name);
                    existing.remove(true, true);
                }
            }
        } catch (Throwable e) {
        }
        DownloadWillBeAddedListener dwbal = null;
        try {
            Torrent t = new TorrentImpl(torrent);
            if (anon) {
                dwbal = new DownloadWillBeAddedListener() {

                    @Override
                    public void initialised(Download download) {
                        try {
                            if (Arrays.equals(download.getTorrentHash(), torrent.getHash())) {
                                PluginCoreUtils.unwrap(download).getDownloadState().setNetworks(AENetworkClassifier.AT_NON_PUBLIC);
                            }
                        } catch (Throwable e) {
                            Debug.out(e);
                        }
                    }
                };
                download_manager.addDownloadWillBeAddedListener(dwbal);
            } else {
                // if torrent includes i2p url and i2p installed then enable network
                Set<String> hosts = TorrentUtils.getUniqueTrackerHosts(torrent);
                boolean has_i2p = false;
                for (String host : hosts) {
                    if (AENetworkClassifier.categoriseAddress(host) == AENetworkClassifier.AT_I2P) {
                        has_i2p = true;
                    }
                }
                if (has_i2p && I2PHelpers.isI2PInstalled()) {
                    dwbal = new DownloadWillBeAddedListener() {

                        @Override
                        public void initialised(Download download) {
                            try {
                                if (Arrays.equals(download.getTorrentHash(), torrent.getHash())) {
                                    PluginCoreUtils.unwrap(download).getDownloadState().setNetworks(new String[] { AENetworkClassifier.AT_PUBLIC, AENetworkClassifier.AT_I2P });
                                }
                            } catch (Throwable e) {
                                Debug.out(e);
                            }
                        }
                    };
                    download_manager.addDownloadWillBeAddedListener(dwbal);
                }
            }
            if (persistent) {
                download = download_manager.addDownload(t, torrent_file, data_dir);
            } else {
                download = download_manager.addNonPersistentDownload(t, torrent_file, data_dir);
            }
        } finally {
            if (dwbal != null) {
                download_manager.removeDownloadWillBeAddedListener(dwbal);
            }
        }
        download.moveTo(1);
        download.setForceStart(true);
        // Prevents any move-on-completion or move-on-removal behaviour happening.
        download.setFlag(Download.FLAG_DISABLE_AUTO_FILE_MOVE, true);
        if (COConfigurationManager.getBooleanParameter("Ip Filter Disable For Updates")) {
            download.setFlag(Download.FLAG_DISABLE_IP_FILTER, true);
        }
        download_manager.addListener(new DownloadManagerListener() {

            @Override
            public void downloadAdded(Download download) {
            }

            @Override
            public void downloadRemoved(Download _download) {
                if (download == _download) {
                    ResourceDownloaderTorrentImpl.this.downloadRemoved(torrent_file, data_dir);
                }
            }
        });
        download.addListener(new DownloadListener() {

            @Override
            public void stateChanged(final Download download, int old_state, int new_state) {
                if (new_state == Download.ST_SEEDING) {
                    download.removeListener(this);
                    PluginInitializer.getDefaultInterface().getUtilities().createThread("resource complete event dispatcher", new Runnable() {

                        @Override
                        public void run() {
                            downloadSucceeded(download, torrent_file, data_dir);
                        }
                    });
                }
            }

            @Override
            public void positionChanged(Download download, int oldPosition, int newPosition) {
            }
        });
        Thread t = new AEThread("RDTorrent percentage checker") {

            @Override
            public void runSupport() {
                int last_percentage = 0;
                while (result == null) {
                    int this_percentage = download.getStats().getDownloadCompleted(false) / 10;
                    long total = torrent.getSize();
                    if (this_percentage != last_percentage) {
                        reportPercentComplete(ResourceDownloaderTorrentImpl.this, this_percentage);
                        last_percentage = this_percentage;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (Throwable e) {
                        Debug.printStackTrace(e);
                    }
                }
            }
        };
        t.setDaemon(true);
        t.start();
        if (download.getState() == Download.ST_SEEDING) {
            downloadSucceeded(download, torrent_file, data_dir);
        }
    } catch (Throwable e) {
        failed(this, new ResourceDownloaderException(this, "Torrent download failed", e));
    }
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent) Torrent(com.biglybt.pif.torrent.Torrent) TorrentImpl(com.biglybt.pifimpl.local.torrent.TorrentImpl) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) TOTorrent(com.biglybt.core.torrent.TOTorrent) File(java.io.File)

Example 8 with TorrentImpl

use of com.biglybt.pifimpl.local.torrent.TorrentImpl in project BiglyBT by BiglySoftware.

the class DownloadManagerImpl method getDownload.

protected Download getDownload(TOTorrent torrent) throws DownloadException {
    if (torrent != null) {
        for (int i = 0; i < downloads.size(); i++) {
            Download dl = (Download) downloads.get(i);
            TorrentImpl t = (TorrentImpl) dl.getTorrent();
            if (t == null) {
                continue;
            }
            if (t.getTorrent().hasSameHashAs(torrent)) {
                return (dl);
            }
        }
    }
    throw (new DownloadException("DownloadManager::getDownload: download not found"));
}
Also used : TorrentImpl(com.biglybt.pifimpl.local.torrent.TorrentImpl)

Example 9 with TorrentImpl

use of com.biglybt.pifimpl.local.torrent.TorrentImpl in project BiglyBT by BiglySoftware.

the class ShareResourceFileOrDirImpl method createTorrent.

protected void createTorrent() throws ShareException {
    try {
        manager.reportCurrentTask((item == null ? "Creating" : "Re-creating").concat(" torrent for '").concat(file.toString()).concat("'"));
        URL[] urls = manager.getAnnounceURLs();
        TOTorrentCreator creator = TOTorrentFactory.createFromFileOrDirWithComputedPieceLength(file, urls[0], manager.getAddHashes());
        creator.addListener(manager);
        TOTorrent to_torrent;
        try {
            manager.setTorrentCreator(creator);
            to_torrent = creator.create();
        } finally {
            manager.setTorrentCreator(null);
        }
        if (personal_key != null) {
            Map map = to_torrent.serialiseToMap();
            Map info = (Map) map.get("info");
            info.put("az_salt", personal_key);
            to_torrent = TOTorrentFactory.deserialiseFromMap(map);
        }
        LocaleTorrentUtil.setDefaultTorrentEncoding(to_torrent);
        for (int i = 1; i < urls.length; i++) {
            TorrentUtils.announceGroupsInsertLast(to_torrent, new URL[] { urls[i] });
        }
        String comment = COConfigurationManager.getStringParameter("Sharing Torrent Comment").trim();
        boolean private_torrent = COConfigurationManager.getBooleanParameter("Sharing Torrent Private");
        boolean dht_backup_enabled = COConfigurationManager.getBooleanParameter("Sharing Permit DHT");
        TorrentAttribute ta_props = TorrentManagerImpl.getSingleton().getAttribute(TorrentAttribute.TA_SHARE_PROPERTIES);
        String props = getAttribute(ta_props);
        if (props != null) {
            StringTokenizer tok = new StringTokenizer(props, ";");
            while (tok.hasMoreTokens()) {
                String token = tok.nextToken();
                int pos = token.indexOf('=');
                if (pos == -1) {
                    Debug.out("ShareProperty invalid: " + props);
                } else {
                    String lhs = token.substring(0, pos).trim().toLowerCase();
                    String rhs = token.substring(pos + 1).trim().toLowerCase();
                    boolean set = rhs.equals("true");
                    if (lhs.equals("private")) {
                        private_torrent = set;
                    } else if (lhs.equals("dht_backup")) {
                        dht_backup_enabled = set;
                    } else if (lhs.equals("comment")) {
                        comment = rhs;
                    } else {
                        Debug.out("ShareProperty invalid: " + props);
                        break;
                    }
                }
            }
        }
        if (comment.length() > 0) {
            to_torrent.setComment(comment);
        }
        TorrentUtils.setDHTBackupEnabled(to_torrent, dht_backup_enabled);
        TorrentUtils.setPrivate(to_torrent, private_torrent);
        if (TorrentUtils.isDecentralised(to_torrent)) {
            TorrentUtils.setDecentralised(to_torrent);
        }
        if (COConfigurationManager.getBooleanParameter("Sharing Disable RCM")) {
            TorrentUtils.setFlag(to_torrent, TorrentUtils.TORRENT_FLAG_DISABLE_RCM, true);
        }
        DownloadManagerState download_manager_state = DownloadManagerStateFactory.getDownloadState(to_torrent);
        TorrentUtils.setResumeDataCompletelyValid(download_manager_state);
        download_manager_state.save();
        if (item == null) {
            byte[] fingerprint = getFingerPrint();
            item = new ShareItemImpl(this, fingerprint, new TorrentImpl(to_torrent));
        } else {
            item.setTorrent(new TorrentImpl(to_torrent));
            item.writeTorrent();
        }
    } catch (TOTorrentException e) {
        if (e.getReason() == TOTorrentException.RT_CANCELLED) {
            throw (new ShareException("ShareResource: Operation cancelled", e));
        } else {
            throw (new ShareException("ShareResource: Torrent create failed", e));
        }
    } catch (Throwable e) {
        throw (new ShareException("ShareResource: Torrent create failed", e));
    }
}
Also used : TorrentImpl(com.biglybt.pifimpl.local.torrent.TorrentImpl) DownloadManagerState(com.biglybt.core.download.DownloadManagerState) URL(java.net.URL) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) StringTokenizer(java.util.StringTokenizer) TorrentAttribute(com.biglybt.pif.torrent.TorrentAttribute) ShareException(com.biglybt.pif.sharing.ShareException) TOTorrent(com.biglybt.core.torrent.TOTorrent) TOTorrentCreator(com.biglybt.core.torrent.TOTorrentCreator) Map(java.util.Map)

Aggregations

TorrentImpl (com.biglybt.pifimpl.local.torrent.TorrentImpl)9 TOTorrent (com.biglybt.core.torrent.TOTorrent)6 Torrent (com.biglybt.pif.torrent.Torrent)4 URL (java.net.URL)4 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)2 Download (com.biglybt.pif.download.Download)2 File (java.io.File)2 DiskManager (com.biglybt.core.disk.DiskManager)1 DownloadManager (com.biglybt.core.download.DownloadManager)1 DownloadManagerPeerListener (com.biglybt.core.download.DownloadManagerPeerListener)1 DownloadManagerState (com.biglybt.core.download.DownloadManagerState)1 LightWeightSeed (com.biglybt.core.lws.LightWeightSeed)1 Engine (com.biglybt.core.metasearch.Engine)1 WebEngine (com.biglybt.core.metasearch.impl.web.WebEngine)1 PEPeer (com.biglybt.core.peer.PEPeer)1 PEPeerManager (com.biglybt.core.peer.PEPeerManager)1 PluginProxy (com.biglybt.core.proxy.AEProxyFactory.PluginProxy)1 SECertificateListener (com.biglybt.core.security.SECertificateListener)1 TOTorrentCreator (com.biglybt.core.torrent.TOTorrentCreator)1 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)1