- 移除 AiChatController 中的 PDF 读取相关逻辑与依赖- 新增 DocumentController 支持文件上传接口 - 新增 DocumentIngestionService 接口及实现,负责文档处理流程 - 抽象 DocumentParser 接口统一各类文档解析器行为 - 重构所有具体文档读取器(PDF、HTML、JSON 等)实现新的解析接口- 引入 MultipartFileResource 工具类以适配 Spring AI 读取器 - 添加 DocumentUploadResponse 响应模型类 - 各文档读取器增加对文件扩展名和 MIME 类型的支持判断
29 lines
865 B
Java
29 lines
865 B
Java
package com.hanserwei.chat.reader;
|
|
|
|
import org.springframework.ai.document.Document;
|
|
import org.springframework.ai.reader.TextReader;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.util.List;
|
|
|
|
@Component
|
|
public class MyTextReader implements DocumentParser {
|
|
|
|
/**
|
|
* 读取 Txt 文档
|
|
* @return 读取的文档集合
|
|
*/
|
|
@Override
|
|
public List<Document> parse(MultipartFile file) {
|
|
TextReader textReader = new TextReader(MultipartFileResource.of(file));
|
|
textReader.getCustomMetadata().put("filename", file.getOriginalFilename());
|
|
return textReader.read();
|
|
}
|
|
|
|
@Override
|
|
public boolean supports(String filename, String contentType) {
|
|
return hasExtension(filename, "txt") || matchesContentType(contentType, "text/plain");
|
|
}
|
|
}
|