Search in sources :

Example 41 with TOTorrentException

use of com.biglybt.core.torrent.TOTorrentException in project BiglyBT by BiglySoftware.

the class TorrentOpener method mergeFileIntoTorrentInfo.

/**
 * Creates a TorrentInfo from a file.  Prompts user if the file is invalid,
 * torrent already exists
 *
 * @param sFileName
 * @param sOriginatingLocation
 * @return
 * @since 5.0.0.1
 */
// TODO: i18n
public static boolean mergeFileIntoTorrentInfo(String sFileName, final String sOriginatingLocation, TorrentOpenOptions torrentOptions) {
    TOTorrent torrent = null;
    File torrentFile;
    boolean bDeleteFileOnCancel = false;
    // actually made a copy.
    try {
        if (sFileName.startsWith("file://localhost/")) {
            sFileName = UrlUtils.decode(sFileName.substring(16));
        }
        final File fOriginal = new File(sFileName);
        if (!fOriginal.isFile() || !fOriginal.exists()) {
            UIFunctionsManager.getUIFunctions().showErrorMessage("OpenTorrentWindow.mb.openError", fOriginal.toString(), new String[] { UrlUtils.decode(sOriginatingLocation), "Not a File" });
            return false;
        }
        if (fOriginal.length() > TorrentUtils.MAX_TORRENT_FILE_SIZE) {
            UIFunctionsManager.getUIFunctions().showErrorMessage("OpenTorrentWindow.mb.openError", fOriginal.toString(), new String[] { UrlUtils.decode(sOriginatingLocation), "Too large to be a torrent" });
            return false;
        }
        torrentFile = TorrentUtils.copyTorrentFileToSaveDir(fOriginal, true);
        bDeleteFileOnCancel = !fOriginal.equals(torrentFile);
    // TODO if the files are still equal, and it isn't in the save
    // dir, we should copy it to a temp file in case something
    // re-writes it.  No need to copy a torrent coming from the
    // downloader though..
    } catch (IOException e1) {
        // Use torrent in wherever it is and hope for the best
        // XXX Should error instead?
        Debug.out(e1);
        torrentFile = new File(sFileName);
    }
    VuzeFileHandler vfh = VuzeFileHandler.getSingleton();
    VuzeFile vf = vfh.loadVuzeFile(torrentFile);
    if (vf != null) {
        vfh.handleFiles(new VuzeFile[] { vf }, VuzeFileComponent.COMP_TYPE_NONE);
        return false;
    }
    if (RSSUtils.isRSSFeed(torrentFile)) {
        boolean done = false;
        try {
            URL url = new URL(sOriginatingLocation);
            UIManager ui_manager = StaticUtilities.getUIManager(10 * 1000);
            if (ui_manager != null) {
                String details = MessageText.getString("subscription.request.add.message", new String[] { sOriginatingLocation });
                long res = ui_manager.showMessageBox("subscription.request.add.title", "!" + details + "!", UIManagerEvent.MT_YES | UIManagerEvent.MT_NO);
                if (res == UIManagerEvent.MT_YES) {
                    SubscriptionManager sm = PluginInitializer.getDefaultInterface().getUtilities().getSubscriptionManager();
                    sm.requestSubscription(url);
                    done = true;
                }
            }
        } catch (Throwable e) {
            Debug.out(e);
        }
        if (done) {
            if (bDeleteFileOnCancel) {
                torrentFile.delete();
            }
            return false;
        }
    }
    // Do a quick check to see if it's a torrent
    if (!TorrentUtil.isFileTorrent(sOriginatingLocation, torrentFile, torrentFile.getName(), !torrentOptions.getHideErrors())) {
        if (bDeleteFileOnCancel) {
            torrentFile.delete();
        }
        return false;
    }
    // Load up the torrent, see it it's real
    try {
        torrent = TorrentUtils.readFromFile(torrentFile, false);
    } catch (final TOTorrentException e) {
        UIFunctionsManager.getUIFunctions().showErrorMessage("OpenTorrentWindow.mb.openError", Debug.getStackTrace(e), new String[] { sOriginatingLocation, e.getMessage() });
        if (bDeleteFileOnCancel)
            torrentFile.delete();
        return false;
    }
    if (bDeleteFileOnCancel) {
        torrentOptions.setDeleteFileOnCancel(bDeleteFileOnCancel);
    }
    torrentOptions.sFileName = torrentFile.getAbsolutePath();
    torrentOptions.setTorrent(torrent);
    torrentOptions.sOriginatingLocation = sOriginatingLocation;
    return torrentOptions.getTorrent() != null;
}
Also used : UIManager(com.biglybt.pif.ui.UIManager) IOException(java.io.IOException) SubscriptionManager(com.biglybt.pif.utils.subscriptions.SubscriptionManager) URL(java.net.URL) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) TOTorrent(com.biglybt.core.torrent.TOTorrent) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) File(java.io.File) VuzeFileHandler(com.biglybt.core.vuzefile.VuzeFileHandler)

Example 42 with TOTorrentException

use of com.biglybt.core.torrent.TOTorrentException in project BiglyBT by BiglySoftware.

the class ExportTorrentWizard method performExport.

protected boolean performExport() {
    File input_file;
    try {
        input_file = new File(getTorrentFile()).getCanonicalFile();
    } catch (IOException e) {
        MessageBox mb = new MessageBox(getWizardWindow(), SWT.ICON_ERROR | SWT.OK);
        mb.setText(MessageText.getString("exportTorrentWizard.process.inputfilebad.title"));
        mb.setMessage(MessageText.getString("exportTorrentWizard.process.inputfilebad.message") + "\n" + e.toString());
        mb.open();
        return (false);
    }
    File output_file = new File(export_file);
    if (output_file.exists()) {
        MessageBox mb = new MessageBox(this.getWizardWindow(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
        mb.setText(MessageText.getString("exportTorrentWizard.process.outputfileexists.title"));
        mb.setMessage(MessageText.getString("exportTorrentWizard.process.outputfileexists.message"));
        int result = mb.open();
        if (result == SWT.NO) {
            return (false);
        }
    }
    String error_title;
    String error_detail;
    try {
        TOTorrent torrent;
        try {
            torrent = TOTorrentFactory.deserialiseFromBEncodedFile(input_file);
            try {
                torrent.serialiseToXMLFile(output_file);
                return (true);
            } catch (TOTorrentException e) {
                error_title = MessageText.getString("exportTorrentWizard.process.exportfail.title");
                error_detail = TorrentUtils.exceptionToText(e);
            }
        } catch (TOTorrentException e) {
            error_title = MessageText.getString("exportTorrentWizard.process.torrentfail.title");
            error_detail = TorrentUtils.exceptionToText(e);
        }
    } catch (Throwable e) {
        error_title = MessageText.getString("exportTorrentWizard.process.unknownfail.title");
        error_detail = e.toString();
    }
    MessageBox mb = new MessageBox(this.getWizardWindow(), SWT.ICON_ERROR | SWT.OK);
    mb.setText(error_title);
    mb.setMessage(error_detail);
    mb.open();
    return (false);
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) TOTorrent(com.biglybt.core.torrent.TOTorrent) IOException(java.io.IOException) File(java.io.File) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 43 with TOTorrentException

use of com.biglybt.core.torrent.TOTorrentException in project BiglyBT by BiglySoftware.

the class TorrentUIUtilsV3 method _loadTorrent.

private static void _loadTorrent(final Core core, final DownloadUrlInfo dlInfo, // open player
final boolean playNow, // as for open player but don't actually open it
final boolean playPrepare, final boolean bringToFront) {
    if (dlInfo instanceof DownloadUrlInfoSWT) {
        DownloadUrlInfoSWT dlInfoSWT = (DownloadUrlInfoSWT) dlInfo;
        dlInfoSWT.invoke(playNow ? "play" : "download");
        return;
    }
    String url = dlInfo.getDownloadURL();
    try {
        Matcher m = hashPattern.matcher(url);
        if (m.find()) {
            String hash = m.group(1);
            GlobalManager gm = core.getGlobalManager();
            final DownloadManager dm = gm.getDownloadManager(new HashWrapper(Base32.decode(hash)));
            if (dm != null) {
                if (playNow || playPrepare) {
                    new AEThread2("playExisting", true) {

                        @Override
                        public void run() {
                            if (playNow) {
                                Debug.outNoStack("loadTorrent already exists.. playing", false);
                                TorrentListViewsUtils.playOrStream(dm, -1);
                            } else {
                                Debug.outNoStack("loadTorrent already exists.. preparing", false);
                                PlayUtils.prepareForPlay(dm);
                            }
                        }
                    }.start();
                } else {
                    new MessageBoxShell(SWT.OK, MSG_ALREADY_EXISTS, new String[] { " ", dm.getDisplayName(), MessageText.getString(MSG_ALREADY_EXISTS_NAME) }).open(null);
                }
                return;
            }
        }
        UIFunctionsSWT uiFunctions = (UIFunctionsSWT) UIFunctionsManager.getUIFunctions();
        if (uiFunctions != null) {
            // if (!COConfigurationManager.getBooleanParameter("add_torrents_silently")) { not used 11/30/2015
            if (bringToFront) {
                uiFunctions.bringToFront();
            }
            // }
            Shell shell = uiFunctions.getMainShell();
            if (shell != null) {
                new FileDownloadWindow(shell, url, dlInfo.getReferer(), dlInfo.getRequestProperties(), null, new TorrentDownloaderCallBackInterface() {

                    @Override
                    public void TorrentDownloaderEvent(int state, TorrentDownloader inf) {
                        if (state == TorrentDownloader.STATE_FINISHED) {
                            File file = inf.getFile();
                            file.deleteOnExit();
                            // Do a quick check to see if it's a torrent
                            if (!TorrentUtil.isFileTorrent(dlInfo.getDownloadURL(), file, file.getName(), true)) {
                                return;
                            }
                            TOTorrent torrent;
                            try {
                                torrent = TorrentUtils.readFromFile(file, false);
                            } catch (TOTorrentException e) {
                                Debug.out(e);
                                return;
                            }
                            // Security: Only allow torrents from whitelisted trackers
                            if (playNow && !PlatformTorrentUtils.isPlatformTracker(torrent)) {
                                Debug.out("stopped loading torrent because it's not in whitelist");
                                return;
                            }
                            HashWrapper hw;
                            try {
                                hw = torrent.getHashWrapper();
                            } catch (TOTorrentException e1) {
                                Debug.out(e1);
                                return;
                            }
                            GlobalManager gm = core.getGlobalManager();
                            if (playNow || playPrepare) {
                                DownloadManager existingDM = gm.getDownloadManager(hw);
                                if (existingDM != null) {
                                    if (playNow) {
                                        TorrentListViewsUtils.playOrStream(existingDM, -1);
                                    } else {
                                        PlayUtils.prepareForPlay(existingDM);
                                    }
                                    return;
                                }
                            }
                            final HashWrapper fhw = hw;
                            GlobalManagerListener l = new GlobalManagerAdapter() {

                                @Override
                                public void downloadManagerAdded(DownloadManager dm) {
                                    try {
                                        core.getGlobalManager().removeListener(this);
                                        handleDMAdded(dm, playNow, playPrepare, fhw);
                                    } catch (Exception e) {
                                        Debug.out(e);
                                    }
                                }
                            };
                            gm.addListener(l, false);
                            TorrentOpener.openTorrent(file.getAbsolutePath());
                        }
                    }
                });
            }
        }
    } catch (Exception e) {
        Debug.out(e);
    }
}
Also used : TorrentDownloaderCallBackInterface(com.biglybt.core.torrentdownloader.TorrentDownloaderCallBackInterface) Matcher(java.util.regex.Matcher) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) DownloadManager(com.biglybt.core.download.DownloadManager) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) DownloadUrlInfoSWT(com.biglybt.ui.swt.browser.listener.DownloadUrlInfoSWT) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) Shell(org.eclipse.swt.widgets.Shell) GlobalManager(com.biglybt.core.global.GlobalManager) GlobalManagerAdapter(com.biglybt.core.global.GlobalManagerAdapter) TOTorrent(com.biglybt.core.torrent.TOTorrent) GlobalManagerListener(com.biglybt.core.global.GlobalManagerListener) TorrentDownloader(com.biglybt.core.torrentdownloader.TorrentDownloader) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) File(java.io.File)

Example 44 with TOTorrentException

use of com.biglybt.core.torrent.TOTorrentException in project BiglyBT by BiglySoftware.

the class DownloadManagerImpl method getDownload.

@Override
public Download getDownload(byte[] hash) {
    DownloadManager manager = global_manager.getDownloadManager(new HashWrapper(hash));
    if (manager != null) {
        try {
            return getDownload(manager);
        } catch (DownloadException e) {
        }
    }
    List dls = global_manager.getDownloadManagers();
    for (int i = 0; i < dls.size(); i++) {
        DownloadManager man = (DownloadManager) dls.get(i);
        // torrent can be null if download manager torrent file read fails
        TOTorrent torrent = man.getTorrent();
        if (torrent != null) {
            try {
                if (Arrays.equals(torrent.getHash(), hash)) {
                    return (getDownload(torrent));
                }
            } catch (DownloadException e) {
            // not found
            } catch (TOTorrentException e) {
                Debug.printStackTrace(e);
            }
        }
    }
    return (null);
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) TOTorrent(com.biglybt.core.torrent.TOTorrent) DownloadManager(com.biglybt.core.download.DownloadManager)

Example 45 with TOTorrentException

use of com.biglybt.core.torrent.TOTorrentException 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

TOTorrentException (com.biglybt.core.torrent.TOTorrentException)45 TOTorrent (com.biglybt.core.torrent.TOTorrent)23 File (java.io.File)15 IOException (java.io.IOException)9 URL (java.net.URL)8 DownloadManager (com.biglybt.core.download.DownloadManager)7 Core (com.biglybt.core.Core)3 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)3 GlobalManager (com.biglybt.core.global.GlobalManager)3 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)3 SimpleXMLParserDocumentNode (com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentNode)3 List (java.util.List)3 Map (java.util.Map)3 CoreRunningListener (com.biglybt.core.CoreRunningListener)2 DownloadManagerState (com.biglybt.core.download.DownloadManagerState)2 LogEvent (com.biglybt.core.logging.LogEvent)2 TOTorrentProgressListener (com.biglybt.core.torrent.TOTorrentProgressListener)2 TRHostTorrent (com.biglybt.core.tracker.host.TRHostTorrent)2 TorrentImpl (com.biglybt.pifimpl.local.torrent.TorrentImpl)2 TrackerEditorListener (com.biglybt.ui.swt.maketorrent.TrackerEditorListener)2