feat: 3d模型

This commit is contained in:
2025-12-09 17:13:20 +08:00
parent 5f603bc69d
commit 358639a8ac
492 changed files with 74670 additions and 190 deletions
@@ -0,0 +1,129 @@
package com.central.file.controller;
import com.central.common.model.Result;
import com.central.file.service.ModelFileUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;
/**
* 3D模型文件上传Controller
* 专用于上传到MinIO的goflymodel桶
*
* @author claude
* @date 2024-12-08
*/
@Slf4j
@RestController
@RequestMapping("/model-file")
@RequiredArgsConstructor
@Tag(name = "3D模型文件上传")
public class ModelFileController {
private final ModelFileUploadService modelFileUploadService;
/**
* 上传单个模型文件
*
* @param file 文件
* @param folderPath 文件夹路径(模型文件夹名称)
* @param filePath 文件相对路径(相对于文件夹)
* @return CDN访问URL
*/
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "上传单个模型文件")
public Result<Map<String, Object>> uploadFile(
@Parameter(description = "文件") @RequestParam("file") MultipartFile file,
@Parameter(description = "模型文件夹路径") @RequestParam("folderPath") String folderPath,
@Parameter(description = "文件相对路径") @RequestParam("filePath") String filePath) {
try {
Map<String, Object> result = modelFileUploadService.uploadFile(folderPath, filePath, file);
return Result.succeed(result, "上传成功");
} catch (Exception e) {
log.error("上传模型文件失败: folderPath={}, filePath={}", folderPath, filePath, e);
return Result.failed("上传失败: " + e.getMessage());
}
}
/**
* 批量上传模型文件
*
* @param files 文件列表
* @param folderPath 文件夹路径
* @param filePaths 文件相对路径列表(与files一一对应)
* @return 上传结果
*/
@PostMapping(value = "/upload/batch", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "批量上传模型文件")
public Result<Map<String, Object>> uploadFiles(
@Parameter(description = "文件列表") @RequestParam("files") MultipartFile[] files,
@Parameter(description = "模型文件夹路径") @RequestParam("folderPath") String folderPath,
@Parameter(description = "文件相对路径列表") @RequestParam("filePaths") List<String> filePaths) {
try {
Map<String, Object> result = modelFileUploadService.uploadFiles(folderPath, files, filePaths);
return Result.succeed(result);
} catch (Exception e) {
log.error("批量上传模型文件失败: folderPath={}", folderPath, e);
return Result.failed("上传失败: " + e.getMessage());
}
}
/**
* 删除模型文件夹(所有文件)
*
* @param folderPath 文件夹路径
* @return 操作结果
*/
@DeleteMapping("/folder")
@Operation(summary = "删除模型文件夹")
public Result<Void> deleteFolder(
@Parameter(description = "文件夹路径") @RequestParam("folderPath") String folderPath) {
try {
modelFileUploadService.deleteFolder(folderPath);
return Result.succeed("删除成功");
} catch (Exception e) {
log.error("删除模型文件夹失败: folderPath={}", folderPath, e);
return Result.failed("删除失败: " + e.getMessage());
}
}
/**
* 检查文件是否存在
*
* @param folderPath 文件夹路径
* @param filePath 文件相对路径
* @return 是否存在
*/
@GetMapping("/exists")
@Operation(summary = "检查文件是否存在")
public Result<Boolean> checkFileExists(
@Parameter(description = "文件夹路径") @RequestParam("folderPath") String folderPath,
@Parameter(description = "文件相对路径") @RequestParam("filePath") String filePath) {
boolean exists = modelFileUploadService.fileExists(folderPath, filePath);
return Result.succeed(exists);
}
/**
* 获取文件CDN访问URL
*
* @param folderPath 文件夹路径
* @param filePath 文件相对路径
* @return CDN URL
*/
@GetMapping("/url")
@Operation(summary = "获取文件CDN URL")
public Result<String> getFileUrl(
@Parameter(description = "文件夹路径") @RequestParam("folderPath") String folderPath,
@Parameter(description = "文件相对路径") @RequestParam("filePath") String filePath) {
String url = modelFileUploadService.getFileUrl(folderPath, filePath);
return Result.succeed(url);
}
}
@@ -0,0 +1,278 @@
package com.central.file.service;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;
import com.central.oss.template.S3Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.util.*;
/**
* 3D模型文件上传服务
* 专用于上传到MinIO的goflymodel桶
*
* @author claude
* @date 2024-12-08
*/
@Slf4j
@Service
public class ModelFileUploadService {
private static final String MODEL_BUCKET = "goflymodel";
@Autowired(required = false)
private S3Template s3Template;
@Value("${zlt.file-server.s3.endpoint:}")
private String endpoint;
@Value("${cdn.s3.domain:minicdn.goflytec.com}")
private String cdnDomain;
@Value("${cdn.s3.enabled:true}")
private boolean cdnEnabled;
private AmazonS3 amazonS3;
@PostConstruct
public void init() {
if (s3Template != null) {
this.amazonS3 = s3Template.getAmazonS3();
ensureBucketExists();
} else {
log.warn("S3Template未配置,3D模型上传服务不可用");
}
}
/**
* 确保bucket存在
*/
private void ensureBucketExists() {
if (amazonS3 == null) return;
try {
if (!amazonS3.doesBucketExistV2(MODEL_BUCKET)) {
amazonS3.createBucket(MODEL_BUCKET);
log.info("创建bucket成功: {}", MODEL_BUCKET);
// 设置bucket为公共读取
amazonS3.setBucketPolicy(MODEL_BUCKET, getPublicReadPolicy());
}
} catch (Exception e) {
log.warn("检查/创建bucket失败: {}", e.getMessage());
}
}
/**
* 获取公共读取策略
*/
private String getPublicReadPolicy() {
return "{\n" +
" \"Version\": \"2012-10-17\",\n" +
" \"Statement\": [\n" +
" {\n" +
" \"Effect\": \"Allow\",\n" +
" \"Principal\": \"*\",\n" +
" \"Action\": [\"s3:GetObject\"],\n" +
" \"Resource\": [\"arn:aws:s3:::" + MODEL_BUCKET + "/*\"]\n" +
" }\n" +
" ]\n" +
"}";
}
/**
* 上传单个文件
*
* @param folderPath 文件夹路径
* @param filePath 文件相对路径
* @param file 文件
* @return 上传结果 {objectKey, cdnUrl, directUrl, fileSize}
*/
public Map<String, Object> uploadFile(String folderPath, String filePath, MultipartFile file) {
if (amazonS3 == null) {
throw new RuntimeException("S3服务未初始化");
}
String objectKey = folderPath + "/" + filePath;
try (InputStream inputStream = file.getInputStream()) {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(file.getSize());
metadata.setContentType(getContentType(file.getOriginalFilename()));
PutObjectRequest request = new PutObjectRequest(MODEL_BUCKET, objectKey, inputStream, metadata);
request.getRequestClientOptions().setReadLimit((int) file.getSize() + 1);
amazonS3.putObject(request);
Map<String, Object> result = new HashMap<>();
result.put("objectKey", objectKey);
result.put("cdnUrl", buildCdnUrl(objectKey));
result.put("directUrl", buildDirectUrl(objectKey));
result.put("fileSize", file.getSize());
log.debug("上传文件成功: {}", objectKey);
return result;
} catch (Exception e) {
log.error("上传文件到MinIO失败: bucket={}, key={}", MODEL_BUCKET, objectKey, e);
throw new RuntimeException("文件上传失败: " + e.getMessage());
}
}
/**
* 批量上传文件
*
* @param folderPath 文件夹路径
* @param files 文件数组
* @param filePaths 对应的文件路径列表
* @return 上传结果
*/
public Map<String, Object> uploadFiles(String folderPath, MultipartFile[] files, List<String> filePaths) {
if (files.length != filePaths.size()) {
throw new IllegalArgumentException("文件数量与路径数量不匹配");
}
List<Map<String, Object>> successList = new ArrayList<>();
List<Map<String, Object>> failedList = new ArrayList<>();
long totalSize = 0;
for (int i = 0; i < files.length; i++) {
MultipartFile file = files[i];
String filePath = filePaths.get(i);
try {
Map<String, Object> result = uploadFile(folderPath, filePath, file);
successList.add(result);
totalSize += file.getSize();
} catch (Exception e) {
Map<String, Object> failed = new HashMap<>();
failed.put("filePath", filePath);
failed.put("error", e.getMessage());
failedList.add(failed);
}
}
Map<String, Object> result = new HashMap<>();
result.put("success", failedList.isEmpty());
result.put("successCount", successList.size());
result.put("failedCount", failedList.size());
result.put("successFiles", successList);
result.put("failedFiles", failedList);
result.put("totalSize", totalSize);
// 如果有tileset.json,返回其URL
for (Map<String, Object> item : successList) {
String key = (String) item.get("objectKey");
if (key != null && key.endsWith("tileset.json") && !key.contains("/Block/")) {
result.put("tilesetUrl", item.get("cdnUrl"));
break;
}
}
return result;
}
/**
* 删除文件夹(批量删除)
*
* @param folderPath 文件夹路径
*/
public void deleteFolder(String folderPath) {
if (amazonS3 == null) {
throw new RuntimeException("S3服务未初始化");
}
try {
ListObjectsV2Request listRequest = new ListObjectsV2Request()
.withBucketName(MODEL_BUCKET)
.withPrefix(folderPath);
ListObjectsV2Result result;
List<DeleteObjectsRequest.KeyVersion> keysToDelete = new ArrayList<>();
do {
result = amazonS3.listObjectsV2(listRequest);
for (S3ObjectSummary summary : result.getObjectSummaries()) {
keysToDelete.add(new DeleteObjectsRequest.KeyVersion(summary.getKey()));
}
listRequest.setContinuationToken(result.getNextContinuationToken());
} while (result.isTruncated());
if (!keysToDelete.isEmpty()) {
DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(MODEL_BUCKET)
.withKeys(keysToDelete);
amazonS3.deleteObjects(deleteRequest);
log.info("批量删除文件成功: {}/{}, 共{}个文件", MODEL_BUCKET, folderPath, keysToDelete.size());
}
} catch (Exception e) {
log.error("删除文件夹失败: {}/{}", MODEL_BUCKET, folderPath, e);
throw new RuntimeException("删除失败: " + e.getMessage());
}
}
/**
* 检查文件是否存在
*/
public boolean fileExists(String folderPath, String filePath) {
if (amazonS3 == null) return false;
String objectKey = folderPath + "/" + filePath;
try {
amazonS3.getObjectMetadata(MODEL_BUCKET, objectKey);
return true;
} catch (Exception e) {
return false;
}
}
/**
* 获取文件CDN URL
*/
public String getFileUrl(String folderPath, String filePath) {
String objectKey = folderPath + "/" + filePath;
return buildCdnUrl(objectKey);
}
/**
* 构建CDN URL
*/
private String buildCdnUrl(String objectKey) {
if (cdnEnabled && cdnDomain != null && !cdnDomain.isEmpty()) {
return "https://" + cdnDomain + "/" + MODEL_BUCKET + "/" + objectKey;
} else {
return buildDirectUrl(objectKey);
}
}
/**
* 构建直接访问URL
*/
private String buildDirectUrl(String objectKey) {
return endpoint + "/" + MODEL_BUCKET + "/" + objectKey;
}
/**
* 根据文件名获取ContentType
*/
private String getContentType(String fileName) {
if (fileName == null) return "application/octet-stream";
String lower = fileName.toLowerCase();
if (lower.endsWith(".json")) return "application/json";
if (lower.endsWith(".b3dm")) return "application/octet-stream";
if (lower.endsWith(".i3dm")) return "application/octet-stream";
if (lower.endsWith(".pnts")) return "application/octet-stream";
if (lower.endsWith(".cmpt")) return "application/octet-stream";
if (lower.endsWith(".glb")) return "model/gltf-binary";
if (lower.endsWith(".gltf")) return "model/gltf+json";
if (lower.endsWith(".jpg") || lower.endsWith(".jpeg")) return "image/jpeg";
if (lower.endsWith(".png")) return "image/png";
if (lower.endsWith(".ktx2")) return "image/ktx2";
return "application/octet-stream";
}
}