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