提交 7094edc5 authored 作者: yangli's avatar yangli

.

上级 628c4c94
package com.priusis.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.StreamProgress;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
......@@ -29,9 +30,8 @@ import javax.validation.constraints.Pattern;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
......@@ -76,14 +76,18 @@ public class ProgramController {
if (CollUtil.isEmpty(rows)) return R.success(page.setRecords(Collections.emptyList()));
List<Map<String, String>> programs = installedProgramCache.getPrograms();
List<Map<String, String>> list = rows.stream().map(JSONObject.class::cast)
.map(o -> MapUtil.<String, String>builder()
.put("id", o.getStr("id"))
.put("name", StrUtil.subBefore(o.getStr("fileName"), '.', true))
.put("size", o.getStr("fileSize"))
.put("version", o.getStr("version"))
.put("localVersion", null)
.put("url", o.getStr("url"))
.build())
.map(o -> {
Map<String, String> map = new HashMap<>();
map.put("id", o.getStr(type.equals("install") ? "installId" : "upgradeId"));
map.put("name", StrUtil.subBefore(o.getStr("fileName"), '.', true));
map.put("size", o.getStr("fileSize"));
map.put("version", o.getStr("version"));
map.put("localVersion", null);
map.put("url", o.getStr("url"));
Long id = Convert.toLong(map.get("id"));
map.put("status", SET.contains(id) ? "1" : MAP.containsKey(id) ? "2" : "0");
return map;
})
.peek(m -> programs.stream()
.collect(Collectors.toMap(p -> StrUtil.similar(p.get("name").toLowerCase(), m.get("name").toLowerCase()),
p -> p.getOrDefault("version", ""), (l, r) -> r))
......@@ -97,9 +101,14 @@ public class ProgramController {
return R.success(page.setRecords(list));
}
private static final Set<Long> SET = new ConcurrentHashSet<>();
private static final Map<Long, Thread> MAP = new ConcurrentHashMap<>();
@GetMapping(value = "download_and_install")
public void downloadAndInstall(@RequestParam @NotBlank String url, HttpServletResponse response) throws IOException {
public void downloadAndInstall(@RequestParam @NotBlank Long id,
@RequestParam @NotBlank String url,
HttpServletResponse response) throws IOException {
File dir = new File(SysConfigUtil.getProperty("download.path",
System.getProperty("user.home") + File.separator + "Downloads"));
if (!dir.exists()) dir.mkdirs();
......@@ -110,6 +119,7 @@ public class ProgramController {
@SneakyThrows
@Override
public void start() {
SET.add(id);
response.getWriter().write("0\n");
}
......@@ -123,13 +133,23 @@ public class ProgramController {
@Override
public void finish() {
response.getWriter().close();
SET.remove(id);
}
});
try {
Runtime.getRuntime().exec("cmd /c " + file.getPath());
} catch (Exception e) {
log.error("执行安装文件 {} 失败", file.getPath(), e);
}
MAP.put(id, new Thread(() -> {
try {
Runtime.getRuntime().exec("cmd /c " + file.getPath()).waitFor();
} catch (Exception e) {
log.error("执行安装文件 {} 失败", file.getPath(), e);
}
MAP.remove(id);
}));
MAP.get(id).start();
}
@GetMapping(value = "is_installing")
public R<Boolean> isInstalling(@RequestParam @NotBlank Long id) {
return R.success(MAP.containsKey(id));
}
@GetMapping(value = "get_version")
......@@ -160,5 +180,4 @@ public class ProgramController {
HttpUtil.post(gateway + addProgramUrl,
JSONUtil.createObj().set("macAddress", mac).set("programList", programNames).toString());
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论