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);
}
}
});
}
}
}
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();
}
}
}
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;
}
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;
}
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;
}
Aggregations