Search in sources :

Example 1 with WsForm

use of lee.study.down.mvc.form.WsForm in project proxyee-down by monkeyWie.

the class HttpDownProgressEventTask method run.

@Override
public void run() {
    while (true) {
        try {
            for (TaskInfo taskInfo : ContentManager.DOWN.getStartTasks()) {
                if (taskInfo.getStatus() != HttpDownStatus.DONE && taskInfo.getStatus() != HttpDownStatus.FAIL && taskInfo.getStatus() != HttpDownStatus.PAUSE) {
                    taskInfo.setLastTime(System.currentTimeMillis());
                    for (ChunkInfo chunkInfo : taskInfo.getChunkInfoList()) {
                        if (chunkInfo.getStatus() != HttpDownStatus.DONE && chunkInfo.getStatus() != HttpDownStatus.PAUSE) {
                            chunkInfo.setLastTime(System.currentTimeMillis());
                        }
                    }
                    // 保存任务进度记录
                    synchronized (taskInfo) {
                        if (taskInfo.getStatus() != HttpDownStatus.DONE) {
                            ContentManager.DOWN.saveTask(taskInfo.getId());
                        }
                    }
                }
            }
            ContentManager.WS.sendMsg(ContentManager.DOWN.buildDowningWsForm());
            if (HttpDownController.updateBootstrap != null && HttpDownController.updateBootstrap.getHttpDownInfo().getTaskInfo().getStatus() != HttpDownStatus.DONE) {
                ContentManager.WS.sendMsg(new WsForm(WsDataType.UPDATE_PROGRESS, HttpDownController.updateBootstrap.getHttpDownInfo().getTaskInfo()));
            }
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (Exception e) {
            LOGGER.error("eventTask:", e);
        }
    }
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) ChunkInfo(lee.study.down.model.ChunkInfo) WsForm(lee.study.down.mvc.form.WsForm)

Example 2 with WsForm

use of lee.study.down.mvc.form.WsForm in project proxyee-down by monkeyWie.

the class DownContent method buildWsForm.

public WsForm buildWsForm(String taskId) {
    List<TaskInfo> list = new ArrayList<>();
    TaskInfo taskInfo = getTaskInfo(taskId);
    if (taskInfo == null) {
        return null;
    } else {
        list.add(taskInfo);
        return new WsForm(WsDataType.TASK_LIST, setUrl(list));
    }
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) ArrayList(java.util.ArrayList) WsForm(lee.study.down.mvc.form.WsForm)

Example 3 with WsForm

use of lee.study.down.mvc.form.WsForm in project proxyee-down by monkeyWie.

the class HttpDownHandleCallback method onDone.

@Override
public void onDone(HttpDownInfo httpDownInfo) throws Exception {
    TaskInfo taskInfo = httpDownInfo.getTaskInfo();
    // 更改任务下载状态为已完成
    ContentManager.DOWN.save();
    // 删除任务进度记录文件
    synchronized (taskInfo) {
        FileUtil.deleteIfExists(taskInfo.buildTaskRecordFilePath());
        FileUtil.deleteIfExists(taskInfo.buildTaskRecordBakFilePath());
    }
    sendTask(httpDownInfo.getTaskInfo().getId());
    NewTaskForm taskForm = NewTaskForm.parse(httpDownInfo);
    if (taskForm.isUnzip()) {
        if (BdyZip.isBdyZip(taskInfo.buildTaskFilePath())) {
            WsForm wsForm = new WsForm(WsDataType.UNZIP_NEW, new HashMap<String, String>() {

                {
                    put("filePath", taskInfo.buildTaskFilePath());
                    put("toPath", taskForm.getUnzipPath());
                }
            });
            ContentManager.WS.sendMsg(wsForm);
        }
    }
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) NewTaskForm(lee.study.down.mvc.form.NewTaskForm) WsForm(lee.study.down.mvc.form.WsForm)

Example 4 with WsForm

use of lee.study.down.mvc.form.WsForm in project proxyee-down by monkeyWie.

the class HttpDownController method bdyUnzip.

@RequestMapping("/bdyUnzip")
public ResultInfo bdyUnzip(@RequestParam String id, @RequestParam boolean ignore, @RequestBody UnzipForm unzipForm) throws IOException {
    ResultInfo resultInfo = new ResultInfo();
    File file = new File(unzipForm.getFilePath());
    if (file.exists() && file.isFile()) {
        if (!unzipForm.getFilePath().equalsIgnoreCase(unzipForm.getToPath())) {
            if (ignore || BdyZip.isBdyZip(unzipForm.getFilePath())) {
                UnzipInfo unzipInfo = new UnzipInfo().setId(id);
                if (!FileUtil.exists(unzipForm.getToPath())) {
                    FileUtil.createDirSmart(unzipForm.getToPath());
                }
                if (!FileUtil.canWrite(unzipForm.getToPath())) {
                    resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("无权访问解压路径,请修改路径或开放目录写入权限");
                    return resultInfo;
                }
                new Thread(() -> {
                    try {
                        BdyZip.unzip(unzipForm.getFilePath(), unzipForm.getToPath(), new BdyUnzipCallback() {

                            @Override
                            public void onStart() {
                                unzipInfo.setType(BdyZip.ON_START).setStartTime(System.currentTimeMillis());
                                ContentManager.WS.sendMsg(new WsForm(WsDataType.UNZIP_ING, unzipInfo));
                            }

                            @Override
                            public void onFix(long totalSize, long fixSize) {
                                unzipInfo.setType(BdyZip.ON_FIX).setTotalFixSize(totalSize).setFixSize(fixSize);
                                ContentManager.WS.sendMsg(new WsForm(WsDataType.UNZIP_ING, unzipInfo));
                            }

                            @Override
                            public void onFixDone(List<BdyZipEntry> list) {
                                unzipInfo.setType(BdyZip.ON_FIX_DONE).setTotalFileSize(list.stream().map(entry -> entry.getCompressedSize()).reduce((s1, s2) -> s1 + s2).get());
                            }

                            @Override
                            public void onEntryStart(BdyZipEntry entry) {
                                unzipInfo.setType(BdyZip.ON_ENTRY_START).setEntry(entry).setCurrFileSize(entry.getCompressedSize()).setCurrWriteSize(0);
                                ContentManager.WS.sendMsg(new WsForm(WsDataType.UNZIP_ING, unzipInfo));
                            }

                            @Override
                            public void onEntryWrite(long totalSize, long writeSize) {
                                unzipInfo.setType(BdyZip.ON_ENTRY_WRITE).setCurrWriteSize(unzipInfo.getCurrWriteSize() + writeSize).setTotalWriteSize(unzipInfo.getTotalWriteSize() + writeSize);
                                ContentManager.WS.sendMsg(new WsForm(WsDataType.UNZIP_ING, unzipInfo));
                            }

                            @Override
                            public void onDone() {
                                unzipInfo.setType(BdyZip.ON_DONE).setEndTime(System.currentTimeMillis());
                                ContentManager.WS.sendMsg(new WsForm(WsDataType.UNZIP_ING, unzipInfo));
                            }

                            @Override
                            public void onError(Exception e) {
                                unzipInfo.setType(BdyZip.ON_ERROR).setErrorMsg(e.toString());
                                ContentManager.WS.sendMsg(new WsForm(WsDataType.UNZIP_ING, unzipInfo));
                            }
                        });
                    } catch (Exception e) {
                        LOGGER.error("unzip error:", e);
                    }
                }).start();
            } else {
                resultInfo.setStatus(ResultStatus.BAD.getCode());
                resultInfo.setMsg("解压失败,请确认是否为百度云批量下载zip文件");
            }
        } else {
            resultInfo.setStatus(ResultStatus.BAD.getCode());
            resultInfo.setMsg("解压失败,文件路径与解压路径相同");
        }
    } else {
        resultInfo.setStatus(ResultStatus.BAD.getCode());
        resultInfo.setMsg("解压失败,文件不存在");
    }
    return resultInfo;
}
Also used : 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) BdyUnzipCallback(lee.study.down.io.BdyZip.BdyUnzipCallback) UnzipInfo(lee.study.down.model.UnzipInfo) List(java.util.List) LinkedList(java.util.LinkedList) WsForm(lee.study.down.mvc.form.WsForm) ResultInfo(lee.study.down.model.ResultInfo) File(java.io.File) BdyZipEntry(lee.study.down.io.BdyZip.BdyZipEntry) TimeoutException(java.util.concurrent.TimeoutException) BootstrapException(lee.study.down.exception.BootstrapException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with WsForm

use of lee.study.down.mvc.form.WsForm 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)

Aggregations

WsForm (lee.study.down.mvc.form.WsForm)5 TaskInfo (lee.study.down.model.TaskInfo)4 TimeoutException (java.util.concurrent.TimeoutException)2 HttpDownCallback (lee.study.down.dispatch.HttpDownCallback)2 HttpDownInfo (lee.study.down.model.HttpDownInfo)2 ResultInfo (lee.study.down.model.ResultInfo)2 NewTaskForm (lee.study.down.mvc.form.NewTaskForm)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Desktop (java.awt.Desktop)1 File (java.io.File)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1