step上传
This commit is contained in:
+85
@@ -0,0 +1,85 @@
|
||||
package com.linkerbot.vibemake.service.admin;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.linkerbot.vibemake.bean.Contracts.ProjectView;
|
||||
import com.linkerbot.vibemake.bean.StepContracts.*;
|
||||
import com.linkerbot.vibemake.bean.entity.Project;
|
||||
import com.linkerbot.vibemake.common.BusinessException;
|
||||
import com.linkerbot.vibemake.service.ProjectService;
|
||||
import com.linkerbot.vibemake.service.mapper.ProjectMapper;
|
||||
import com.linkerbot.vibemake.service.oss.StepResources;
|
||||
import com.linkerbot.vibemake.service.support.ProjectActors;
|
||||
import com.linkerbot.vibemake.service.support.ProjectLocks;
|
||||
import com.linkerbot.vibemake.service.support.ProjectSupport;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class AdminStepService {
|
||||
private static final Logger log = LoggerFactory.getLogger(AdminStepService.class);
|
||||
private final ProjectService projects;
|
||||
private final ProjectMapper mapper;
|
||||
private final StepResources resources;
|
||||
private final ProjectLocks locks;
|
||||
|
||||
private void requireAdmin() {
|
||||
if (!ProjectActors.isAdmin())
|
||||
throw new BusinessException(403, "FORBIDDEN", "仅管理员可管理 STEP 文件");
|
||||
}
|
||||
|
||||
public StepUploadSignature signature(String projectCode, StepUploadInput input) {
|
||||
requireAdmin();
|
||||
projects.readyOwner(0, projectCode);
|
||||
if (input == null) throw StepResources.invalidFile();
|
||||
return resources.sign(projectCode, input.files());
|
||||
}
|
||||
|
||||
public ProjectView confirm(String projectCode, StepConfirmInput input) {
|
||||
requireAdmin();
|
||||
projects.readyOwner(0, projectCode);
|
||||
if (input == null) throw StepResources.invalidFile();
|
||||
// OSS 网络调用不占用项目锁;落库前重新读取项目并核验状态。
|
||||
String path = resources.verify(projectCode, input.batchCode(), input.files());
|
||||
StepFile file = input.files().getFirst();
|
||||
return locks.serial(projectCode, () -> {
|
||||
Project project = projects.readyOwner(0, projectCode);
|
||||
project.setStepObjectKey(path);
|
||||
project.setStepFileName(file.relativePath());
|
||||
project.setStepSizeBytes(file.sizeBytes());
|
||||
persist(project);
|
||||
log.info("绑定 STEP 文件 projectCode={} batchCode={} operatorCode={} sizeBytes={}",
|
||||
projectCode, input.batchCode(), ProjectActors.admin().userCode(), file.sizeBytes());
|
||||
return projects.view(project, null);
|
||||
});
|
||||
}
|
||||
|
||||
public void clear(String projectCode) {
|
||||
requireAdmin();
|
||||
locks.serial(projectCode, () -> {
|
||||
Project project = projects.readyOwner(0, projectCode);
|
||||
project.setStepObjectKey("");
|
||||
project.setStepFileName("");
|
||||
project.setStepSizeBytes(0L);
|
||||
persist(project);
|
||||
log.info("移除 STEP 绑定 projectCode={} operatorCode={}",
|
||||
projectCode, ProjectActors.admin().userCode());
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private void persist(Project project) {
|
||||
project.setUpdatedAt(ProjectSupport.now());
|
||||
int updated = mapper.update(null, new LambdaUpdateWrapper<Project>()
|
||||
.eq(Project::getProjectCode, project.getProjectCode())
|
||||
.eq(Project::getDeleted, false)
|
||||
.eq(Project::getRepoStatus, "READY")
|
||||
.set(Project::getStepObjectKey, project.getStepObjectKey())
|
||||
.set(Project::getStepFileName, project.getStepFileName())
|
||||
.set(Project::getStepSizeBytes, project.getStepSizeBytes())
|
||||
.set(Project::getUpdatedAt, project.getUpdatedAt()));
|
||||
if (updated != 1) throw BusinessException.conflict("项目状态已变化,请刷新后重试");
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.linkerbot.vibemake.service.config;
|
||||
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Positive;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Validated
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "vibemake.oss.step")
|
||||
public class StepProperties {
|
||||
@Positive
|
||||
@Max(value = 5368709120L, message = "STEP 文件大小不能超过 OSS 表单直传的 5 GiB 上限")
|
||||
private long maxFileBytes = 500L * 1024 * 1024;
|
||||
}
|
||||
+8
-1
@@ -199,6 +199,7 @@ public class ProjectServiceImpl implements ProjectService {
|
||||
ProjectImportTask imported,
|
||||
boolean includeFiles) {
|
||||
try {
|
||||
boolean hasStep = project.getStepObjectKey() != null && !project.getStepObjectKey().isEmpty();
|
||||
return new ProjectView(
|
||||
project.getProjectCode(),
|
||||
project.getName(),
|
||||
@@ -206,6 +207,9 @@ public class ProjectServiceImpl implements ProjectService {
|
||||
resourcePaths.coverUrl(project.getCoverUrl()),
|
||||
urdfUrl(project.getUrdfMainObjectKey()),
|
||||
urdfUrl(project.getUrdfPrefix()),
|
||||
resourcePaths.publicUrl(project.getStepObjectKey()),
|
||||
hasStep ? project.getStepFileName() : null,
|
||||
hasStep ? project.getStepSizeBytes() : null,
|
||||
resourcePaths.publicUrl(project.getModelGlbObjectKey()),
|
||||
resourcePaths.publicUrl(project.getModelPartsPrefix()),
|
||||
includeFiles ? resourceFiles(project.getModelPartsPrefix(), project.getModelPartsFiles(),
|
||||
@@ -307,7 +311,10 @@ public class ProjectServiceImpl implements ProjectService {
|
||||
if (source != null) {
|
||||
project.setSourceProjectId(source.getId());
|
||||
project.setSourceCommitSha(source.getPublishedCommitSha());
|
||||
// 新增资源只继承数据库引用;不查询或复制 OSS,源项目替换也不会改变副本绑定。
|
||||
// STEP 等项目资源只继承数据库引用;不访问 OSS,源项目替换不会改变副本绑定。
|
||||
project.setStepObjectKey(source.getStepObjectKey());
|
||||
project.setStepFileName(source.getStepFileName());
|
||||
project.setStepSizeBytes(source.getStepSizeBytes());
|
||||
project.setModelGlbObjectKey(source.getModelGlbObjectKey());
|
||||
project.setModelGlbSizeBytes(source.getModelGlbSizeBytes());
|
||||
project.setModelPartsPrefix(source.getModelPartsPrefix());
|
||||
|
||||
+11
-3
@@ -54,7 +54,7 @@ public class OssPostSigner {
|
||||
public CoverUploadSignature sign(String userCode, String contentType, String extension) {
|
||||
String key = properties.getRootDirectory() + "/project-covers/" + userCode + "/"
|
||||
+ BUSINESS_DATE.format(clock.instant()) + "/" + Codes.next() + "." + extension;
|
||||
SignedPost post = post(key, contentType, 1, MAX_FILE_BYTES, VALIDITY, false);
|
||||
SignedPost post = post(key, contentType, 1, MAX_FILE_BYTES, VALIDITY, false, null);
|
||||
return new CoverUploadSignature(properties.uploadUrl(), key,
|
||||
resourcePaths.publicUrl(key), post.expiresAt(), post.formFields());
|
||||
}
|
||||
@@ -66,11 +66,16 @@ public class OssPostSigner {
|
||||
}
|
||||
|
||||
public SignedPost signObject(String key, String contentType, long sizeBytes, Duration validity) {
|
||||
return post(key, contentType, sizeBytes, sizeBytes, validity, true);
|
||||
return signObject(key, contentType, sizeBytes, validity, null);
|
||||
}
|
||||
|
||||
public SignedPost signObject(String key, String contentType, long sizeBytes,
|
||||
Duration validity, String contentDisposition) {
|
||||
return post(key, contentType, sizeBytes, sizeBytes, validity, true, contentDisposition);
|
||||
}
|
||||
|
||||
private SignedPost post(String key, String contentType, long minBytes, long maxBytes,
|
||||
Duration validity, boolean forbidOverwrite) {
|
||||
Duration validity, boolean forbidOverwrite, String contentDisposition) {
|
||||
Instant now = clock.instant().truncatedTo(ChronoUnit.MILLIS);
|
||||
Instant expiration = now.plus(validity);
|
||||
String date = DATE.format(now);
|
||||
@@ -92,6 +97,8 @@ public class OssPostSigner {
|
||||
conditions.add(List.of("eq", "$x-oss-forbid-overwrite", "true"));
|
||||
conditions.add(List.of("eq", "$x-oss-content-type", contentType));
|
||||
}
|
||||
if (contentDisposition != null)
|
||||
conditions.add(List.of("eq", "$Content-Disposition", contentDisposition));
|
||||
policy.put("conditions", conditions);
|
||||
try {
|
||||
String encoded = Base64.getEncoder().encodeToString(json.writeValueAsBytes(policy));
|
||||
@@ -105,6 +112,7 @@ public class OssPostSigner {
|
||||
properties.getRegion(), encoded));
|
||||
fields.put("content-type", contentType);
|
||||
fields.put("success_action_status", "200");
|
||||
if (contentDisposition != null) fields.put("Content-Disposition", contentDisposition);
|
||||
if (forbidOverwrite) {
|
||||
fields.put("x-oss-forbid-overwrite", "true");
|
||||
fields.put("x-oss-content-type", contentType);
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.linkerbot.vibemake.service.oss;
|
||||
|
||||
import com.linkerbot.vibemake.bean.StepContracts.*;
|
||||
import com.linkerbot.vibemake.common.BusinessException;
|
||||
import com.linkerbot.vibemake.common.Codes;
|
||||
import com.linkerbot.vibemake.common.FilePaths;
|
||||
import com.linkerbot.vibemake.service.config.OssProperties;
|
||||
import com.linkerbot.vibemake.service.config.StepProperties;
|
||||
import com.linkerbot.vibemake.service.support.ProjectSupport;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.ContentDisposition;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/** 与 URDF 一致的无状态签名和核验;批次编码仅用于隔离对象目录。 */
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class StepResources {
|
||||
private static final Logger log = LoggerFactory.getLogger(StepResources.class);
|
||||
private final OssProperties oss;
|
||||
private final StepProperties limits;
|
||||
private final OssPostSigner signer;
|
||||
private final OssObjectStore store;
|
||||
private final OssResourcePaths paths;
|
||||
|
||||
public static BusinessException invalidFile() {
|
||||
return new BusinessException(400, "STEP_INVALID_FILE", "请提供一个合法的非空 STEP 或 STP 文件");
|
||||
}
|
||||
|
||||
public StepFile validate(List<StepFile> files) {
|
||||
if (files == null || files.size() != 1 || files.getFirst() == null) throw invalidFile();
|
||||
StepFile file = files.getFirst();
|
||||
if (file.sizeBytes() == null || file.sizeBytes() <= 0) throw invalidFile();
|
||||
if (file.sizeBytes() > limits.getMaxFileBytes())
|
||||
throw new BusinessException(413, "STEP_FILE_TOO_LARGE",
|
||||
"STEP 文件大小不能超过 " + limits.getMaxFileBytes() + " 字节");
|
||||
String name;
|
||||
try {
|
||||
name = FilePaths.file(file.relativePath());
|
||||
} catch (BusinessException error) {
|
||||
throw invalidFile();
|
||||
}
|
||||
int dot = name.lastIndexOf('.');
|
||||
if (name.contains("/") || name.chars().anyMatch(Character::isISOControl)
|
||||
|| dot <= 0 || name.substring(0, dot).isBlank()) throw invalidFile();
|
||||
String extension = name.substring(dot).toLowerCase(Locale.ROOT);
|
||||
if (!extension.equals(".step") && !extension.equals(".stp")) throw invalidFile();
|
||||
return file;
|
||||
}
|
||||
|
||||
public String storagePath(String projectCode, String batchCode, StepFile file) {
|
||||
if (projectCode == null || !projectCode.matches("[a-z0-9]{12}|[a-z0-9]{32}")
|
||||
|| batchCode == null || !batchCode.matches("[a-f0-9]{32}"))
|
||||
throw new BusinessException(400, "STEP_INVALID_FILE", "项目或 STEP 上传批次编码不合法");
|
||||
String extension = file.relativePath().substring(file.relativePath().lastIndexOf('.'));
|
||||
return "/project-resources/" + projectCode + "/step/" + batchCode + "/model" + extension;
|
||||
}
|
||||
|
||||
private void requireEnabled() {
|
||||
if (!oss.isEnabled())
|
||||
throw new BusinessException(503, "OSS_UNAVAILABLE", "OSS STEP 上传暂未开启");
|
||||
}
|
||||
|
||||
public StepUploadSignature sign(String projectCode, List<StepFile> files) {
|
||||
StepFile file = validate(files);
|
||||
requireEnabled();
|
||||
String batchCode = Codes.next();
|
||||
String key = paths.objectKey(storagePath(projectCode, batchCode, file));
|
||||
String disposition = ContentDisposition.attachment()
|
||||
.filename(file.relativePath(), StandardCharsets.UTF_8).build().toString();
|
||||
OssPostSigner.SignedPost post = signer.signObject(key, "application/octet-stream",
|
||||
file.sizeBytes(), Duration.ofMinutes(15), disposition);
|
||||
log.info("生成 STEP 上传签名 projectCode={} batchCode={} sizeBytes={}",
|
||||
projectCode, batchCode, file.sizeBytes());
|
||||
return new StepUploadSignature(batchCode, file.relativePath(), oss.uploadUrl(),
|
||||
post.expiresAt().atZone(ProjectSupport.TIME_ZONE).toInstant(),
|
||||
List.of(new StepFileSignature(file.relativePath(), key, post.formFields())));
|
||||
}
|
||||
|
||||
public String verify(String projectCode, String batchCode, List<StepFile> files) {
|
||||
StepFile file = validate(files);
|
||||
String path = storagePath(projectCode, batchCode, file);
|
||||
requireEnabled();
|
||||
try {
|
||||
OssObjectStore.Metadata metadata = store.head(paths.objectKey(path));
|
||||
if (metadata.sizeBytes() != file.sizeBytes()) {
|
||||
log.warn("STEP 文件大小不符 projectCode={} batchCode={} expectedBytes={} actualBytes={}",
|
||||
projectCode, batchCode, file.sizeBytes(), metadata.sizeBytes());
|
||||
throw new BusinessException(409, "STEP_FILE_SIZE_MISMATCH", "STEP 文件大小不符,请重新上传");
|
||||
}
|
||||
return path;
|
||||
} catch (BusinessException error) {
|
||||
log.warn("STEP 文件核验失败 projectCode={} batchCode={} code={}",
|
||||
projectCode, batchCode, error.code());
|
||||
if ("URDF_FILE_MISSING".equals(error.code()))
|
||||
throw new BusinessException(409, "STEP_FILE_MISSING", "STEP 文件尚未上传,请重新上传");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user