use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.
the class Protocol method buildFile.
/**
* <p>设置下载文件、目录</p>
*
* @param fileName 文件名称
*
* @throws DownloadException 下载异常
*/
protected void buildFile(String fileName) throws DownloadException {
final String filePath = DownloadConfig.getPath(fileName);
final File file = new File(filePath);
if (file.exists()) {
throw new DownloadException("下载文件已经存在:" + file);
}
this.taskEntity.setFile(filePath);
}
use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.
the class FtpProtocol method buildSize.
@Override
protected void buildSize() throws DownloadException {
final FtpClient client = FtpClient.newInstance(this.url);
try {
client.connect();
final long size = client.size();
this.taskEntity.setSize(size);
} catch (NetException e) {
throw new DownloadException(e);
} finally {
client.close();
}
}
use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.
the class HlsProtocol method buildM3u8.
/**
* <p>获取M3U8信息</p>
*
* @throws NetException 网络异常
* @throws DownloadException 下载异常
*/
private void buildM3u8() throws NetException, DownloadException {
final var response = HttpClient.newInstance(this.url).get().responseToString();
final var m3u8Check = M3u8Builder.newInstance(response, this.url).build();
if (m3u8Check.getType() == M3u8.Type.M3U8) {
this.url = m3u8Check.maxRateLink();
this.buildM3u8();
} else if (m3u8Check.getType() == M3u8.Type.STREAM) {
throw new DownloadException("不支持直播流媒体下载");
} else {
this.m3u8 = m3u8Check;
}
}
use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.
the class TorrentSession method saveTorrent.
/**
* <p>保存种子文件</p>
* <p>重新并加载种子文件和InfoHash</p>
*/
public void saveTorrent() {
final TorrentBuilder builder = TorrentBuilder.newInstance(this.infoHash, this.trackerLauncherGroup.trackers());
final String torrentFilePath = builder.buildFile(this.taskSession.downloadFolder().getAbsolutePath());
try {
this.torrent = TorrentContext.loadTorrent(torrentFilePath);
this.infoHash = this.torrent.infoHash();
} catch (DownloadException e) {
LOGGER.error("加载种子异常:{}", torrentFilePath, e);
}
final long torrentFileSize = FileUtils.fileSize(torrentFilePath);
this.taskSession.setTorrent(torrentFilePath);
this.taskSession.setSize(torrentFileSize);
this.taskSession.downloadSize(torrentFileSize);
this.taskSession.update();
this.checkCompletedAndDone();
}
use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.
the class ProtocolContext method buildDownloader.
/**
* <p>新建下载器</p>
*
* @param taskSession 任务信息
*
* @return 下载器
*
* @throws DownloadException 下载异常
*/
public IDownloader buildDownloader(ITaskSession taskSession) throws DownloadException {
final var type = taskSession.getType();
final Optional<Protocol> optional = this.protocols.stream().filter(Protocol::available).filter(protocol -> protocol.type() == type).findFirst();
if (optional.isEmpty()) {
throw new DownloadException("不支持的下载类型:" + type);
}
final IDownloader downloader = optional.get().buildDownloader(taskSession);
if (downloader == null) {
throw new DownloadException("不支持的下载类型:" + type);
}
return downloader;
}
Aggregations