|
|
@@ -27,17 +27,21 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.core.io.FileSystemResource;
|
|
|
import org.springframework.core.io.Resource;
|
|
|
+import org.springframework.core.io.support.ResourceRegion;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.http.CacheControl;
|
|
|
import org.springframework.http.ContentDisposition;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpRange;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
-import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
@@ -48,6 +52,7 @@ import java.sql.Statement;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
+import java.util.List;
|
|
|
import java.util.Locale;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
@@ -130,20 +135,30 @@ public class SystemService extends ServiceImpl<SystemInfoMapper, SystemInfo> {
|
|
|
* @param license 授权文件路径
|
|
|
*/
|
|
|
|
|
|
- public void noNet(String license) {
|
|
|
- Assert.isTrue(StrUtil.isNotEmpty(license), () -> ServerException.spe("请选择授权文件!"));
|
|
|
- File file = new File(license);
|
|
|
- Assert.isTrue(file.exists(), () -> ServerException.spe("授权文件不存在!"));
|
|
|
- String fileName = file.getName();
|
|
|
- log.debug("fileName:{}", fileName);
|
|
|
- if (StrUtil.isBlank(fileName)) {
|
|
|
+ /**
|
|
|
+ * 无外网授权激活(离线授权)
|
|
|
+ *
|
|
|
+ * <p>Web 化改造:直接接收浏览器上传的授权文件(原实现由前端传服务端文件路径)。
|
|
|
+ *
|
|
|
+ * @param licenseFile 上传的授权文件(.xlts)
|
|
|
+ */
|
|
|
+ public void noNet(MultipartFile licenseFile) {
|
|
|
+ Assert.isTrue(licenseFile != null && !licenseFile.isEmpty(), () -> ServerException.spe("请选择授权文件!"));
|
|
|
+ String fileName = String.valueOf(licenseFile.getOriginalFilename());
|
|
|
+ if (StrUtil.isBlank(fileName) || "null".equals(fileName)) {
|
|
|
throw new ServerException(LicenseErrorEnum.FILE_UPLOAD_FAIL.getMessage());
|
|
|
}
|
|
|
if (!fileName.toLowerCase().endsWith(".xlts")) {
|
|
|
throw new ServerException(LicenseErrorEnum.FILE_UPLOAD_FAIL.getMessage());
|
|
|
}
|
|
|
- //文件写入
|
|
|
- FileUtil.copyFile(file, PathConst.LICENSE_PATH_FILE.toFile(), StandardCopyOption.REPLACE_EXISTING);
|
|
|
+ //文件写入(覆盖 QingJian/license/license.xlts)
|
|
|
+ try (InputStream in = licenseFile.getInputStream()) {
|
|
|
+ Files.createDirectories(PathConst.LICENSE_PATH);
|
|
|
+ Files.copy(in, PathConst.LICENSE_PATH_FILE, StandardCopyOption.REPLACE_EXISTING);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("保存授权文件失败", e);
|
|
|
+ throw new ServerException(LicenseErrorEnum.FILE_UPLOAD_FAIL.getMessage());
|
|
|
+ }
|
|
|
if (!LicenseUtil.isWin()) {
|
|
|
chmodLicenseFile();
|
|
|
}
|
|
|
@@ -186,6 +201,39 @@ public class SystemService extends ServiceImpl<SystemInfoMapper, SystemInfo> {
|
|
|
return Json.toCls(exec.msg(), AuthInfo.class);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 上传升级包并执行升级。
|
|
|
+ *
|
|
|
+ * <p>Web 化改造:接收浏览器上传的 zip 升级包,落盘到仓库根目录的临时文件后,
|
|
|
+ * 复用 {@link #upgrade(String)} 的原有逻辑(版本校验 → 解压 → 覆盖 db → 执行升级 SQL),
|
|
|
+ * 无论成功失败都会清理临时包。
|
|
|
+ *
|
|
|
+ * @param file 升级包(zip)
|
|
|
+ */
|
|
|
+ public void uploadUpgrade(MultipartFile file) {
|
|
|
+ Assert.isTrue(file != null && !file.isEmpty(), () -> ServerException.spe("请选择升级文件!"));
|
|
|
+ String fileName = String.valueOf(file.getOriginalFilename());
|
|
|
+ if (StrUtil.isBlank(fileName) || !fileName.toLowerCase().endsWith(".zip")) {
|
|
|
+ throw ServerException.spe("升级包必须是 zip 文件!");
|
|
|
+ }
|
|
|
+ Path tempZip = PathConst.ROOT_PATH.resolve("upgrade-package.zip").toAbsolutePath().normalize();
|
|
|
+ try {
|
|
|
+ try (InputStream in = file.getInputStream()) {
|
|
|
+ Files.copy(in, tempZip, StandardCopyOption.REPLACE_EXISTING);
|
|
|
+ }
|
|
|
+ upgrade(tempZip.toString());
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("保存升级包失败", e);
|
|
|
+ throw ServerException.spe("保存升级包失败!");
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ Files.deleteIfExists(tempZip);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.warn("清理临时升级包失败: {}", tempZip, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 系统版本升级
|
|
|
* 解压升级包并执行数据库升级脚本
|
|
|
@@ -387,6 +435,100 @@ public class SystemService extends ServiceImpl<SystemInfoMapper, SystemInfo> {
|
|
|
SystemConfManager.INSTANCE.set("mapLocal", path);
|
|
|
}
|
|
|
|
|
|
+ /** 离线地图瓦片文件名(固定放在 mapLocal 配置的目录下) */
|
|
|
+ private static final String MAP_PMTILES_FILE = "china.pmtiles";
|
|
|
+ /** 离线地图样式文件名(可选,放在 mapLocal 配置的目录下;没有则前端用内置样式) */
|
|
|
+ private static final String MAP_STYLE_FILE = "behavior-spacetime-style.json";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 离线地图瓦片(PMTiles):从 {@code mapLocal} 配置目录读取,支持 HTTP Range。
|
|
|
+ *
|
|
|
+ * <p>Web 端浏览器地图组件按块(Range)读取瓦片,替代原 Electron 的 ipc 文件读取;
|
|
|
+ * 瓦片全部来自服务端配置的目录,调用方无法指定路径。
|
|
|
+ *
|
|
|
+ * @param rangeHeader 请求头 {@code Range},可为空(空则返回整个文件)
|
|
|
+ */
|
|
|
+ public ResponseEntity<?> mapPmtiles(String rangeHeader) {
|
|
|
+ return serveLocalFile(resolveMapFile(MAP_PMTILES_FILE), rangeHeader);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 离线地图自定义样式 JSON(配置目录里的可选文件)。
|
|
|
+ */
|
|
|
+ public ResponseEntity<?> mapStyle() {
|
|
|
+ return serveLocalFile(resolveMapFile(MAP_STYLE_FILE), null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析离线地图目录下的文件:目录取自系统配置 {@code mapLocal}
|
|
|
+ * (运维可通过 {@code /sys/mapLocal} 或 {@code QingJian/conf/c5e503ad.json} 设置)。
|
|
|
+ *
|
|
|
+ * <p>文件名是固定白名单常量,不接受调用方传入,避免任意文件读取。
|
|
|
+ */
|
|
|
+ private Path resolveMapFile(String fileName) {
|
|
|
+ String dir = getMapLocal();
|
|
|
+ if (StrUtil.isBlank(dir)) {
|
|
|
+ throw new ServerException(404, "未配置离线地图目录");
|
|
|
+ }
|
|
|
+ Path file = Path.of(dir).resolve(fileName).toAbsolutePath().normalize();
|
|
|
+ if (Files.notExists(file) || !Files.isRegularFile(file)) {
|
|
|
+ throw new ServerException(404, "离线地图文件不存在:" + file);
|
|
|
+ }
|
|
|
+ return file;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按 HTTP Range 语义返回本地文件:无 Range 返回 200 全量,单区间返回 206 分片。
|
|
|
+ *
|
|
|
+ * <p>刻意不做 {@code Result} 包装 —— 响应体是文件内容本身。
|
|
|
+ */
|
|
|
+ private ResponseEntity<?> serveLocalFile(Path file, String rangeHeader) {
|
|
|
+ Resource resource = new FileSystemResource(file);
|
|
|
+ long length;
|
|
|
+ try {
|
|
|
+ length = Files.size(file);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new ServerException(404, "文件不存在或已被清理");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StrUtil.isBlank(rangeHeader)) {
|
|
|
+ return ResponseEntity.ok()
|
|
|
+ .contentType(MediaType.APPLICATION_OCTET_STREAM)
|
|
|
+ .header(HttpHeaders.ACCEPT_RANGES, "bytes")
|
|
|
+ .contentLength(length)
|
|
|
+ .body(resource);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<HttpRange> ranges;
|
|
|
+ try {
|
|
|
+ ranges = HttpRange.parseRanges(rangeHeader);
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ return rangeNotSatisfiable(length);
|
|
|
+ }
|
|
|
+ if (ranges.size() != 1) {
|
|
|
+ // PMTiles 只发单区间请求;多区间/非法区间一律 416
|
|
|
+ return rangeNotSatisfiable(length);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ HttpRange range = ranges.get(0);
|
|
|
+ long start = range.getRangeStart(length);
|
|
|
+ long end = range.getRangeEnd(length);
|
|
|
+ ResourceRegion region = new ResourceRegion(resource, start, end - start + 1);
|
|
|
+ return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT)
|
|
|
+ .contentType(MediaType.APPLICATION_OCTET_STREAM)
|
|
|
+ .header(HttpHeaders.ACCEPT_RANGES, "bytes")
|
|
|
+ .body(region);
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ return rangeNotSatisfiable(length);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static ResponseEntity<?> rangeNotSatisfiable(long length) {
|
|
|
+ return ResponseEntity.status(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE)
|
|
|
+ .header(HttpHeaders.CONTENT_RANGE, "bytes */" + length)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
public String checkUpdate() {
|
|
|
if (Files.exists(PathConst.ORI_UPGRADE_PATH)) {
|
|
|
return PathConst.ORI_UPGRADE_PATH.toString();
|