Search in sources :

Example 6 with HttpDownInfo

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

the class HttpDownController method getTask.

@RequestMapping("/getTask")
public ResultInfo getTask(@RequestParam String id) throws Exception {
    ResultInfo resultInfo = new ResultInfo();
    HttpDownInfo httpDownInfo = ContentManager.DOWN.getDownInfo(id);
    if (httpDownInfo != null) {
        TaskInfo taskInfo = httpDownInfo.getTaskInfo();
        Map<String, Object> data = new HashMap<>();
        data.put("task", NewTaskForm.parse(httpDownInfo));
        // 检查是否有相同大小的文件
        List<HttpDownInfo> sameTasks = ContentManager.DOWN.getDownInfos().stream().filter(downInfo -> HttpDownStatus.WAIT != downInfo.getTaskInfo().getStatus() && HttpDownStatus.DONE != downInfo.getTaskInfo().getStatus() && downInfo.getTaskInfo().getTotalSize() == taskInfo.getTotalSize()).collect(Collectors.toList());
        data.put("sameTasks", NewTaskForm.parse(sameTasks));
        resultInfo.setData(data);
    }
    return resultInfo;
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) Arrays(java.util.Arrays) HttpDownConstant(lee.study.down.constant.HttpDownConstant) BdyZip(lee.study.down.io.BdyZip) BdyZipEntry(lee.study.down.io.BdyZip.BdyZipEntry) RequestParam(org.springframework.web.bind.annotation.RequestParam) ContentManager(lee.study.down.content.ContentManager) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) ResultStatus(lee.study.down.model.ResultInfo.ResultStatus) FileUtil(lee.study.down.util.FileUtil) TaskInfo(lee.study.down.model.TaskInfo) UpdateService(lee.study.down.update.UpdateService) DirInfo(lee.study.down.model.DirInfo) DirForm(lee.study.down.mvc.form.DirForm) HttpDownApplication(lee.study.down.gui.HttpDownApplication) Map(java.util.Map) GithubUpdateService(lee.study.down.update.GithubUpdateService) HttpDownCallback(lee.study.down.dispatch.HttpDownCallback) Collectors(java.util.stream.Collectors) UnzipForm(lee.study.down.mvc.form.UnzipForm) RestController(org.springframework.web.bind.annotation.RestController) UpdateInfo(lee.study.down.model.UpdateInfo) List(java.util.List) WsForm(lee.study.down.mvc.form.WsForm) BootstrapException(lee.study.down.exception.BootstrapException) ProxyConfig(lee.study.proxyee.proxy.ProxyConfig) HttpDownStatus(lee.study.down.constant.HttpDownStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Value(org.springframework.beans.factory.annotation.Value) RequestBody(org.springframework.web.bind.annotation.RequestBody) AbstractHttpDownBootstrap(lee.study.down.boot.AbstractHttpDownBootstrap) BdyUnzipCallback(lee.study.down.io.BdyZip.BdyUnzipCallback) ConfigInfo(lee.study.down.model.ConfigInfo) LinkedList(java.util.LinkedList) BuildTaskForm(lee.study.down.mvc.form.BuildTaskForm) Desktop(java.awt.Desktop) NewTaskForm(lee.study.down.mvc.form.NewTaskForm) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) HttpRequestInfo(lee.study.down.model.HttpRequestInfo) WsDataType(lee.study.down.mvc.ws.WsDataType) ResultInfo(lee.study.down.model.ResultInfo) IOException(java.io.IOException) File(java.io.File) UnzipInfo(lee.study.down.model.UnzipInfo) OsUtil(lee.study.down.util.OsUtil) DownContent(lee.study.down.content.DownContent) HttpDownInfo(lee.study.down.model.HttpDownInfo) Paths(java.nio.file.Paths) ConfigForm(lee.study.down.mvc.form.ConfigForm) HttpDownUtil(lee.study.down.util.HttpDownUtil) StringUtils(org.springframework.util.StringUtils) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ResultInfo(lee.study.down.model.ResultInfo) HttpDownInfo(lee.study.down.model.HttpDownInfo) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with HttpDownInfo

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

the class GithubUpdateService method update.

@Override
public AbstractHttpDownBootstrap update(UpdateInfo updateInfo, HttpDownCallback callback) throws Exception {
    HttpRequestInfo requestInfo = HttpDownUtil.buildGetRequest(updateInfo.getUrl());
    TaskInfo taskInfo = HttpDownUtil.getTaskInfo(requestInfo, null, null, HttpDownConstant.clientSslContext, HttpDownConstant.clientLoopGroup).setConnections(64).setFileName("proxyee-down-jar.zip").setFilePath(HttpDownConstant.HOME_PATH.substring(0, HttpDownConstant.HOME_PATH.length() - 1));
    HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, requestInfo, null);
    AbstractHttpDownBootstrap bootstrap = HttpDownBootstrapFactory.create(httpDownInfo, 5, HttpDownConstant.clientSslContext, HttpDownConstant.clientLoopGroup, callback);
    FileUtil.deleteIfExists(bootstrap.getHttpDownInfo().getTaskInfo().buildTaskFilePath());
    bootstrap.startDown();
    return bootstrap;
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) AbstractHttpDownBootstrap(lee.study.down.boot.AbstractHttpDownBootstrap) HttpRequestInfo(lee.study.down.model.HttpRequestInfo) HttpDownInfo(lee.study.down.model.HttpDownInfo)

Example 8 with HttpDownInfo

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

the class HttpDownController method doUpdate.

@RequestMapping("/doUpdate")
public ResultInfo doUpdate() throws Exception {
    ResultInfo resultInfo = new ResultInfo();
    if (updateInfo == null) {
        resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("没有可用版本进行更新");
        return resultInfo;
    }
    if (updateBootstrap != null) {
        updateBootstrap.close();
        updateBootstrap = null;
    }
    try {
        updateBootstrap = updateService.update(updateInfo, new HttpDownCallback() {

            @Override
            public void onDone(HttpDownInfo httpDownInfo) throws Exception {
                String zipPath = httpDownInfo.getTaskInfo().buildTaskFilePath();
                String unzipDir = "proxyee-down-" + updateInfo.getVersionStr();
                String unzipPath = unzipDir + "/main/proxyee-down-core.jar";
                // 下载完解压
                FileUtil.unzip(zipPath, null, unzipPath);
                // 复制出来
                Files.copy(Paths.get(httpDownInfo.getTaskInfo().getFilePath() + File.separator + unzipPath), Paths.get(httpDownInfo.getTaskInfo().getFilePath() + File.separator + "proxyee-down-core.jar.bak"));
                // 删除临时的文件
                FileUtil.deleteIfExists(zipPath);
                FileUtil.deleteIfExists(httpDownInfo.getTaskInfo().getFilePath() + File.separator + unzipDir);
                // 通知客户端
                ContentManager.WS.sendMsg(new WsForm(WsDataType.UPDATE_PROGRESS, httpDownInfo.getTaskInfo()));
                // 清空更新下载信息
                updateBootstrap = null;
            }
        });
    } catch (TimeoutException e) {
        resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("检测更新超时,请重试");
        return resultInfo;
    }
    return resultInfo;
}
Also used : HttpDownCallback(lee.study.down.dispatch.HttpDownCallback) WsForm(lee.study.down.mvc.form.WsForm) ResultInfo(lee.study.down.model.ResultInfo) HttpDownInfo(lee.study.down.model.HttpDownInfo) TimeoutException(java.util.concurrent.TimeoutException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with HttpDownInfo

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

the class HttpDownController method commonStartTask.

public static ResultInfo commonStartTask(NewTaskForm taskForm) throws Exception {
    ResultInfo resultInfo = new ResultInfo();
    AbstractHttpDownBootstrap bootstrap = ContentManager.DOWN.getBoot(taskForm.getId());
    HttpDownInfo httpDownInfo = bootstrap.getHttpDownInfo();
    // 覆盖下载
    if (!StringUtils.isEmpty(taskForm.getOldId())) {
        AbstractHttpDownBootstrap oldBootstrap = ContentManager.DOWN.getBoot(taskForm.getOldId());
        if (oldBootstrap == null) {
            resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("任务不存在");
            return resultInfo;
        } else {
            // 暂停之前的下载任务
            oldBootstrap.pauseDown();
            // 修改request
            oldBootstrap.getHttpDownInfo().setRequest(httpDownInfo.getRequest());
            oldBootstrap.getHttpDownInfo().setProxyConfig(httpDownInfo.getProxyConfig());
            Map<String, Object> attr = oldBootstrap.getHttpDownInfo().getAttrs();
            if (attr == null) {
                attr = new HashMap<>();
                oldBootstrap.getHttpDownInfo().setAttrs(attr);
            }
            attr.put(NewTaskForm.KEY_UNZIP_FLAG, taskForm.isUnzip());
            attr.put(NewTaskForm.KEY_UNZIP_PATH, taskForm.getUnzipPath());
            // 移除新的下载任务
            ContentManager.DOWN.removeBoot(taskForm.getId());
            // 持久化
            ContentManager.DOWN.save();
            // 用新链接继续下载
            oldBootstrap.continueDown();
        }
    } else {
        if (StringUtils.isEmpty(taskForm.getFileName())) {
            resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("文件名不能为空");
            return resultInfo;
        }
        if (StringUtils.isEmpty(taskForm.getFilePath())) {
            resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("路径不能为空");
            return resultInfo;
        }
        buildDefaultValues(taskForm);
        TaskInfo taskInfo = httpDownInfo.getTaskInfo();
        synchronized (taskInfo) {
            if (taskInfo.getStatus() != HttpDownStatus.WAIT) {
                resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("任务已添加至下载列表");
            }
            taskInfo.setFileName(taskForm.getFileName());
            taskInfo.setFilePath(taskForm.getFilePath());
            Map<String, Object> attr = httpDownInfo.getAttrs();
            if (attr == null) {
                attr = new HashMap<>();
                httpDownInfo.setAttrs(attr);
            }
            attr.put(NewTaskForm.KEY_UNZIP_FLAG, taskForm.isUnzip());
            attr.put(NewTaskForm.KEY_UNZIP_PATH, taskForm.getUnzipPath());
            if (taskInfo.isSupportRange()) {
                taskInfo.setConnections(taskForm.getConnections());
            } else {
                taskInfo.setConnections(1);
            }
            try {
                bootstrap.startDown();
            } catch (BootstrapException e) {
                resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg(e.getMessage());
                return resultInfo;
            }
            // 记录存储路径
            String lastPath = ContentManager.CONFIG.get().getLastPath();
            if (!taskForm.getFilePath().equalsIgnoreCase(lastPath)) {
                ContentManager.CONFIG.get().setLastPath(taskForm.getFilePath());
                ContentManager.CONFIG.save();
            }
        }
    }
    return resultInfo;
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) AbstractHttpDownBootstrap(lee.study.down.boot.AbstractHttpDownBootstrap) BootstrapException(lee.study.down.exception.BootstrapException) ResultInfo(lee.study.down.model.ResultInfo) HttpDownInfo(lee.study.down.model.HttpDownInfo)

Aggregations

HttpDownInfo (lee.study.down.model.HttpDownInfo)9 TaskInfo (lee.study.down.model.TaskInfo)7 IOException (java.io.IOException)4 AbstractHttpDownBootstrap (lee.study.down.boot.AbstractHttpDownBootstrap)4 BootstrapException (lee.study.down.exception.BootstrapException)4 HttpRequestInfo (lee.study.down.model.HttpRequestInfo)4 ResultInfo (lee.study.down.model.ResultInfo)4 TimeoutException (java.util.concurrent.TimeoutException)3 HttpDownCallback (lee.study.down.dispatch.HttpDownCallback)3 Channel (io.netty.channel.Channel)2 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)2 HttpContent (io.netty.handler.codec.http.HttpContent)2 File (java.io.File)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 HttpDownStatus (lee.study.down.constant.HttpDownStatus)2