Search in sources :

Example 1 with TorrentFile

use of com.acgist.snail.pojo.bean.TorrentFile in project snail by acgist.

the class TorrentStreamTest method testReadWrite.

@Test
void testReadWrite() throws DownloadException, IOException {
    final var path = "E:/snail/902FFAA29EE632C8DC966ED9AB573409BA9A518E.torrent";
    final var session = TorrentContext.getInstance().newTorrentSession(path);
    final var entity = new TaskEntity();
    entity.setFile("E:/snail/tmp");
    entity.setType(Type.TORRENT);
    entity.setName("acgist");
    entity.setStatus(Status.COMPLETED);
    long pos = 0L;
    TorrentFile torrentFile = null;
    final var files = session.torrent().getInfo().files();
    for (TorrentFile file : files) {
        if (file.path().endsWith("Scans/Vol.1/Box_1.png")) {
            torrentFile = file;
            break;
        }
        pos += file.getLength();
    }
    final List<String> list = new ArrayList<>();
    entity.setDescription(DescriptionWrapper.newEncoder(list).serialize());
    session.upload(TaskSession.newInstance(entity));
    final var group = session.torrentStreamGroup();
    final long pieceLength = session.torrent().getInfo().getPieceLength();
    final String sourceFile = "E:/snail/server/Scans/Vol.1/Box_1.png";
    final var oldStream = TorrentStream.newInstance(pieceLength, sourceFile, torrentFile.getLength(), pos, true, group);
    oldStream.install();
    oldStream.verify();
    final String targetFile = "E:/snail/tmp/server/Scans/Vol.1/Box_1.png";
    final var newStream = TorrentStream.newInstance(pieceLength, targetFile, torrentFile.getLength(), pos, false, group);
    newStream.install();
    newStream.verify();
    int pieceBeginIndex = (int) (pos / pieceLength);
    int pieceEndIndex = (int) ((pos + torrentFile.getLength()) / pieceLength) + 1;
    final BitSet peerPieces = new BitSet();
    peerPieces.set(pieceBeginIndex, pieceEndIndex);
    this.cost();
    for (int index = pieceBeginIndex; index < pieceEndIndex; index++) {
        var torrentPiece = newStream.pick(0, peerPieces, peerPieces);
        if (torrentPiece != null) {
            torrentPiece.write(torrentPiece.getBegin(), oldStream.read(index));
            newStream.write(torrentPiece);
        }
    }
    this.costed();
    oldStream.release();
    newStream.release();
    final var sourceHash = FileUtils.sha1(sourceFile);
    final var targetHash = FileUtils.sha1(targetFile);
    assertEquals(targetHash, sourceHash);
}
Also used : TaskEntity(com.acgist.snail.pojo.entity.TaskEntity) ArrayList(java.util.ArrayList) BitSet(java.util.BitSet) TorrentFile(com.acgist.snail.pojo.bean.TorrentFile) Test(org.junit.jupiter.api.Test)

Example 2 with TorrentFile

use of com.acgist.snail.pojo.bean.TorrentFile in project snail by acgist.

the class SnailTest method testTorrent.

@Test
void testTorrent() throws DownloadException {
    final String torrentPath = "E:\\snail\\0B156834B59B0FF64EE0C9305D4D6EDE421196E6.torrent";
    final var snail = SnailBuilder.newBuilder().enableTorrent().buildSync();
    assertNotNull(snail);
    // 解析种子文件
    final var torrent = TorrentContext.loadTorrent(torrentPath);
    // 过滤下载文件
    final var list = torrent.getInfo().files().stream().filter(TorrentFile::notPaddingFile).map(TorrentFile::path).filter(path -> path.endsWith(".mkv")).collect(Collectors.toList());
    // 设置下载文件
    GuiContext.getInstance().files(DescriptionWrapper.newEncoder(list).serialize());
    // 注册文件选择事件
    GuiContext.register(new MultifileEventAdapter());
    // 开始下载
    snail.download(torrentPath);
    snail.lockDownload();
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Socket(java.net.Socket) Performance(com.acgist.snail.utils.Performance) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) GuiContext(com.acgist.snail.context.GuiContext) DownloadException(com.acgist.snail.context.exception.DownloadException) TorrentFile(com.acgist.snail.pojo.bean.TorrentFile) Test(org.junit.jupiter.api.Test) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) SystemConfig(com.acgist.snail.config.SystemConfig) DescriptionWrapper(com.acgist.snail.pojo.wrapper.DescriptionWrapper) SnailBuilder(com.acgist.snail.Snail.SnailBuilder) MultifileEventAdapter(com.acgist.snail.gui.event.adapter.MultifileEventAdapter) TorrentContext(com.acgist.snail.context.TorrentContext) MultifileEventAdapter(com.acgist.snail.gui.event.adapter.MultifileEventAdapter) TorrentFile(com.acgist.snail.pojo.bean.TorrentFile) Test(org.junit.jupiter.api.Test)

Example 3 with TorrentFile

use of com.acgist.snail.pojo.bean.TorrentFile in project snail by acgist.

the class TorrentDownloaderTest method testTorrentDownloaderBuild.

@Test
void testTorrentDownloaderBuild() throws DownloadException {
    final String url = "E://snail/902FFAA29EE632C8DC966ED9AB573409BA9A518E.torrent";
    ProtocolContext.getInstance().register(TorrentProtocol.getInstance()).available(true);
    final var torrent = TorrentContext.loadTorrent(url);
    final var list = torrent.getInfo().files().stream().filter(TorrentFile::notPaddingFile).map(TorrentFile::path).collect(Collectors.toList());
    GuiContext.register(new MultifileEventAdapter());
    GuiContext.getInstance().files(DescriptionWrapper.newEncoder(list).serialize());
    final var taskSession = TorrentProtocol.getInstance().buildTaskSession(url);
    final var downloader = taskSession.buildDownloader();
    // downloader.run(); // 不下载
    assertNotNull(downloader);
    final var file = new File(taskSession.getFile());
    assertTrue(file.exists());
    FileUtils.delete(taskSession.getFile());
    taskSession.delete();
}
Also used : MultifileEventAdapter(com.acgist.snail.gui.event.adapter.MultifileEventAdapter) TorrentFile(com.acgist.snail.pojo.bean.TorrentFile) File(java.io.File) TorrentFile(com.acgist.snail.pojo.bean.TorrentFile) Test(org.junit.jupiter.api.Test)

Example 4 with TorrentFile

use of com.acgist.snail.pojo.bean.TorrentFile in project snail by acgist.

the class TorrentDownloaderTest method testTorrentDownloader.

@Test
void testTorrentDownloader() throws DownloadException {
    TorrentInitializer.newInstance().sync();
    final String url = "E://snail/902FFAA29EE632C8DC966ED9AB573409BA9A518E.torrent";
    ProtocolContext.getInstance().register(TorrentProtocol.getInstance()).available(true);
    final var torrent = TorrentContext.loadTorrent(url);
    final var list = torrent.getInfo().files().stream().filter(TorrentFile::notPaddingFile).map(TorrentFile::path).collect(Collectors.toList());
    GuiContext.register(new MultifileEventAdapter());
    GuiContext.getInstance().files(DescriptionWrapper.newEncoder(list).serialize());
    final var taskSession = TorrentProtocol.getInstance().buildTaskSession(url);
    final var downloader = taskSession.buildDownloader();
    downloader.run();
    final var file = new File(taskSession.getFile());
    assertTrue(file.exists());
    assertTrue(ArrayUtils.isNotEmpty(file.list()));
    FileUtils.delete(taskSession.getFile());
    taskSession.delete();
}
Also used : MultifileEventAdapter(com.acgist.snail.gui.event.adapter.MultifileEventAdapter) TorrentFile(com.acgist.snail.pojo.bean.TorrentFile) File(java.io.File) TorrentFile(com.acgist.snail.pojo.bean.TorrentFile) Test(org.junit.jupiter.api.Test)

Example 5 with TorrentFile

use of com.acgist.snail.pojo.bean.TorrentFile in project snail by acgist.

the class TorrentSession method buildSelectedFiles.

/**
 * <p>获取选择下载文件列表</p>
 *
 * @return 选择下载文件列表
 */
private List<TorrentFile> buildSelectedFiles() {
    // 切记不可排除填充文件
    final List<TorrentFile> torrentFiles = this.torrent.getInfo().files();
    final List<String> selectedFiles = this.taskSession.multifileSelected();
    for (TorrentFile torrentFile : torrentFiles) {
        torrentFile.selected(selectedFiles.contains(torrentFile.path()));
    }
    return torrentFiles;
}
Also used : TorrentFile(com.acgist.snail.pojo.bean.TorrentFile)

Aggregations

TorrentFile (com.acgist.snail.pojo.bean.TorrentFile)5 Test (org.junit.jupiter.api.Test)4 MultifileEventAdapter (com.acgist.snail.gui.event.adapter.MultifileEventAdapter)3 File (java.io.File)2 SnailBuilder (com.acgist.snail.Snail.SnailBuilder)1 SystemConfig (com.acgist.snail.config.SystemConfig)1 GuiContext (com.acgist.snail.context.GuiContext)1 TorrentContext (com.acgist.snail.context.TorrentContext)1 DownloadException (com.acgist.snail.context.exception.DownloadException)1 TaskEntity (com.acgist.snail.pojo.entity.TaskEntity)1 DescriptionWrapper (com.acgist.snail.pojo.wrapper.DescriptionWrapper)1 Performance (com.acgist.snail.utils.Performance)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 Collectors (java.util.stream.Collectors)1 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)1 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)1