feat(workbench-client): 新增数富AI工作台客户端包管理独立更新渠道
后台上传安装包,confirm 阶段由服务端从 MinIO 实物流式复算 sha256 后落库, 客户端经 /api/workbench-client/version/latest 自动检查更新;与 ShuFuAI 桌面端 的 nanri-image/versions/ 渠道隔离,包体落 client 桶 workbench-client/versions/。
This commit is contained in:
+96
-46
@@ -20,9 +20,13 @@ import org.springframework.stereotype.Service;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
@@ -42,6 +46,8 @@ public class OssStorageService {
|
||||
private static final String SOFTWARE_VERSION_PREFIX = "nanri-image/versions/";
|
||||
/** 教程安装包对象键前缀(公开 client 桶,工具台「立即下载教程」直链来源)。 */
|
||||
private static final String TUTORIAL_PREFIX = "tutorial/";
|
||||
/** 数富AI工作台客户端安装/更新包前缀(公开 client 桶,独立于桌面端 nanri-image/versions/)。 */
|
||||
private static final String WORKBENCH_CLIENT_PREFIX = "workbench-client/versions/";
|
||||
private static final String LEGACY_MINIO_ENDPOINT = "http://47.110.241.161:9000";
|
||||
/** readObjectBytes 兜底读取上限(20MB):防止大对象一次性读入内存导致 OOM。 */
|
||||
private static final long DEFAULT_READ_MAX_BYTES = 20L * 1024 * 1024;
|
||||
@@ -191,12 +197,11 @@ public class OssStorageService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 为软件版本安装包签发浏览器直传用的预签名 PUT URL(client 桶公开路径)。
|
||||
* 签名 host 取 public-endpoint(oss.aishufu.top),浏览器对返回的 URL 直接 PUT 即可落对象;
|
||||
* 服务端自己的读写走 internal-endpoint 那张 client,两者不能合用一张。
|
||||
* 公开 client 桶直传预签名共用实现:桌面端版本 / 教程 / 工作台客户端三族只差前缀守卫。
|
||||
* 签名 host 取 public-endpoint(oss.aishufu.top),浏览器拿到后直接 PUT;不能用内网 client 签。
|
||||
*/
|
||||
public String presignSoftwareVersionUpload(String objectKey, int expirySeconds) {
|
||||
requirePrefixedObjectKey(objectKey, SOFTWARE_VERSION_PREFIX, "software version");
|
||||
private String presignPublicClientUpload(String objectKey, String prefix, String what, int expirySeconds) {
|
||||
requirePrefixedObjectKey(objectKey, prefix, what);
|
||||
try {
|
||||
return presignClient().getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
||||
.method(Method.PUT)
|
||||
@@ -209,9 +214,18 @@ public class OssStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
/** 返回软件版本对象的字节数;对象不存在返回 -1(直传完成后由服务端核对大小是否超限)。 */
|
||||
public long softwareVersionObjectSize(String objectKey) {
|
||||
requirePrefixedObjectKey(objectKey, SOFTWARE_VERSION_PREFIX, "software version");
|
||||
/**
|
||||
* 为软件版本安装包签发浏览器直传用的预签名 PUT URL(client 桶公开路径)。
|
||||
* 签名 host 取 public-endpoint(oss.aishufu.top),浏览器对返回的 URL 直接 PUT 即可落对象;
|
||||
* 服务端自己的读写走 internal-endpoint 那张 client,两者不能合用一张。
|
||||
*/
|
||||
public String presignSoftwareVersionUpload(String objectKey, int expirySeconds) {
|
||||
return presignPublicClientUpload(objectKey, SOFTWARE_VERSION_PREFIX, "software version", expirySeconds);
|
||||
}
|
||||
|
||||
/** 公开 client 桶对象大小共用实现:对象不存在返回 -1(直传确认时先卡大小)。 */
|
||||
private long publicClientObjectSize(String objectKey, String prefix, String what) {
|
||||
requirePrefixedObjectKey(objectKey, prefix, what);
|
||||
try {
|
||||
var stat = buildClient().statObject(StatObjectArgs.builder()
|
||||
.bucket(publicClientBucket())
|
||||
@@ -228,6 +242,11 @@ public class OssStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
/** 返回软件版本对象的字节数;对象不存在返回 -1(直传完成后由服务端核对大小是否超限)。 */
|
||||
public long softwareVersionObjectSize(String objectKey) {
|
||||
return publicClientObjectSize(objectKey, SOFTWARE_VERSION_PREFIX, "software version");
|
||||
}
|
||||
|
||||
/** 生成软件版本对象的公开下载 URL(与历史 file_url 同构:path-style client 桶)。 */
|
||||
public String getSoftwareVersionDownloadUrl(String objectKey) {
|
||||
requirePrefixedObjectKey(objectKey, SOFTWARE_VERSION_PREFIX, "software version");
|
||||
@@ -246,9 +265,9 @@ public class OssStorageService {
|
||||
: buildPublicUrl(base, objectKey, publicClientBucket());
|
||||
}
|
||||
|
||||
/** 删除软件版本对象(client 桶);确认环节发现超限等异常时清理残留,避免公共桶悬挂大对象。 */
|
||||
public void removeSoftwareVersionObject(String objectKey) {
|
||||
requirePrefixedObjectKey(objectKey, SOFTWARE_VERSION_PREFIX, "software version");
|
||||
/** 公开 client 桶对象删除共用实现;后台删除记录时清理无引用对象。 */
|
||||
private void removePublicClientObject(String objectKey, String prefix, String what) {
|
||||
requirePrefixedObjectKey(objectKey, prefix, what);
|
||||
try {
|
||||
buildClient().removeObject(RemoveObjectArgs.builder()
|
||||
.bucket(publicClientBucket())
|
||||
@@ -259,41 +278,22 @@ public class OssStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
/** 删除软件版本对象(client 桶);确认环节发现超限等异常时清理残留,避免公共桶悬挂大对象。 */
|
||||
public void removeSoftwareVersionObject(String objectKey) {
|
||||
removePublicClientObject(objectKey, SOFTWARE_VERSION_PREFIX, "software version");
|
||||
}
|
||||
|
||||
/**
|
||||
* 为教程安装包签发浏览器直传用的预签名 PUT URL(公开 client 桶 tutorial/ 前缀)。
|
||||
* 签名 host 同 {@link #presignSoftwareVersionUpload}:必须是公网入口,不能用内网 client。
|
||||
*/
|
||||
public String presignTutorialUpload(String objectKey, int expirySeconds) {
|
||||
requireTutorialKey(objectKey);
|
||||
try {
|
||||
return presignClient().getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
||||
.method(Method.PUT)
|
||||
.bucket(publicClientBucket())
|
||||
.object(objectKey)
|
||||
.expiry(Math.max(60, expirySeconds), TimeUnit.SECONDS)
|
||||
.build());
|
||||
} catch (Exception ex) {
|
||||
throw storageFailure("presign", objectKey, ex);
|
||||
}
|
||||
return presignPublicClientUpload(objectKey, TUTORIAL_PREFIX, "tutorial", expirySeconds);
|
||||
}
|
||||
|
||||
/** 返回教程对象的字节数;对象不存在返回 -1(直传完成后由服务端核对大小是否超限)。 */
|
||||
public long tutorialObjectSize(String objectKey) {
|
||||
requireTutorialKey(objectKey);
|
||||
try {
|
||||
var stat = buildClient().statObject(StatObjectArgs.builder()
|
||||
.bucket(publicClientBucket())
|
||||
.object(objectKey)
|
||||
.build());
|
||||
return stat.size();
|
||||
} catch (ErrorResponseException ex) {
|
||||
if (isNotFound(ex)) {
|
||||
return -1L;
|
||||
}
|
||||
throw storageFailure("stat", objectKey, ex);
|
||||
} catch (Exception ex) {
|
||||
throw storageFailure("stat", objectKey, ex);
|
||||
}
|
||||
return publicClientObjectSize(objectKey, TUTORIAL_PREFIX, "tutorial");
|
||||
}
|
||||
|
||||
/** 生成教程对象的公开下载 URL(与历史直链同构:https://oss.aishufu.top/client/tutorial/...)。 */
|
||||
@@ -304,21 +304,71 @@ public class OssStorageService {
|
||||
|
||||
/** 删除教程对象(client 桶);后台删除记录时清理无引用对象。 */
|
||||
public void removeTutorialObject(String objectKey) {
|
||||
requireTutorialKey(objectKey);
|
||||
try {
|
||||
buildClient().removeObject(RemoveObjectArgs.builder()
|
||||
.bucket(publicClientBucket())
|
||||
.object(objectKey)
|
||||
.build());
|
||||
} catch (Exception ex) {
|
||||
throw storageFailure("delete", objectKey, ex);
|
||||
}
|
||||
removePublicClientObject(objectKey, TUTORIAL_PREFIX, "tutorial");
|
||||
}
|
||||
|
||||
private void requireTutorialKey(String objectKey) {
|
||||
requirePrefixedObjectKey(objectKey, TUTORIAL_PREFIX, "tutorial");
|
||||
}
|
||||
|
||||
/**
|
||||
* 为工作台客户端包签发浏览器直传 PUT 预签名(client 桶 workbench-client/versions/ 前缀)。
|
||||
* 签名 host 与软件版本/教程包一致:必须走公网入口,不能拿内网 client 签。
|
||||
*/
|
||||
public String presignWorkbenchClientUpload(String objectKey, int expirySeconds) {
|
||||
return presignPublicClientUpload(objectKey, WORKBENCH_CLIENT_PREFIX, "workbench client", expirySeconds);
|
||||
}
|
||||
|
||||
/** 返回工作台客户端包字节数;对象不存在返回 -1(直传确认时先卡大小,再算摘要)。 */
|
||||
public long workbenchClientObjectSize(String objectKey) {
|
||||
return publicClientObjectSize(objectKey, WORKBENCH_CLIENT_PREFIX, "workbench client");
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务端流式复算工作台客户端包的 sha256(hex 小写)。
|
||||
* 直传路径不能相信浏览器报的摘要:这条摘要是客户端更新时的硬校验锚点,必须对 MinIO 实物算。
|
||||
*/
|
||||
public String workbenchClientObjectSha256(String objectKey) {
|
||||
requireWorkbenchClientKey(objectKey);
|
||||
MessageDigest digest;
|
||||
try {
|
||||
digest = MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException ex) {
|
||||
throw new IllegalStateException("SHA-256 not available", ex);
|
||||
}
|
||||
try (InputStream stream = buildClient().getObject(GetObjectArgs.builder()
|
||||
.bucket(publicClientBucket())
|
||||
.object(objectKey)
|
||||
.build())) {
|
||||
byte[] buffer = new byte[1 << 20];
|
||||
int read;
|
||||
while ((read = stream.read(buffer)) > 0) {
|
||||
digest.update(buffer, 0, read);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw storageFailure("sha256", objectKey, ex);
|
||||
}
|
||||
return HexFormat.of().formatHex(digest.digest());
|
||||
}
|
||||
|
||||
/** 工作台客户端包公开下载 URL:优先 download-endpoint(主机 B 反代回源),未配置回退 public-endpoint。 */
|
||||
public String getWorkbenchClientDownloadUrl(String objectKey) {
|
||||
requireWorkbenchClientKey(objectKey);
|
||||
String base = downloadEndpoint();
|
||||
return base == null
|
||||
? getPublicUrl(objectKey, publicClientBucket())
|
||||
: buildPublicUrl(base, objectKey, publicClientBucket());
|
||||
}
|
||||
|
||||
/** 删除工作台客户端包对象(client 桶);后台删除记录时清理无引用对象。 */
|
||||
public void removeWorkbenchClientObject(String objectKey) {
|
||||
removePublicClientObject(objectKey, WORKBENCH_CLIENT_PREFIX, "workbench client");
|
||||
}
|
||||
|
||||
private void requireWorkbenchClientKey(String objectKey) {
|
||||
requirePrefixedObjectKey(objectKey, WORKBENCH_CLIENT_PREFIX, "workbench client");
|
||||
}
|
||||
|
||||
/** objectKey 前缀守卫:软件版本族与教程族八格同款,只是前缀与文案里的名字不同。 */
|
||||
private void requirePrefixedObjectKey(String objectKey, String prefix, String what) {
|
||||
if (objectKey == null || !objectKey.startsWith(prefix)) {
|
||||
|
||||
+2
-1
@@ -90,7 +90,8 @@ public class PermissionMenuSchemaInitializer {
|
||||
new DefaultAdminMenu("生成记录", "admin_history", "history", 70, "admin_group_record"),
|
||||
new DefaultAdminMenu("视频任务记录", "admin_image_video_tasks", "image-video-tasks", 75, "admin_group_record"),
|
||||
new DefaultAdminMenu("软件版本管理", "admin_version", "version", 80, "admin_group_record"),
|
||||
new DefaultAdminMenu("数字人版本管理", "digital_human_version", "digital-human-version", 81, "admin_group_record")
|
||||
new DefaultAdminMenu("数字人版本管理", "digital_human_version", "digital-human-version", 81, "admin_group_record"),
|
||||
new DefaultAdminMenu("工作台客户端包管理", "admin_workbench_client_package", "records/workbench-client-package", 83, "admin_group_record")
|
||||
);
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
|
||||
+2
-2
@@ -58,7 +58,7 @@ public class PermissionMenuService {
|
||||
|
||||
/**
|
||||
* 管理/平台配置类菜单:出口仍是 {@code requireAdmin}(用户、菜单、分组、密钥、公司、版本、教程、
|
||||
* 日志),普通账号即使被勾了也用不了。
|
||||
* 工作台客户端包、日志),普通账号即使被勾了也用不了。
|
||||
*
|
||||
* <p>2026-10-06 入口放开后必须与下发集一起剪:现网实查有 13 个普通账号带着 {@code admin_users}、
|
||||
* 11 个带 {@code admin_user_secrets}、9 个带 {@code admin_version} 的授权行,只在出口拦而照常下发
|
||||
@@ -69,7 +69,7 @@ public class PermissionMenuService {
|
||||
"admin_users", "admin_columns", "admin_group_manage",
|
||||
"admin_user_secrets", "admin_user_secret_usage",
|
||||
"admin_version", "digital_human_version", "admin_tutorial",
|
||||
"admin_device_logs");
|
||||
"admin_workbench_client_package", "admin_device_logs");
|
||||
|
||||
private final PermissionMenuMapper permissionMenuMapper;
|
||||
private final UserColumnPermissionMapper userColumnPermissionMapper;
|
||||
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.nanri.aiimage.modules.workbenchclient.controller;
|
||||
|
||||
import com.nanri.aiimage.modules.workbenchclient.service.WorkbenchClientPackageService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 工作台客户端公开版本接口。
|
||||
*
|
||||
* <p>返回裸 JSON(不套 ApiResponse),字段名固定为 {@code version/file_url/sha256}:
|
||||
* 客户端更新器直接按这三个字段比较版本、下载、校验摘要。新增字段只能追加,不能改名。
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/workbench-client/version")
|
||||
@Tag(name = "工作台客户端公开版本接口", description = "工作台客户端自动更新检查")
|
||||
public class PublicWorkbenchClientVersionController {
|
||||
|
||||
private final WorkbenchClientPackageService packageService;
|
||||
|
||||
@GetMapping("/latest")
|
||||
@Operation(summary = "最新客户端包", description = "返回最新上传的版本号、公开下载直链与 sha256;空表时字段为 null")
|
||||
public Map<String, Object> latestVersion() {
|
||||
return packageService.latestPackage();
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
@Operation(summary = "客户端包版本列表", description = "按上传时间倒序返回最近 50 条,供客户端「指定版本更新」使用")
|
||||
public Map<String, Object> listVersions() {
|
||||
return packageService.listPublicPackages();
|
||||
}
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package com.nanri.aiimage.modules.workbenchclient.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.modules.workbenchclient.service.WorkbenchClientPackageService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 数富AI工作台客户端包管理(后台「工作台客户端包管理」页)。
|
||||
* <p>列表、直传预签名、确认落库、删除;客户端读公开接口
|
||||
* {@code /api/workbench-client/version/latest} 自动检查更新。
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/admin/workbench-client")
|
||||
@Tag(name = "工作台客户端包管理", description = "数富AI工作台安装/更新包管理:列表与上传(浏览器直传 MinIO,服务端复算 sha256)")
|
||||
public class WorkbenchClientPackageAdminController {
|
||||
|
||||
private final WorkbenchClientPackageService packageService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
|
||||
@GetMapping("/packages")
|
||||
@Operation(summary = "客户端包列表", description = "按上传时间倒序返回全部记录(第一条即客户端当前更新目标)")
|
||||
public ApiResponse<Map<String, Object>> listPackages(HttpServletRequest request) {
|
||||
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
|
||||
log.info("[workbench-client] 管理端查询客户端包列表 operator={}", operator.getUsername());
|
||||
return ApiResponse.success(Map.of("items", packageService.listPackages()));
|
||||
}
|
||||
|
||||
@PostMapping("/package/presign")
|
||||
@Operation(summary = "签发客户端包直传预签名", description = "返回浏览器直传 MinIO client 桶的 PUT 预签名地址;直传完成后调用 /package/confirm 校验落库")
|
||||
public ApiResponse<Map<String, Object>> presignPackage(
|
||||
HttpServletRequest request,
|
||||
@Parameter(description = "上传的 zip 文件名") @RequestParam(value = "file_name", required = false) String fileName,
|
||||
@Parameter(description = "客户端版本号,如 1.0.0") @RequestParam(value = "version", required = false) String version) {
|
||||
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
|
||||
log.info("[workbench-client] 管理端签发客户端包直传预签名 operator={} fileName={} version={}",
|
||||
operator.getUsername(), fileName, version);
|
||||
return ApiResponse.success(packageService.presignPackage(fileName, version));
|
||||
}
|
||||
|
||||
@PostMapping("/package/confirm")
|
||||
@Operation(summary = "确认客户端包直传完成", description = "服务端核对对象、复算 sha256 并落库,返回新记录行")
|
||||
public ApiResponse<Map<String, Object>> confirmPackage(
|
||||
HttpServletRequest request,
|
||||
@Parameter(description = "预签名返回的对象 key") @RequestParam(value = "object_key", required = false) String objectKey,
|
||||
@Parameter(description = "上传的 zip 文件名") @RequestParam(value = "file_name", required = false) String fileName,
|
||||
@Parameter(description = "客户端版本号,如 1.0.0") @RequestParam(value = "version", required = false) String version) {
|
||||
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
|
||||
log.info("[workbench-client] 管理端确认客户端包直传完成 operator={} objectKey={} fileName={} version={}",
|
||||
operator.getUsername(), objectKey, fileName, version);
|
||||
return ApiResponse.success("上传成功", Map.of("item",
|
||||
packageService.confirmPackage(objectKey, fileName, version)));
|
||||
}
|
||||
|
||||
@PostMapping("/package/delete")
|
||||
@Operation(summary = "批量删除客户端包", description = "按 id 删除记录;关联 MinIO 包体仅当无其它记录引用时清除")
|
||||
public ApiResponse<Map<String, Object>> deletePackages(
|
||||
HttpServletRequest request,
|
||||
@RequestBody List<Long> ids) {
|
||||
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
|
||||
log.info("[workbench-client] 管理端删除客户端包 operator={} ids={}", operator.getUsername(), ids);
|
||||
int deleted = packageService.deletePackages(ids);
|
||||
return ApiResponse.success("删除成功", Map.of("deleted", deleted));
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.nanri.aiimage.modules.workbenchclient.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.nanri.aiimage.modules.workbenchclient.model.entity.WorkbenchClientPackageEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface WorkbenchClientPackageMapper extends BaseMapper<WorkbenchClientPackageEntity> {
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.nanri.aiimage.modules.workbenchclient.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 数富AI工作台客户端安装/更新包记录。
|
||||
*
|
||||
* <p>包体放公开 client 桶 {@code workbench-client/versions/} 前缀,与桌面端 ShuFuAI 的
|
||||
* {@code nanri-image/versions/} 完全隔离;客户端从 {@code /api/workbench-client/version/latest}
|
||||
* 取版本号、下载直链与 sha256,自行比较版本并升级。
|
||||
*/
|
||||
@Data
|
||||
@TableName("biz_workbench_client_package")
|
||||
public class WorkbenchClientPackageEntity {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/** 客户端版本号(客户端据此判断是否需要升级) */
|
||||
private String version;
|
||||
|
||||
/** 原始 zip 文件名(展示用) */
|
||||
private String fileName;
|
||||
|
||||
/** MinIO 对象 key(client 桶,workbench-client/versions/ 前缀) */
|
||||
private String objectKey;
|
||||
|
||||
/** 文件字节数 */
|
||||
private Long fileSize;
|
||||
|
||||
/** 更新包 sha256(hex 小写),客户端下载后的完整性锚点 */
|
||||
private String sha256;
|
||||
|
||||
/** 上传时间,由数据库 CURRENT_TIMESTAMP 兜底;最新一条即客户端更新目标 */
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
+295
@@ -0,0 +1,295 @@
|
||||
package com.nanri.aiimage.modules.workbenchclient.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.workbenchclient.mapper.WorkbenchClientPackageMapper;
|
||||
import com.nanri.aiimage.modules.workbenchclient.model.entity.WorkbenchClientPackageEntity;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 数富AI工作台客户端安装/更新包管理。
|
||||
*
|
||||
* <p>后台只做三件事:上传 zip(浏览器直传 MinIO)、列表、删除。客户端更新接口按上传时间取
|
||||
* 最新一条({@code created_at DESC, id DESC}),删除最新包后自动回落到上一版。
|
||||
* <p>与教程包的关键差别:confirm 阶段必须由服务端从 MinIO 流式复算 sha256 并落库,
|
||||
* 不能留空——客户端下载后要用它做整包完整性校验。
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class WorkbenchClientPackageService {
|
||||
|
||||
/** 独立于桌面端软件版本的前缀:同一公开 client 桶,互不覆盖。 */
|
||||
public static final String OBJECT_KEY_PREFIX = "workbench-client/versions/";
|
||||
|
||||
/** 与桌面端安装包同量级,沿用 512MB 上限。 */
|
||||
private static final long MAX_UPLOAD_BYTES = 512L * 1024 * 1024;
|
||||
|
||||
/** 版本号长度上限,与 biz_workbench_client_package.version VARCHAR(64) 对齐。 */
|
||||
private static final int MAX_VERSION_LENGTH = 64;
|
||||
|
||||
/** 直传预签名有效期:3 分钟足够数百 MB 级安装包上传。 */
|
||||
private static final int PRESIGN_EXPIRY_SECONDS = 180;
|
||||
|
||||
/** 公开版本列表上限:够客户端做「指定版本更新」下拉即可。 */
|
||||
private static final int PUBLIC_LIST_LIMIT = 50;
|
||||
|
||||
private static final DateTimeFormatter CREATED_AT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
||||
|
||||
/** 毫秒级时间戳:同秒连续上传同一版本时也不会覆盖对象。 */
|
||||
private static final DateTimeFormatter OBJECT_NAME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
|
||||
|
||||
/** 对齐软件版本/教程包的 key 安全化规则:保留中文/字母/数字/._-,其余替换为 _。 */
|
||||
private static final Pattern UNSAFE_KEY_CHARS =
|
||||
Pattern.compile("[^\\w.\\-]", Pattern.UNICODE_CHARACTER_CLASS);
|
||||
|
||||
private final WorkbenchClientPackageMapper packageMapper;
|
||||
private final OssStorageService ossStorageService;
|
||||
|
||||
/** 管理端列表:按上传时间倒序,第一条即客户端当前更新目标。 */
|
||||
public List<Map<String, Object>> listPackages() {
|
||||
List<WorkbenchClientPackageEntity> entities = packageMapper.selectList(
|
||||
new LambdaQueryWrapper<WorkbenchClientPackageEntity>()
|
||||
.orderByDesc(WorkbenchClientPackageEntity::getCreatedAt)
|
||||
.orderByDesc(WorkbenchClientPackageEntity::getId));
|
||||
log.info("[workbench-client] 查询客户端包列表 count={}", entities.size());
|
||||
List<Map<String, Object>> items = new ArrayList<>(entities.size());
|
||||
for (WorkbenchClientPackageEntity entity : entities) {
|
||||
items.add(toItemMap(entity));
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/** 客户端更新检查:最新上传的一条;空表时字段全为 null,客户端保持当前版本。 */
|
||||
public Map<String, Object> latestPackage() {
|
||||
WorkbenchClientPackageEntity entity = newest();
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
if (entity == null) {
|
||||
log.info("[workbench-client] 查询最新客户端包:无记录");
|
||||
result.put("version", null);
|
||||
result.put("file_url", null);
|
||||
result.put("sha256", null);
|
||||
result.put("file_name", null);
|
||||
result.put("file_size", null);
|
||||
result.put("created_at", null);
|
||||
return result;
|
||||
}
|
||||
log.info("[workbench-client] 查询最新客户端包 id={} version={} objectKey={}",
|
||||
entity.getId(), entity.getVersion(), entity.getObjectKey());
|
||||
result.put("version", entity.getVersion() == null ? "" : entity.getVersion());
|
||||
result.put("file_url", downloadUrl(entity));
|
||||
result.put("sha256", entity.getSha256() == null ? "" : entity.getSha256());
|
||||
result.put("file_name", entity.getFileName() == null ? "" : entity.getFileName());
|
||||
result.put("file_size", entity.getFileSize() == null ? 0L : entity.getFileSize());
|
||||
result.put("created_at", formatCreatedAt(entity));
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 公开版本列表:桌面端/工作台「指定版本更新」下拉的数据源。 */
|
||||
public Map<String, Object> listPublicPackages() {
|
||||
List<WorkbenchClientPackageEntity> entities = packageMapper.selectList(
|
||||
new LambdaQueryWrapper<WorkbenchClientPackageEntity>()
|
||||
.orderByDesc(WorkbenchClientPackageEntity::getCreatedAt)
|
||||
.orderByDesc(WorkbenchClientPackageEntity::getId)
|
||||
.last("LIMIT " + PUBLIC_LIST_LIMIT));
|
||||
List<Map<String, Object>> items = new ArrayList<>(entities.size());
|
||||
for (WorkbenchClientPackageEntity entity : entities) {
|
||||
items.add(toItemMap(entity));
|
||||
}
|
||||
log.info("[workbench-client] 查询公开客户端包列表 limit={} count={}", PUBLIC_LIST_LIMIT, items.size());
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("items", items);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 签发直传预签名:对象 key 由「毫秒时间戳 + 安全版本号」组成,每次上传互不覆盖。
|
||||
*/
|
||||
public Map<String, Object> presignPackage(String fileName, String version) {
|
||||
String normalizedName = requireZipFileName(fileName);
|
||||
String normalizedVersion = requireVersion(version);
|
||||
String objectKey = OBJECT_KEY_PREFIX
|
||||
+ OBJECT_NAME_FORMATTER.format(LocalDateTime.now())
|
||||
+ "-" + safeVersionKey(normalizedVersion) + ".zip";
|
||||
log.info("[workbench-client] 签发客户端包直传预签名 version={} fileName={} objectKey={} expirySeconds={}",
|
||||
normalizedVersion, normalizedName, objectKey, PRESIGN_EXPIRY_SECONDS);
|
||||
String uploadUrl = ossStorageService.presignWorkbenchClientUpload(objectKey, PRESIGN_EXPIRY_SECONDS);
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("object_key", objectKey);
|
||||
result.put("upload_url", uploadUrl);
|
||||
result.put("file_url", ossStorageService.getWorkbenchClientDownloadUrl(objectKey));
|
||||
result.put("version", normalizedVersion);
|
||||
result.put("file_name", normalizedName);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 直传确认:校验对象存在、大小上限、版本号与对象名一致,服务端复算 sha256 后落库。
|
||||
* <p>sha256 为空或长度不对直接拒绝——宁可让管理员重传,也不让客户端拿到没有锚点的包。
|
||||
*/
|
||||
public Map<String, Object> confirmPackage(String objectKey, String fileName, String version) {
|
||||
if (objectKey == null || !objectKey.startsWith(OBJECT_KEY_PREFIX)) {
|
||||
log.warn("[workbench-client] 直传确认失败:非法对象路径 objectKey={}", objectKey);
|
||||
throw new BusinessException("非法的对象路径,请重新上传");
|
||||
}
|
||||
String normalizedName = requireZipFileName(fileName);
|
||||
String normalizedVersion = requireVersion(version);
|
||||
String expectedSuffix = "-" + safeVersionKey(normalizedVersion) + ".zip";
|
||||
if (!objectKey.endsWith(expectedSuffix)) {
|
||||
log.warn("[workbench-client] 直传确认失败:版本号与对象不匹配 version={} objectKey={}",
|
||||
normalizedVersion, objectKey);
|
||||
throw new BusinessException("版本号与上传对象不匹配,请重新上传");
|
||||
}
|
||||
long size = ossStorageService.workbenchClientObjectSize(objectKey);
|
||||
if (size < 0) {
|
||||
log.warn("[workbench-client] 直传确认失败:对象不存在 objectKey={}", objectKey);
|
||||
throw new BusinessException("对象存储中未找到上传的安装包,请重新上传");
|
||||
}
|
||||
if (size > MAX_UPLOAD_BYTES) {
|
||||
log.warn("[workbench-client] 直传对象超过大小上限,清理残留 objectKey={} bytes={}", objectKey, size);
|
||||
ossStorageService.removeWorkbenchClientObject(objectKey);
|
||||
throw new BusinessException("文件超过允许的大小限制");
|
||||
}
|
||||
String sha256 = ossStorageService.workbenchClientObjectSha256(objectKey);
|
||||
if (sha256 == null || sha256.length() != 64) {
|
||||
log.warn("[workbench-client] 直传确认失败:摘要计算异常 objectKey={} sha256={}", objectKey, sha256);
|
||||
throw new BusinessException("安装包摘要计算失败,请重新上传");
|
||||
}
|
||||
Long duplicated = packageMapper.selectCount(new LambdaQueryWrapper<WorkbenchClientPackageEntity>()
|
||||
.eq(WorkbenchClientPackageEntity::getObjectKey, objectKey));
|
||||
if (duplicated != null && duplicated > 0) {
|
||||
log.warn("[workbench-client] 直传确认失败:对象已被记录,重复提交 objectKey={}", objectKey);
|
||||
throw new BusinessException("该安装包已登记,请勿重复提交");
|
||||
}
|
||||
|
||||
WorkbenchClientPackageEntity entity = new WorkbenchClientPackageEntity();
|
||||
entity.setVersion(normalizedVersion);
|
||||
entity.setFileName(normalizedName);
|
||||
entity.setObjectKey(objectKey);
|
||||
entity.setFileSize(size);
|
||||
entity.setSha256(sha256);
|
||||
packageMapper.insert(entity);
|
||||
log.info("[workbench-client] 客户端包登记成功 id={} version={} fileName={} bytes={} sha256={}",
|
||||
entity.getId(), normalizedVersion, normalizedName, size, sha256);
|
||||
WorkbenchClientPackageEntity saved = packageMapper.selectById(entity.getId());
|
||||
return toItemMap(saved == null ? entity : saved);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 id 批量删除记录;MinIO 对象仅在删除后无任何剩余记录引用时才清除。
|
||||
* <p>最新包被删后,客户端下次检查自动回落到上一版(按上传时间取最新)。
|
||||
*/
|
||||
public int deletePackages(List<Long> ids) {
|
||||
List<Long> distinctIds = ids == null ? List.of() : ids.stream()
|
||||
.filter(id -> id != null).distinct().toList();
|
||||
if (distinctIds.isEmpty()) {
|
||||
throw new BusinessException("请选择要删除的客户端包");
|
||||
}
|
||||
List<WorkbenchClientPackageEntity> targets = packageMapper.selectBatchIds(distinctIds);
|
||||
int deleted = 0;
|
||||
for (Long id : distinctIds) {
|
||||
deleted += packageMapper.deleteById(id);
|
||||
}
|
||||
Set<String> touchedKeys = new LinkedHashSet<>();
|
||||
for (WorkbenchClientPackageEntity target : targets) {
|
||||
if (target.getObjectKey() != null && !target.getObjectKey().isBlank()) {
|
||||
touchedKeys.add(target.getObjectKey());
|
||||
}
|
||||
}
|
||||
int removedObjects = 0;
|
||||
for (String objectKey : touchedKeys) {
|
||||
Long remain = packageMapper.selectCount(new LambdaQueryWrapper<WorkbenchClientPackageEntity>()
|
||||
.eq(WorkbenchClientPackageEntity::getObjectKey, objectKey));
|
||||
if (remain == null || remain <= 0) {
|
||||
removedObjects += removeObjectQuietly(objectKey) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
log.info("[workbench-client] 批量删除客户端包 ids={} deleted={} 清理关联文件数={}",
|
||||
distinctIds, deleted, removedObjects);
|
||||
return deleted;
|
||||
}
|
||||
|
||||
private WorkbenchClientPackageEntity newest() {
|
||||
return packageMapper.selectOne(new LambdaQueryWrapper<WorkbenchClientPackageEntity>()
|
||||
.orderByDesc(WorkbenchClientPackageEntity::getCreatedAt)
|
||||
.orderByDesc(WorkbenchClientPackageEntity::getId)
|
||||
.last("LIMIT 1"));
|
||||
}
|
||||
|
||||
private boolean removeObjectQuietly(String objectKey) {
|
||||
try {
|
||||
if (!objectKey.startsWith(OBJECT_KEY_PREFIX)) {
|
||||
return false;
|
||||
}
|
||||
ossStorageService.removeWorkbenchClientObject(objectKey);
|
||||
return true;
|
||||
} catch (RuntimeException ex) {
|
||||
log.warn("[workbench-client] 清理客户端包对象失败,保留对象 objectKey={} err={}",
|
||||
objectKey, ex.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private String requireZipFileName(String fileName) {
|
||||
String normalized = fileName == null ? "" : fileName.trim();
|
||||
if (normalized.isEmpty()) {
|
||||
throw new BusinessException("请选择要上传的 zip 压缩包");
|
||||
}
|
||||
if (!normalized.toLowerCase(Locale.ROOT).endsWith(".zip")) {
|
||||
throw new BusinessException("仅支持 .zip 格式");
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private String requireVersion(String version) {
|
||||
String normalized = version == null ? "" : version.trim();
|
||||
if (normalized.isEmpty()) {
|
||||
throw new BusinessException("请填写版本号");
|
||||
}
|
||||
if (normalized.length() > MAX_VERSION_LENGTH) {
|
||||
throw new BusinessException("版本号不能超过 " + MAX_VERSION_LENGTH + " 个字符");
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private String safeVersionKey(String version) {
|
||||
String replaced = UNSAFE_KEY_CHARS.matcher(version).replaceAll("_");
|
||||
return replaced.isEmpty() ? "unknown" : replaced;
|
||||
}
|
||||
|
||||
private String downloadUrl(WorkbenchClientPackageEntity entity) {
|
||||
return entity.getObjectKey() == null || entity.getObjectKey().isBlank()
|
||||
? "" : ossStorageService.getWorkbenchClientDownloadUrl(entity.getObjectKey());
|
||||
}
|
||||
|
||||
private String formatCreatedAt(WorkbenchClientPackageEntity entity) {
|
||||
return entity.getCreatedAt() == null ? "" : entity.getCreatedAt().format(CREATED_AT_FORMATTER);
|
||||
}
|
||||
|
||||
private Map<String, Object> toItemMap(WorkbenchClientPackageEntity entity) {
|
||||
Map<String, Object> item = new LinkedHashMap<>();
|
||||
item.put("id", entity.getId());
|
||||
item.put("version", entity.getVersion() == null ? "" : entity.getVersion());
|
||||
item.put("file_name", entity.getFileName() == null ? "" : entity.getFileName());
|
||||
item.put("object_key", entity.getObjectKey() == null ? "" : entity.getObjectKey());
|
||||
item.put("file_size", entity.getFileSize() == null ? 0L : entity.getFileSize());
|
||||
item.put("sha256", entity.getSha256() == null ? "" : entity.getSha256());
|
||||
item.put("file_url", downloadUrl(entity));
|
||||
item.put("created_at", formatCreatedAt(entity));
|
||||
return item;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user