Search in sources :

Example 1 with FileStorage

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

the class BTDownload method getIncompleteFiles.

public Set<File> getIncompleteFiles() {
    Set<File> s = new HashSet<>();
    try {
        if (!th.isValid()) {
            return s;
        }
        long[] progress = th.fileProgress(TorrentHandle.FileProgressFlags.PIECE_GRANULARITY);
        TorrentInfo ti = th.torrentFile();
        if (ti == null) {
            // still downloading the info (from magnet)
            return s;
        }
        FileStorage fs = ti.files();
        String prefix = savePath.getAbsolutePath();
        long createdTime = created.getTime();
        for (int i = 0; i < progress.length; i++) {
            String fePath = fs.filePath(i);
            long feSize = fs.fileSize(i);
            if (progress[i] < feSize) {
                // lets see if indeed the file is incomplete
                File f = new File(prefix, fePath);
                if (!f.exists()) {
                    // nothing to do here
                    continue;
                }
                if (f.lastModified() >= createdTime) {
                    // we have a file modified (supposedly) by this transfer
                    s.add(f);
                }
            }
        }
    } catch (Throwable e) {
        LOG.error("Error calculating the incomplete files set", e);
    }
    return s;
}
Also used : FileStorage(com.frostwire.jlibtorrent.FileStorage) TorrentInfo(com.frostwire.jlibtorrent.TorrentInfo) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with FileStorage

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

the class PerformersHelper method crawlTorrent.

/**
 * This method is only public allow reuse inside the package search, consider it a private API
 */
public static List<? extends SearchResult> crawlTorrent(SearchPerformer performer, TorrentCrawlableSearchResult sr, byte[] data, boolean detectAlbums) {
    List<TorrentCrawledSearchResult> list = new LinkedList<>();
    if (data == null) {
        return list;
    }
    TorrentInfo ti;
    try {
        ti = TorrentInfo.bdecode(data);
    } catch (Throwable t) {
        // LOG.error("Can't bdecode:\n" + new String(data) + "\n\n");
        throw t;
    }
    int numFiles = ti.numFiles();
    FileStorage fs = ti.files();
    for (int i = 0; !performer.isStopped() && i < numFiles; i++) {
        // TODO: Check for the hidden attribute
        if (fs.padFileAt(i)) {
            continue;
        }
        list.add(new TorrentCrawledSearchResult(sr, ti, i, fs.filePath(i), fs.fileSize(i)));
    }
    if (detectAlbums) {
        List<SearchResult> temp = new LinkedList<>();
        temp.addAll(list);
        temp.addAll(new AlbumCluster().detect(sr, list));
        return temp;
    } else {
        return list;
    }
}
Also used : TorrentCrawledSearchResult(com.frostwire.search.torrent.TorrentCrawledSearchResult) FileStorage(com.frostwire.jlibtorrent.FileStorage) TorrentCrawlableSearchResult(com.frostwire.search.torrent.TorrentCrawlableSearchResult) TorrentCrawledSearchResult(com.frostwire.search.torrent.TorrentCrawledSearchResult) TorrentInfo(com.frostwire.jlibtorrent.TorrentInfo) LinkedList(java.util.LinkedList)

Example 3 with FileStorage

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

the class TorrentFetcherDownload method calculateSelection.

private boolean[] calculateSelection(TorrentInfo ti, String path) {
    boolean[] selection = new boolean[ti.numFiles()];
    FileStorage fs = ti.files();
    for (int i = 0; i < selection.length; i++) {
        String filePath = fs.filePath(i);
        if (path.endsWith(filePath) || filePath.endsWith(path)) {
            selection[i] = true;
        }
    }
    return selection;
}
Also used : FileStorage(com.frostwire.jlibtorrent.FileStorage) TcpEndpoint(com.frostwire.jlibtorrent.TcpEndpoint)

Example 4 with FileStorage

use of com.frostwire.jlibtorrent.FileStorage 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 5 with FileStorage

use of com.frostwire.jlibtorrent.FileStorage 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

FileStorage (com.frostwire.jlibtorrent.FileStorage)6 TorrentInfo (com.frostwire.jlibtorrent.TorrentInfo)4 TcpEndpoint (com.frostwire.jlibtorrent.TcpEndpoint)1 TorrentCrawlableSearchResult (com.frostwire.search.torrent.TorrentCrawlableSearchResult)1 TorrentCrawledSearchResult (com.frostwire.search.torrent.TorrentCrawledSearchResult)1 TransferItem (com.frostwire.transfers.TransferItem)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1