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)) {
|
||||
|
||||
Reference in New Issue
Block a user