Search in sources :

Example 1 with FileCollectionStorage

use of com.turn.ttorrent.client.storage.FileCollectionStorage in project teamcity-torrent-plugin by JetBrains.

the class TeamcityTorrentClient method downloadAndShareOrFail.

public void downloadAndShareOrFail(@NotNull final File torrentFile, @NotNull final List<String> fileNames, @NotNull final String hexInfoHash, @NotNull final File destFile, @NotNull final File destDir, @NotNull final FileProgress fileDownloadProgress, final int downloadTimeoutMs, final int minSeedersCount, final int maxTimeoutForConnect) throws Exception {
    checkThatTorrentContainsFile(fileNames, destFile);
    destDir.mkdirs();
    if (myCommunicationManager.containsTorrentWithHash(hexInfoHash)) {
        LOG.info("Already seeding torrent with hash " + hexInfoHash + ". Stop seeding and try download again");
        stopSeeding(torrentFile);
    }
    LOG.info(String.format("Will attempt to download uninterruptibly %s into %s. Timeout:%d", destFile.getAbsolutePath(), destDir.getAbsolutePath(), downloadTimeoutMs));
    TorrentMetadataProvider metadataProvider = new FileMetadataProvider(torrentFile.getAbsolutePath());
    TorrentMetadata metadata = metadataProvider.getTorrentMetadata();
    FileCollectionStorage fileCollectionStorage = FileCollectionStorage.create(metadata, destDir);
    PieceStorage pieceStorage = EmptyPieceStorageFactory.INSTANCE.createStorage(metadata, fileCollectionStorage);
    TorrentDownloader torrentDownloader = new TorrentDownloader(metadata, fileDownloadProgress, minSeedersCount, maxTimeoutForConnect, downloadTimeoutMs);
    myCommunicationManager.addTorrent(metadataProvider, pieceStorage, Collections.<TorrentListener>singletonList(torrentDownloader));
    Exception exception = null;
    try {
        torrentDownloader.awaitDownload();
    } catch (Exception e) {
        exception = e;
    }
    try {
        myCommunicationManager.removeTorrent(metadata.getHexInfoHash());
        pieceStorage.close();
    } finally {
        boolean downloadFailed = exception != null;
        if (downloadFailed) {
            fileCollectionStorage.delete();
            throw exception;
        }
    }
}
Also used : FileCollectionStorage(com.turn.ttorrent.client.storage.FileCollectionStorage) PieceStorage(com.turn.ttorrent.client.storage.PieceStorage) TorrentMetadata(com.turn.ttorrent.common.TorrentMetadata) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

FileCollectionStorage (com.turn.ttorrent.client.storage.FileCollectionStorage)1 PieceStorage (com.turn.ttorrent.client.storage.PieceStorage)1 TorrentMetadata (com.turn.ttorrent.common.TorrentMetadata)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1