Search in sources :

Example 6 with TorrentInfo

use of com.frostwire.jlibtorrent.TorrentInfo in project frostwire by frostwire.

the class TorrentFetcherDownload method downloadTorrent.

private void downloadTorrent(final byte[] data, final List<TcpEndpoint> peers) {
    if (VPNDropGuard.canUseBitTorrent()) {
        if (relativePath != null) {
            try {
                TorrentInfo ti = TorrentInfo.bdecode(data);
                boolean[] selection = calculateSelection(ti, relativePath);
                BTEngine.getInstance().download(ti, null, selection, peers);
            } catch (Throwable e) {
                LOG.error("Error downloading torrent", e);
            }
        } else {
            GUIMediator.safeInvokeLater(new Runnable() {

                public void run() {
                    try {
                        boolean[] selection = null;
                        if (partial) {
                            PartialFilesDialog dlg = new PartialFilesDialog(GUIMediator.getAppFrame(), data, displayName);
                            dlg.setVisible(true);
                            selection = dlg.getFilesSelection();
                            if (selection == null) {
                                return;
                            }
                        }
                        TorrentInfo ti = TorrentInfo.bdecode(data);
                        BTEngine.getInstance().download(ti, null, selection, peers);
                        GUIMediator.instance().showTransfers(TransfersTab.FilterMode.ALL);
                    } catch (Throwable e) {
                        LOG.error("Error downloading torrent", e);
                    }
                }
            });
        }
    }
}
Also used : TorrentInfo(com.frostwire.jlibtorrent.TorrentInfo)

Example 7 with TorrentInfo

use of com.frostwire.jlibtorrent.TorrentInfo in project frostwire by frostwire.

the class TorrentUtil method makeTorrentAndDownload.

/**
 * @param file                   - The file/dir to make a torrent out of
 * @param uiTorrentMakerListener - an optional listener of this process
 * @param showShareTorrentDialog - show the share dialog when done
 * @param dhtTrackedOnly         - if true, no trackers are added, otherwise adds a list of default trackers.
 */
private static void makeTorrentAndDownload(final File file, final UITorrentMakerListener uiTorrentMakerListener, final boolean showShareTorrentDialog, boolean dhtTrackedOnly) {
    try {
        file_storage fs = new file_storage();
        libtorrent.add_files(fs, file.getAbsolutePath());
        create_torrent torrentCreator = new create_torrent(fs);
        if (!dhtTrackedOnly) {
            torrentCreator.add_tracker("udp://tracker.openbittorrent.com:80", 0);
            torrentCreator.add_tracker("udp://tracker.publicbt.com:80", 0);
            torrentCreator.add_tracker("udp://open.demonii.com:1337", 0);
            torrentCreator.add_tracker("udp://tracker.coppersurfer.tk:6969", 0);
            torrentCreator.add_tracker("udp://tracker.leechers-paradise.org:6969", 0);
            torrentCreator.add_tracker("udp://exodus.desync.com:6969", 0);
            torrentCreator.add_tracker("udp://tracker.pomf.se", 0);
        }
        torrentCreator.set_priv(false);
        torrentCreator.set_creator("FrostWire " + FrostWireUtils.getFrostWireVersion() + " build " + FrostWireUtils.getBuildNumber());
        final File torrentFile = new File(SharingSettings.TORRENTS_DIR_SETTING.getValue(), file.getName() + ".torrent");
        final error_code ec = new error_code();
        libtorrent.set_piece_hashes(torrentCreator, file.getParentFile().getAbsolutePath(), ec);
        if (ec.value() != 0 && uiTorrentMakerListener != null) {
            uiTorrentMakerListener.onCreateTorrentError(ec);
            return;
        }
        final entry torrentEntry = torrentCreator.generate();
        byte[] bencoded_torrent_bytes = Vectors.byte_vector2bytes(torrentEntry.bencode());
        FileOutputStream fos = new FileOutputStream(torrentFile);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bos.write(bencoded_torrent_bytes);
        bos.flush();
        bos.close();
        final TorrentInfo torrent = TorrentInfo.bdecode(bencoded_torrent_bytes);
        GUIMediator.safeInvokeLater(new Runnable() {

            public void run() {
                if (uiTorrentMakerListener != null) {
                    uiTorrentMakerListener.beforeOpenForSeedInUIThread();
                }
                GUIMediator.instance().openTorrentForSeed(torrentFile, file.getParentFile());
                if (showShareTorrentDialog) {
                    new ShareTorrentDialog(GUIMediator.getAppFrame(), torrent).setVisible(true);
                }
            }
        });
    } catch (final Exception e) {
        e.printStackTrace();
        if (uiTorrentMakerListener != null) {
            uiTorrentMakerListener.onException();
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) TorrentInfo(com.frostwire.jlibtorrent.TorrentInfo) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 8 with TorrentInfo

use of com.frostwire.jlibtorrent.TorrentInfo in project frostwire by frostwire.

the class BTDownload method getETA.

public long getETA() {
    if (!th.isValid()) {
        return 0;
    }
    TorrentInfo ti = th.torrentFile();
    if (ti == null) {
        return 0;
    }
    TorrentStatus status = th.status();
    long left = ti.totalSize() - status.totalDone();
    long rate = status.downloadPayloadRate();
    if (left <= 0) {
        return 0;
    }
    if (rate <= 0) {
        return -1;
    }
    return left / rate;
}
Also used : TorrentStatus(com.frostwire.jlibtorrent.TorrentStatus) TorrentInfo(com.frostwire.jlibtorrent.TorrentInfo)

Example 9 with TorrentInfo

use of com.frostwire.jlibtorrent.TorrentInfo in project frostwire by frostwire.

the class BTDownload method getItems.

@Override
public List<TransferItem> getItems() {
    ArrayList<TransferItem> items = new ArrayList<>();
    if (th.isValid()) {
        TorrentInfo ti = th.torrentFile();
        if (ti != null && ti.isValid()) {
            FileStorage fs = ti.files();
            int numFiles = ti.numFiles();
            for (int i = 0; i < numFiles; i++) {
                BTDownloadItem item = new BTDownloadItem(th, i, fs.filePath(i), fs.fileSize(i), piecesTracker);
                items.add(item);
            }
            if (piecesTracker != null) {
                int numPieces = ti.numPieces();
                // perform piece complete check
                for (int i = 0; i < numPieces; i++) {
                    if (th.havePiece(i)) {
                        piecesTracker.setComplete(i, true);
                    }
                }
            }
        }
    }
    return items;
}
Also used : ArrayList(java.util.ArrayList) FileStorage(com.frostwire.jlibtorrent.FileStorage) TorrentInfo(com.frostwire.jlibtorrent.TorrentInfo) TransferItem(com.frostwire.transfers.TransferItem)

Example 10 with TorrentInfo

use of com.frostwire.jlibtorrent.TorrentInfo in project frostwire by frostwire.

the class BTDownload method getPredominantFileExtension.

@Override
public String getPredominantFileExtension() {
    if (predominantFileExtension == null && th != null) {
        TorrentInfo torrentInfo = th.torrentFile();
        if (torrentInfo != null) {
            FileStorage files = torrentInfo.files();
            Map<String, Long> extensionByteSums = new HashMap<>();
            int numFiles = files.numFiles();
            if (files.paths() != null) {
                for (int i = 0; i < numFiles; i++) {
                    String path = files.filePath(i);
                    String extension = FilenameUtils.getExtension(path);
                    if ("".equals(extension)) {
                        // skip folders
                        continue;
                    }
                    if (extensionByteSums.containsKey(extension)) {
                        Long bytes = extensionByteSums.get(extension);
                        extensionByteSums.put(extension, bytes + files.fileSize(i));
                    } else {
                        extensionByteSums.put(extension, files.fileSize(i));
                    }
                }
                String extensionCandidate = null;
                Set<String> exts = extensionByteSums.keySet();
                for (String ext : exts) {
                    if (extensionCandidate == null) {
                        extensionCandidate = ext;
                    } else {
                        if (extensionByteSums.get(ext) > extensionByteSums.get(extensionCandidate)) {
                            extensionCandidate = ext;
                        }
                    }
                }
                predominantFileExtension = extensionCandidate;
            }
        }
    }
    return predominantFileExtension;
}
Also used : HashMap(java.util.HashMap) FileStorage(com.frostwire.jlibtorrent.FileStorage) TorrentInfo(com.frostwire.jlibtorrent.TorrentInfo)

Aggregations

TorrentInfo (com.frostwire.jlibtorrent.TorrentInfo)14 FileStorage (com.frostwire.jlibtorrent.FileStorage)4 File (java.io.File)4 Priority (com.frostwire.jlibtorrent.Priority)3 TcpEndpoint (com.frostwire.jlibtorrent.TcpEndpoint)3 Entry (com.frostwire.jlibtorrent.Entry)2 TorrentHandle (com.frostwire.jlibtorrent.TorrentHandle)2 TorrentStatus (com.frostwire.jlibtorrent.TorrentStatus)2 TransferItem (com.frostwire.transfers.TransferItem)2 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 BTDownload (com.frostwire.bittorrent.BTDownload)1 PieceIndexBitfield (com.frostwire.jlibtorrent.PieceIndexBitfield)1 com.frostwire.jlibtorrent.swig.create_torrent (com.frostwire.jlibtorrent.swig.create_torrent)1 com.frostwire.jlibtorrent.swig.error_code (com.frostwire.jlibtorrent.swig.error_code)1 com.frostwire.jlibtorrent.swig.file_storage (com.frostwire.jlibtorrent.swig.file_storage)1 com.frostwire.jlibtorrent.swig.set_piece_hashes_listener (com.frostwire.jlibtorrent.swig.set_piece_hashes_listener)1 TorrentCrawlableSearchResult (com.frostwire.search.torrent.TorrentCrawlableSearchResult)1 TorrentCrawledSearchResult (com.frostwire.search.torrent.TorrentCrawledSearchResult)1