Search in sources :

Example 1 with TaskInfo

use of lee.study.down.model.TaskInfo in project proxyee-down by monkeyWie.

the class AbstractHttpDownBootstrap method delete.

public void delete(boolean delFile) throws Exception {
    TaskInfo taskInfo = httpDownInfo.getTaskInfo();
    // 删除任务进度记录文件
    synchronized (taskInfo) {
        close();
        timeoutCheckTask.delBoot(httpDownInfo.getTaskInfo().getId());
        FileUtil.deleteIfExists(taskInfo.buildTaskRecordFilePath());
        FileUtil.deleteIfExists(taskInfo.buildTaskRecordBakFilePath());
        if (delFile) {
            FileUtil.deleteIfExists(taskInfo.buildTaskFilePath());
        }
        if (callback != null) {
            callback.onDelete(httpDownInfo);
        }
    }
}
Also used : TaskInfo(lee.study.down.model.TaskInfo)

Example 2 with TaskInfo

use of lee.study.down.model.TaskInfo in project proxyee-down by monkeyWie.

the class AbstractHttpDownBootstrap method startDown.

public void startDown() throws Exception {
    TaskInfo taskInfo = httpDownInfo.getTaskInfo();
    taskInfo.buildChunkInfoList();
    if (!FileUtil.exists(taskInfo.getFilePath())) {
        FileUtil.createDirSmart(taskInfo.getFilePath());
    }
    if (!FileUtil.canWrite(taskInfo.getFilePath())) {
        throw new BootstrapException("无权访问下载路径,请修改路径或开放目录写入权限");
    }
    // 磁盘空间不足
    if (taskInfo.getTotalSize() > FileUtil.getDiskFreeSize(taskInfo.getFilePath())) {
        throw new BootstrapException("磁盘空间不足,请修改路径");
    }
    // 有文件同名
    if (new File(taskInfo.buildTaskFilePath()).exists()) {
        throw new BootstrapException("文件名已存在,请修改文件名");
    }
    // 创建文件
    try (RandomAccessFile randomAccessFile = new RandomAccessFile(taskInfo.buildTaskFilePath(), "rw")) {
        randomAccessFile.setLength(taskInfo.getTotalSize());
    }
    // 文件下载开始回调
    taskInfo.reset();
    taskInfo.setStatus(HttpDownStatus.RUNNING);
    taskInfo.setStartTime(System.currentTimeMillis());
    for (int i = 0; i < taskInfo.getChunkInfoList().size(); i++) {
        ChunkInfo chunkInfo = taskInfo.getChunkInfoList().get(i);
        // 设置状态和时间
        chunkInfo.setStartTime(System.currentTimeMillis());
        startChunkDown(chunkInfo, HttpDownStatus.CONNECTING_NORMAL);
    }
    if (callback != null) {
        callback.onStart(httpDownInfo);
    }
    afterStart();
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) ChunkInfo(lee.study.down.model.ChunkInfo) RandomAccessFile(java.io.RandomAccessFile) BootstrapException(lee.study.down.exception.BootstrapException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 3 with TaskInfo

use of lee.study.down.model.TaskInfo in project proxyee-down by monkeyWie.

the class HttpDownUtil method getTaskInfo.

/**
 * 检测是否支持断点下载
 */
public static TaskInfo getTaskInfo(HttpRequest httpRequest, HttpHeaders resHeaders, ProxyConfig proxyConfig, SslContext clientSslCtx, NioEventLoopGroup loopGroup) throws Exception {
    HttpResponse httpResponse = null;
    if (resHeaders == null) {
        httpResponse = getResponse(httpRequest, proxyConfig, clientSslCtx, loopGroup);
        // 处理重定向
        if ((httpResponse.status().code() + "").indexOf("30") == 0) {
            // TODO 302重定向乱码 https://link.gimhoy.com/googledrive/aHR0cHM6Ly9kcml2ZS5nb29nbGUuY29tL29wZW4/aWQ9MThlVmNKeEhwaE40RUpGTUowSk10bWNXOVhCcWJhVE1k.jpg
            String redirectUrl = httpResponse.headers().get(HttpHeaderNames.LOCATION);
            HttpRequestInfo requestInfo = (HttpRequestInfo) httpRequest;
            requestInfo.headers().remove("Host");
            requestInfo.setUri(redirectUrl);
            RequestProto requestProto = ProtoUtil.getRequestProto(requestInfo);
            requestInfo.headers().set("Host", requestProto.getHost());
            requestInfo.setRequestProto(requestProto);
            return getTaskInfo(httpRequest, null, proxyConfig, clientSslCtx, loopGroup);
        }
        resHeaders = httpResponse.headers();
    }
    TaskInfo taskInfo = new TaskInfo().setId(UUID.randomUUID().toString()).setFileName(getDownFileName(httpRequest, resHeaders)).setTotalSize(getDownFileSize(resHeaders));
    // chunked编码不支持断点下载
    if (resHeaders.contains(HttpHeaderNames.CONTENT_LENGTH)) {
        if (httpResponse == null) {
            httpResponse = getResponse(httpRequest, proxyConfig, clientSslCtx, loopGroup);
        }
        // 206表示支持断点下载
        if (httpResponse.status().equals(HttpResponseStatus.PARTIAL_CONTENT)) {
            taskInfo.setSupportRange(true);
        }
    }
    return taskInfo;
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpRequestInfo(lee.study.down.model.HttpRequestInfo) RequestProto(lee.study.proxyee.util.ProtoUtil.RequestProto)

Example 4 with TaskInfo

use of lee.study.down.model.TaskInfo in project proxyee-down by monkeyWie.

the class DownContent method setUrl.

public static List<TaskInfoForm> setUrl(List<TaskInfo> taskInfoList) {
    List<TaskInfoForm> ret = new ArrayList<>();
    for (TaskInfo taskInfo : taskInfoList) {
        TaskInfoForm taskInfoForm = new TaskInfoForm();
        BeanUtils.copyProperties(taskInfo, taskInfoForm);
        HttpRequestInfo httpRequest = (HttpRequestInfo) ContentManager.DOWN.getDownInfo(taskInfo.getId()).getRequest();
        String uri = httpRequest.uri();
        String host = httpRequest.requestProto().getHost();
        String url = (uri.indexOf("/") == 0 ? host : "") + uri;
        if (url.indexOf("http://") != 0 && url.indexOf("https://") != 0) {
            url = (httpRequest.requestProto().getSsl() ? "https://" : "http://") + url;
        }
        taskInfoForm.setUrl(url);
        ret.add(taskInfoForm);
    }
    return ret;
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) ArrayList(java.util.ArrayList) HttpRequestInfo(lee.study.down.model.HttpRequestInfo) TaskInfoForm(lee.study.down.mvc.form.TaskInfoForm)

Example 5 with TaskInfo

use of lee.study.down.model.TaskInfo in project proxyee-down by monkeyWie.

the class DownContent method init.

/**
 * 从配置文件中加载信息
 */
public void init() {
    downContent = new ConcurrentHashMap<>();
    if (FileUtil.exists(HttpDownConstant.TASK_RECORD_PATH)) {
        try {
            List<HttpDownInfo> records = (List<HttpDownInfo>) ByteUtil.deserialize(HttpDownConstant.TASK_RECORD_PATH);
            for (HttpDownInfo httpDownInfo : records) {
                AbstractHttpDownBootstrap bootstrap = HttpDownBootstrapFactory.create(httpDownInfo, ContentManager.CONFIG.get().getRetryCount(), HttpDownConstant.clientSslContext, HttpDownConstant.clientLoopGroup, HttpDownConstant.httpDownCallback);
                TaskInfo taskInfo = httpDownInfo.getTaskInfo();
                if (taskInfo.getStatus() == HttpDownStatus.WAIT) {
                    continue;
                }
                // 下载未完成
                if (taskInfo.getStatus() != HttpDownStatus.DONE) {
                    String taskDetailPath = taskInfo.buildTaskRecordFilePath();
                    String taskDetailBakPath = taskInfo.buildTaskRecordBakFilePath();
                    // 存在下载进度信息则更新,否则重新下载
                    if (FileUtil.existsAny(taskDetailPath, taskDetailBakPath)) {
                        try {
                            taskInfo = (TaskInfo) ByteUtil.deserialize(taskDetailPath, taskDetailBakPath);
                            httpDownInfo.setTaskInfo(taskInfo);
                        } catch (IOException | ClassNotFoundException e) {
                            taskInfo.reset();
                        }
                    } else {
                        taskInfo.reset();
                    }
                    if (taskInfo.getStatus() != HttpDownStatus.FAIL) {
                        // 设置为暂停状态
                        taskInfo.setStatus(HttpDownStatus.PAUSE);
                        taskInfo.getChunkInfoList().forEach((chunk) -> {
                            if (chunk.getStatus() != HttpDownStatus.DONE) {
                                chunk.setStatus(HttpDownStatus.PAUSE);
                            }
                        });
                    }
                }
                putBoot(bootstrap);
            }
        } catch (Exception e) {
            LOGGER.warn("加载配置文件失败:", e);
        }
    }
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) AbstractHttpDownBootstrap(lee.study.down.boot.AbstractHttpDownBootstrap) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) HttpDownInfo(lee.study.down.model.HttpDownInfo) IOException(java.io.IOException)

Aggregations

TaskInfo (lee.study.down.model.TaskInfo)21 HttpDownInfo (lee.study.down.model.HttpDownInfo)6 ResultInfo (lee.study.down.model.ResultInfo)6 AbstractHttpDownBootstrap (lee.study.down.boot.AbstractHttpDownBootstrap)5 ChunkInfo (lee.study.down.model.ChunkInfo)5 HttpRequestInfo (lee.study.down.model.HttpRequestInfo)5 IOException (java.io.IOException)4 BootstrapException (lee.study.down.exception.BootstrapException)4 WsForm (lee.study.down.mvc.form.WsForm)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 HttpResponse (io.netty.handler.codec.http.HttpResponse)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Channel (io.netty.channel.Channel)2 HttpContent (io.netty.handler.codec.http.HttpContent)2 MalformedURLException (java.net.MalformedURLException)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 TimeoutException (java.util.concurrent.TimeoutException)2 HttpDownCallback (lee.study.down.dispatch.HttpDownCallback)2