Search in sources :

Example 6 with DownloadException

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);
}
Also used : DownloadException(com.acgist.snail.context.exception.DownloadException) File(java.io.File)

Example 7 with DownloadException

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();
    }
}
Also used : NetException(com.acgist.snail.context.exception.NetException) DownloadException(com.acgist.snail.context.exception.DownloadException) FtpClient(com.acgist.snail.net.ftp.FtpClient)

Example 8 with DownloadException

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;
    }
}
Also used : DownloadException(com.acgist.snail.context.exception.DownloadException)

Example 9 with DownloadException

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();
}
Also used : TorrentBuilder(com.acgist.snail.protocol.magnet.TorrentBuilder) DownloadException(com.acgist.snail.context.exception.DownloadException)

Example 10 with DownloadException

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;
}
Also used : Logger(com.acgist.snail.logger.Logger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DownloadException(com.acgist.snail.context.exception.DownloadException) ArrayList(java.util.ArrayList) IDownloader(com.acgist.snail.downloader.IDownloader) ITaskSession(com.acgist.snail.pojo.ITaskSession) List(java.util.List) Snail(com.acgist.snail.Snail) LoggerFactory(com.acgist.snail.logger.LoggerFactory) Optional(java.util.Optional) Protocol(com.acgist.snail.protocol.Protocol) IContext(com.acgist.snail.IContext) StringUtils(com.acgist.snail.utils.StringUtils) DownloadException(com.acgist.snail.context.exception.DownloadException) Protocol(com.acgist.snail.protocol.Protocol) IDownloader(com.acgist.snail.downloader.IDownloader)

Aggregations

DownloadException (com.acgist.snail.context.exception.DownloadException)14 ITaskSession (com.acgist.snail.pojo.ITaskSession)3 File (java.io.File)3 IOException (java.io.IOException)3 Test (org.junit.jupiter.api.Test)3 NetException (com.acgist.snail.context.exception.NetException)2 Performance (com.acgist.snail.utils.Performance)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)2 IContext (com.acgist.snail.IContext)1 Snail (com.acgist.snail.Snail)1 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 IDownloader (com.acgist.snail.downloader.IDownloader)1 MultifileEventAdapter (com.acgist.snail.gui.event.adapter.MultifileEventAdapter)1 Logger (com.acgist.snail.logger.Logger)1 LoggerFactory (com.acgist.snail.logger.LoggerFactory)1