素材上传

This commit is contained in:
zhonghua1
2025-12-24 22:59:54 +08:00
parent 7753523fb7
commit 6a5d6accb4
3 changed files with 335 additions and 36 deletions

View File

@@ -2,13 +2,17 @@ package com.rj.controller;
import com.rj.entity.KnowledgeBase;
import com.rj.service.IKnowledgeBaseService;
import com.rj.service.MinIOService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -20,6 +24,7 @@ import java.util.Map;
* @author 李中华 ,spllzh
* @since 2025-01-27
*/
@Slf4j
@RestController
@RequestMapping("/api/knowledgeBase")
@Tag(name = "知识库管理", description = "知识库管理相关接口")
@@ -28,14 +33,35 @@ public class KnowledgeBaseController {
@Autowired
private IKnowledgeBaseService knowledgeBaseService;
@Autowired
private MinIOService minIOService;
/**
* 新增知识
*/
@PostMapping("/add")
@Operation(summary = "新增知识", description = "添加新的知识信息")
public ResponseEntity<Map<String, Object>> addKnowledge(
@RequestParam(value = "file", required = false) MultipartFile file,
@Parameter(description = "知识信息", required = true)
@RequestBody KnowledgeBase knowledgeBase) {
@ModelAttribute KnowledgeBase knowledgeBase) {
// 如果提供了文件上传到MinIO并保存路径
if (file != null && !file.isEmpty()) {
try {
log.info("开始上传文件到MinIO文件名{}", file.getOriginalFilename());
String fileUrl = minIOService.uploadFile(file);
log.info("文件上传到MinIO成功URL: {}", fileUrl);
// 将上传的文件URL保存到知识库对象的附件URL字段
knowledgeBase.setAttachmentUrl(fileUrl);
} catch (Exception e) {
log.error("文件上传到MinIO失败: {}", e.getMessage(), e);
Map<String, Object> errorResult = new HashMap<>();
errorResult.put("success", false);
errorResult.put("message", "文件上传失败:" + e.getMessage());
return ResponseEntity.internalServerError().body(errorResult);
}
}
Map<String, Object> result = knowledgeBaseService.addKnowledge(knowledgeBase);
Boolean success = (Boolean) result.get("success");
if (success != null && success) {