Search in sources :

Example 46 with TOTorrent

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

the class DownloadStubImpl method remove.

@Override
public void remove(boolean delete_torrent, boolean delete_data) throws DownloadException, DownloadRemovalVetoException {
    if (delete_data) {
        TOTorrent torrent = manager.getTorrent(this);
        if (torrent != null) {
            File save_location = new File(getSavePath());
            if (torrent.isSimpleTorrent()) {
                if (save_location.isFile()) {
                    FileUtil.deleteWithRecycle(save_location, false);
                }
            } else {
                if (save_location.isDirectory()) {
                    DownloadStubFile[] files = getStubFiles();
                    String save_path = save_location.getAbsolutePath();
                    if (!save_path.endsWith(File.separator)) {
                        save_path += File.separator;
                    }
                    int found = 0;
                    for (DownloadStubFile file : files) {
                        File f = file.getFile();
                        String path = f.getAbsolutePath();
                        if (path.startsWith(save_path)) {
                            if (f.exists()) {
                                found++;
                            }
                        }
                    }
                    int actual = countFiles(save_location);
                    if (actual == found) {
                        FileUtil.deleteWithRecycle(save_location, false);
                    } else {
                        for (DownloadStubFile file : files) {
                            File f = file.getFile();
                            String path = f.getAbsolutePath();
                            if (path.startsWith(save_path)) {
                                FileUtil.deleteWithRecycle(f, false);
                            }
                        }
                        TorrentUtils.recursiveEmptyDirDelete(save_location, false);
                    }
                }
            }
        }
    }
    if (delete_torrent) {
        byte[] bytes = (byte[]) gm_map.get("torrent");
        if (bytes != null) {
            try {
                String torrent_file = new String(bytes, "UTF-8");
                File file = new File(torrent_file);
                TorrentUtils.delete(file, false);
            } catch (Throwable e) {
                Debug.out(e);
            }
        }
    }
    manager.remove(this);
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent) File(java.io.File)

Example 47 with TOTorrent

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

the class ExternalSeedReaderGetRight method setupDownloaders.

private void setupDownloaders() {
    synchronized (this) {
        if (http_downloaders != null) {
            return;
        }
        TOTorrent to_torrent = ((TorrentImpl) getTorrent()).getTorrent();
        String ua = getUserAgent();
        if (to_torrent.isSimpleTorrent()) {
            http_downloaders = new ExternalSeedHTTPDownloader[] { linear_download ? new ExternalSeedHTTPDownloaderLinear(url, ua) : new ExternalSeedHTTPDownloaderRange(url, ua) };
            downloader_offsets = new long[] { 0 };
            downloader_lengths = new long[] { to_torrent.getSize() };
        } else {
            TOTorrentFile[] files = to_torrent.getFiles();
            http_downloaders = new ExternalSeedHTTPDownloader[files.length];
            downloader_offsets = new long[files.length];
            downloader_lengths = new long[files.length];
            long offset = 0;
            // encoding is a problem, assume ISO-8859-1
            String base_url = url.toString();
            if (base_url.endsWith("/")) {
                base_url = base_url.substring(0, base_url.length() - 1);
            }
            try {
                base_url += "/" + URLEncoder.encode(new String(to_torrent.getName(), "ISO-8859-1"), "ISO-8859-1").replaceAll("\\+", "%20");
                for (int i = 0; i < files.length; i++) {
                    TOTorrentFile file = files[i];
                    long length = file.getLength();
                    String file_url_str = base_url;
                    byte[][] bits = file.getPathComponents();
                    for (int j = 0; j < bits.length; j++) {
                        file_url_str += "/" + URLEncoder.encode(new String(bits[j], "ISO-8859-1"), "ISO-8859-1").replaceAll("\\+", "%20");
                    }
                    http_downloaders[i] = linear_download ? new ExternalSeedHTTPDownloaderLinear(new URL(file_url_str), ua) : new ExternalSeedHTTPDownloaderRange(new URL(file_url_str), ua);
                    downloader_offsets[i] = offset;
                    downloader_lengths[i] = length;
                    offset += length;
                }
            } catch (Throwable e) {
                Debug.out(e);
            }
        }
    }
}
Also used : ExternalSeedHTTPDownloaderRange(com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloaderRange) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) TorrentImpl(com.biglybt.pifimpl.local.torrent.TorrentImpl) TOTorrent(com.biglybt.core.torrent.TOTorrent) ExternalSeedHTTPDownloaderLinear(com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloaderLinear) URL(java.net.URL)

Example 48 with TOTorrent

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

the class MagnetPlugin method recoverDownloads.

private void recoverDownloads() {
    Map<String, Map> active;
    synchronized (download_activities) {
        active = COConfigurationManager.getMapParameter("MagnetPlugin.active.magnets", new HashMap());
        if (active.size() > 0) {
            active = BEncoder.cloneMap(active);
            COConfigurationManager.setParameter("MagnetPlugin.active.magnets", new HashMap());
        }
    }
    boolean recover = magnet_recovery.getValue();
    if (recover) {
        for (Map map : active.values()) {
            try {
                byte[] hash = (byte[]) map.get("hash");
                String args = new String((byte[]) map.get("args"), "UTF-8");
                InetSocketAddress[] sources = new InetSocketAddress[0];
                List<Map> l_sources = (List<Map>) map.get("sources");
                if (l_sources != null && !l_sources.isEmpty()) {
                    List<InetSocketAddress> l_ias = new ArrayList<>();
                    for (Map m : l_sources) {
                        try {
                            int port = ((Number) m.get("port")).intValue();
                            if (map.containsKey("host")) {
                                String unresolved_host = new String((byte[]) map.get("host"), "UTF-8");
                                l_ias.add(InetSocketAddress.createUnresolved(unresolved_host, port));
                            } else {
                                byte[] address = (byte[]) map.get("address");
                                l_ias.add(new InetSocketAddress(InetAddress.getByAddress(address), port));
                            }
                        } catch (Throwable e) {
                            Debug.out(e);
                        }
                    }
                    sources = l_ias.toArray(new InetSocketAddress[l_ias.size()]);
                }
                long timeout = (Long) map.get("timeout");
                final InetSocketAddress[] f_sources = sources;
                new AEThread2("Magnet Recovery") {

                    public void run() {
                        try {
                            byte[] result = recoverableDownload(null, hash, args, f_sources, timeout, true);
                            if (result != null) {
                                TOTorrent torrent = TOTorrentFactory.deserialiseFromBEncodedByteArray(result);
                                String torrent_name = FileUtil.convertOSSpecificChars(TorrentUtils.getLocalisedName(torrent) + ".torrent", false);
                                File torrent_file;
                                String dir = null;
                                if (COConfigurationManager.getBooleanParameter("Save Torrent Files")) {
                                    dir = COConfigurationManager.getDirectoryParameter("General_sDefaultTorrent_Directory");
                                    if (dir != null) {
                                        if (dir.length() > 0) {
                                            File f = new File(dir);
                                            if (!f.exists()) {
                                                f.mkdirs();
                                            }
                                            if (!(f.isDirectory() && f.canWrite())) {
                                                dir = null;
                                            }
                                        } else {
                                            dir = null;
                                        }
                                    }
                                }
                                if (dir != null) {
                                    torrent_file = new File(dir, torrent_name);
                                } else {
                                    torrent_file = new File(AETemporaryFileHandler.getTempDirectory(), torrent_name);
                                }
                                if (torrent_file.exists()) {
                                    torrent_file = AETemporaryFileHandler.createTempFile();
                                }
                                torrent.serialiseToBEncodedFile(torrent_file);
                                UIFunctions uif = UIFunctionsManager.getUIFunctions();
                                TorrentOpenOptions torrentOptions = new TorrentOpenOptions();
                                torrentOptions.setDeleteFileOnCancel(true);
                                torrentOptions.sFileName = torrent_file.getAbsolutePath();
                                torrentOptions.setTorrent(torrent);
                                uif.addTorrentWithOptions(false, torrentOptions);
                            }
                        } catch (Throwable e) {
                            Debug.out(e);
                        }
                    }
                }.start();
            } catch (Throwable e) {
                Debug.out(e);
            }
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) TorrentOpenOptions(com.biglybt.core.torrent.impl.TorrentOpenOptions) UIFunctions(com.biglybt.ui.UIFunctions) TOTorrent(com.biglybt.core.torrent.TOTorrent) ShareResourceFile(com.biglybt.pif.sharing.ShareResourceFile) File(java.io.File)

Example 49 with TOTorrent

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

the class MagnetPlugin method addTrackersAndWebSeedsEtc.

private byte[] addTrackersAndWebSeedsEtc(byte[] torrent_data, String args, Set<String> networks) {
    List<String> new_web_seeds = new ArrayList<>();
    List<String> new_trackers = new ArrayList<>();
    Set<String> tags = new HashSet<>();
    if (args != null) {
        String[] bits = args.split("&");
        for (String bit : bits) {
            String[] x = bit.split("=");
            if (x.length == 2) {
                String lhs = x[0].toLowerCase();
                if (lhs.equals("ws")) {
                    try {
                        new_web_seeds.add(new URL(UrlUtils.decode(x[1])).toExternalForm());
                    } catch (Throwable e) {
                    }
                } else if (lhs.equals("tr")) {
                    try {
                        new_trackers.add(new URL(UrlUtils.decode(x[1])).toExternalForm());
                    } catch (Throwable e) {
                    }
                } else if (lhs.equals("tag")) {
                    tags.add(UrlUtils.decode(x[1]));
                }
            }
        }
    }
    if (new_web_seeds.size() > 0 || new_trackers.size() > 0 || networks.size() > 0) {
        try {
            TOTorrent torrent = TOTorrentFactory.deserialiseFromBEncodedByteArray(torrent_data);
            boolean update_torrent = false;
            if (new_web_seeds.size() > 0) {
                Object obj = torrent.getAdditionalProperty("url-list");
                List<String> existing = new ArrayList<>();
                if (obj instanceof byte[]) {
                    try {
                        new_web_seeds.remove(new URL(new String((byte[]) obj, "UTF-8")).toExternalForm());
                    } catch (Throwable e) {
                    }
                } else if (obj instanceof List) {
                    List<byte[]> l = (List<byte[]>) obj;
                    for (byte[] b : l) {
                        try {
                            existing.add(new URL(new String((byte[]) b, "UTF-8")).toExternalForm());
                        } catch (Throwable e) {
                        }
                    }
                }
                boolean update_ws = false;
                for (String e : new_web_seeds) {
                    if (!existing.contains(e)) {
                        existing.add(e);
                        update_ws = true;
                    }
                }
                if (update_ws) {
                    List<byte[]> l = new ArrayList<>();
                    for (String s : existing) {
                        l.add(s.getBytes("UTF-8"));
                    }
                    torrent.setAdditionalProperty("url-list", l);
                    update_torrent = true;
                }
            }
            if (new_trackers.size() > 0) {
                URL announce_url = torrent.getAnnounceURL();
                new_trackers.remove(announce_url.toExternalForm());
                TOTorrentAnnounceURLGroup group = torrent.getAnnounceURLGroup();
                TOTorrentAnnounceURLSet[] sets = group.getAnnounceURLSets();
                for (TOTorrentAnnounceURLSet set : sets) {
                    URL[] set_urls = set.getAnnounceURLs();
                    for (URL set_url : set_urls) {
                        new_trackers.remove(set_url.toExternalForm());
                    }
                }
                if (new_trackers.size() > 0) {
                    TOTorrentAnnounceURLSet[] new_sets = new TOTorrentAnnounceURLSet[sets.length + new_trackers.size()];
                    System.arraycopy(sets, 0, new_sets, 0, sets.length);
                    for (int i = 0; i < new_trackers.size(); i++) {
                        TOTorrentAnnounceURLSet new_set = group.createAnnounceURLSet(new URL[] { new URL(new_trackers.get(i)) });
                        new_sets[i + sets.length] = new_set;
                    }
                    group.setAnnounceURLSets(new_sets);
                    update_torrent = true;
                }
            }
            if (networks.size() > 0) {
                TorrentUtils.setNetworkCache(torrent, new ArrayList<>(networks));
                update_torrent = true;
            }
            if (tags.size() > 0) {
                TorrentUtils.setTagCache(torrent, new ArrayList<>(tags));
                update_torrent = true;
            }
            if (update_torrent) {
                torrent_data = BEncoder.encode(torrent.serialiseToMap());
            }
        } catch (Throwable e) {
        }
    }
    return (torrent_data);
}
Also used : URL(java.net.URL) TOTorrentAnnounceURLGroup(com.biglybt.core.torrent.TOTorrentAnnounceURLGroup) TOTorrent(com.biglybt.core.torrent.TOTorrent) TOTorrentAnnounceURLSet(com.biglybt.core.torrent.TOTorrentAnnounceURLSet)

Example 50 with TOTorrent

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

the class MagnetPlugin method getSecondaryLookupResult.

protected byte[] getSecondaryLookupResult(final Object[] result) throws ResourceDownloaderException {
    if (result == null) {
        return (null);
    }
    Object x;
    synchronized (result) {
        x = result[0];
        result[0] = null;
    }
    if (x instanceof InputStream) {
        InputStream is = (InputStream) x;
        try {
            TOTorrent t = TOTorrentFactory.deserialiseFromBEncodedInputStream(is);
            TorrentUtils.setPeerCacheValid(t);
            return (BEncoder.encode(t.serialiseToMap()));
        } catch (Throwable e) {
        }
    } else if (x instanceof ResourceDownloaderException) {
        throw ((ResourceDownloaderException) x);
    }
    return (null);
}
Also used : InputStream(java.io.InputStream) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Aggregations

TOTorrent (com.biglybt.core.torrent.TOTorrent)123 DownloadManager (com.biglybt.core.download.DownloadManager)34 File (java.io.File)32 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)29 URL (java.net.URL)25 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)15 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)13 IOException (java.io.IOException)13 GlobalManager (com.biglybt.core.global.GlobalManager)10 VuzeFile (com.biglybt.core.vuzefile.VuzeFile)10 Torrent (com.biglybt.pif.torrent.Torrent)9 DownloadManagerState (com.biglybt.core.download.DownloadManagerState)8 TOTorrentAnnounceURLSet (com.biglybt.core.torrent.TOTorrentAnnounceURLSet)8 MessageBoxShell (com.biglybt.ui.swt.shells.MessageBoxShell)8 List (java.util.List)8 Map (java.util.Map)7 Core (com.biglybt.core.Core)6 DiskManagerFileInfoSet (com.biglybt.core.disk.DiskManagerFileInfoSet)6 Download (com.biglybt.pif.download.Download)6 TorrentImpl (com.biglybt.pifimpl.local.torrent.TorrentImpl)6