Task 5.3: 文档上传接口 - 创建 DocumentManagementService:文档上传核心逻辑 - 文件格式验证(仅 .md/.txt) - 文件 hash 计算与去重检查 - 文本提取与分块处理 - 文档元数据持久化(ApiDocument) - 向量化索引标记为 TODO(待补充) - 创建 DocumentController:RESTful 上传接口 - POST /api/documents/upload - 支持参数:file, faultCategory, faultSource, apiName, version - 返回:文档 docId 功能特性: - MD5 hash 去重:防止重复上传 - 事务支持:元数据与索引状态一致性 - 异常处理:DocumentProcessException 统一封装 - 分块配置:使用 DocumentChunkConfig 默认配置 待补充: - TODO: VectorIndexService.indexDocumentChunks() 实现 - 当前文档状态直接标记为 INDEXED 编译验证:BUILD SUCCESS Progress: 26/33 tasks completed (79%)
57 lines
2.0 KiB
Java
57 lines
2.0 KiB
Java
package com.superbiz.agent.controller;
|
|
|
|
import com.superbiz.agent.dto.DocumentUploadRequest;
|
|
import com.superbiz.agent.dto.Result;
|
|
import com.superbiz.agent.service.DocumentManagementService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
/**
|
|
* 文档管理控制器
|
|
*/
|
|
@Slf4j
|
|
@RestController
|
|
@RequestMapping("/api/documents")
|
|
public class DocumentController {
|
|
|
|
@Autowired
|
|
private DocumentManagementService documentManagementService;
|
|
|
|
/**
|
|
* 上传文档
|
|
*/
|
|
@PostMapping("/upload")
|
|
public Result<String> uploadDocument(
|
|
@RequestParam("file") MultipartFile file,
|
|
@RequestParam(value = "faultCategory", required = false) String faultCategory,
|
|
@RequestParam(value = "faultSource", required = false) String faultSource,
|
|
@RequestParam(value = "apiName", required = false) String apiName,
|
|
@RequestParam(value = "version", required = false) String version,
|
|
@RequestParam(value = "chunkSize", required = false, defaultValue = "500") Integer chunkSize,
|
|
@RequestParam(value = "chunkOverlap", required = false, defaultValue = "50") Integer chunkOverlap
|
|
) {
|
|
try {
|
|
DocumentUploadRequest request = DocumentUploadRequest.builder()
|
|
.file(file)
|
|
.faultCategory(faultCategory)
|
|
.faultSource(faultSource)
|
|
.apiName(apiName)
|
|
.version(version)
|
|
.chunkSize(chunkSize)
|
|
.chunkOverlap(chunkOverlap)
|
|
.build();
|
|
|
|
String docId = documentManagementService.uploadDocument(request);
|
|
log.info("文档上传成功,docId: {}", docId);
|
|
|
|
return Result.success(docId);
|
|
|
|
} catch (Exception e) {
|
|
log.error("文档上传失败", e);
|
|
return Result.error(500, "文档上传失败: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|