Search in sources :

Example 11 with DownloadException

use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.

the class TorrentContext method loadTorrent.

/**
 * <p>种子文件加载</p>
 *
 * @param path 种子文件地址
 *
 * @return 种子信息
 *
 * @throws DownloadException 下载异常
 */
public static final Torrent loadTorrent(String path) throws DownloadException {
    final File file = new File(path);
    if (!file.exists()) {
        throw new DownloadException("不存在的种子文件");
    }
    try {
        final var bytes = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
        final var decoder = BEncodeDecoder.newInstance(bytes).next();
        if (decoder.isEmpty()) {
            throw new DownloadException("种子文件格式错误");
        }
        final var torrent = Torrent.valueOf(decoder);
        // 直接转储原始信息:防止顺序不对导致种子Hash计算错误
        final var info = decoder.getMap(Torrent.ATTR_INFO);
        final var infoHash = InfoHash.newInstance(BEncodeEncoder.encodeMap(info));
        torrent.infoHash(infoHash);
        return torrent;
    } catch (NetException | IOException e) {
        throw new DownloadException("种子文件加载失败", e);
    }
}
Also used : NetException(com.acgist.snail.context.exception.NetException) DownloadException(com.acgist.snail.context.exception.DownloadException) IOException(java.io.IOException) File(java.io.File)

Example 12 with DownloadException

use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.

the class SingleFileDownloader method buildOutput.

/**
 * <p>新建{@linkplain #output 输出流}</p>
 * <p>通过判断任务已经下载大小验证是否支持断点续传</p>
 *
 * @throws DownloadException 下载异常
 */
protected void buildOutput() throws DownloadException {
    try {
        final long size = this.taskSession.downloadSize();
        final int bufferSize = DownloadConfig.getMemoryBufferByte(this.taskSession.getSize());
        OutputStream outputStream;
        if (size > 0L) {
            // 支持断点续传
            outputStream = new FileOutputStream(this.taskSession.getFile(), true);
        } else {
            // 不支持断点续传
            outputStream = new FileOutputStream(this.taskSession.getFile());
        }
        final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream, bufferSize);
        this.output = Channels.newChannel(bufferedOutputStream);
    } catch (FileNotFoundException e) {
        throw new DownloadException("下载文件打开失败", e);
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) DownloadException(com.acgist.snail.context.exception.DownloadException) FileNotFoundException(java.io.FileNotFoundException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 13 with DownloadException

use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.

the class TsLinkerTest method testLink.

@Test
void testLink() throws DownloadException {
    final String name = "测试";
    final String filePath = "E:\\snail\\m3u8";
    final File parent = new File(filePath);
    final var links = List.of(parent.listFiles()).stream().map(file -> file.getAbsolutePath()).filter(// 排除生成文件:防止重复读写
    path -> !path.contains(name)).collect(Collectors.toList());
    Cipher cipher = null;
    // final var builder = M3u8Builder.newInstance(new File("E://snail/index.m3u8"), "https://www.acgist.com/a/b?v=1234");
    // cipher = builder.build().getCipher();
    final TsLinker linker = TsLinker.newInstance(name, filePath, cipher, links);
    linker.link();
    File file = Paths.get(filePath, "测试.ts").toFile();
    assertTrue(file.exists());
    FileUtils.delete(file);
}
Also used : Test(org.junit.jupiter.api.Test) List(java.util.List) FileUtils(com.acgist.snail.utils.FileUtils) Paths(java.nio.file.Paths) Performance(com.acgist.snail.utils.Performance) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Collectors(java.util.stream.Collectors) Cipher(javax.crypto.Cipher) DownloadException(com.acgist.snail.context.exception.DownloadException) File(java.io.File) Cipher(javax.crypto.Cipher) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 14 with DownloadException

use of com.acgist.snail.context.exception.DownloadException in project snail by acgist.

the class Protocol method buildTaskSession.

/**
 * <p>新建下载任务</p>
 *
 * @param url 下载链接
 *
 * @return 任务信息
 *
 * @throws DownloadException 下载异常
 */
public synchronized ITaskSession buildTaskSession(String url) throws DownloadException {
    this.url = url.strip();
    boolean success = true;
    try {
        this.buildTaskEntity();
        return TaskSession.newInstance(this.taskEntity);
    } catch (DownloadException e) {
        success = false;
        throw e;
    } catch (Exception e) {
        success = false;
        throw new DownloadException("下载失败", e);
    } finally {
        if (success) {
            this.success();
        }
        this.release(success);
    }
}
Also used : DownloadException(com.acgist.snail.context.exception.DownloadException) DownloadException(com.acgist.snail.context.exception.DownloadException)

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