fix
This commit is contained in:
@@ -71,6 +71,14 @@ public class FileRiskController {
|
|||||||
return AlitaResult.success(fileRiskDTO);
|
return AlitaResult.success(fileRiskDTO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/file_risk/dify_risk_rule")
|
||||||
|
@ApiOperation("获取Dify风险规则")
|
||||||
|
public AlitaResult<String> getDifyRiskRule(
|
||||||
|
@ApiParam(value = "文件编码", required = true) @RequestParam String fileCode) {
|
||||||
|
String answer = fileRiskService.getDifyRiskRule(fileCode);
|
||||||
|
return AlitaResult.success(answer);
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/file_risk/list")
|
@GetMapping("/file_risk/list")
|
||||||
@ApiOperation("列表查询文件风险")
|
@ApiOperation("列表查询文件风险")
|
||||||
public AlitaResult<List<FileRiskDTO>> list(FileRiskQueryDTO queryDTO) {
|
public AlitaResult<List<FileRiskDTO>> list(FileRiskQueryDTO queryDTO) {
|
||||||
|
|||||||
@@ -65,6 +65,15 @@ public interface FileRiskService {
|
|||||||
*/
|
*/
|
||||||
FileRiskDTO scanFileRisk(String fileCode);
|
FileRiskDTO scanFileRisk(String fileCode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 Dify 风险规则
|
||||||
|
* 上传文件到 Dify 并调用规则生成 API,返回生成的风险规则
|
||||||
|
*
|
||||||
|
* @param fileCode 文件编码
|
||||||
|
* @return Dify 返回的 answer
|
||||||
|
*/
|
||||||
|
String getDifyRiskRule(String fileCode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 列表查询文件风险
|
* 列表查询文件风险
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -163,14 +163,22 @@ public class FileRiskServiceImpl implements FileRiskService {
|
|||||||
// 2. 如果 difyFileJson 为空,则上传文件
|
// 2. 如果 difyFileJson 为空,则上传文件
|
||||||
DifyFileUploadResponse uploadResponse = null;
|
DifyFileUploadResponse uploadResponse = null;
|
||||||
if (StringUtils.isBlank(fileRiskDTO.getDifyFileJson())) {
|
if (StringUtils.isBlank(fileRiskDTO.getDifyFileJson())) {
|
||||||
uploadResponse = uploadFileToDify(fileRiskDTO, fileCode, riskScanApiKey);
|
uploadResponse = uploadFileToDify(fileCode, riskScanApiKey);
|
||||||
|
String difyFileJson = JsonUtils.toJsonString(uploadResponse);
|
||||||
|
// 3. 更新 difyFileJson 字段
|
||||||
|
FileRisk updateFileRisk = FileRisk.builder()
|
||||||
|
.id(fileRiskDTO.getId())
|
||||||
|
.difyFileJson(difyFileJson)
|
||||||
|
.build();
|
||||||
|
fileRiskMapper.updateEntitySelective(updateFileRisk);
|
||||||
|
// 更新 DTO
|
||||||
|
fileRiskDTO.setDifyFileJson(difyFileJson);
|
||||||
} else {
|
} else {
|
||||||
// 从 difyFileJson 中解析 uploadFileId
|
// 从 difyFileJson 中解析 uploadFileId
|
||||||
uploadResponse = JsonUtils.toObject(fileRiskDTO.getDifyFileJson(), DifyFileUploadResponse.class);
|
uploadResponse = JsonUtils.toObject(fileRiskDTO.getDifyFileJson(), DifyFileUploadResponse.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 如果 riskJson 为空,则调用 Dify Chat 分析
|
// 3. 如果 riskJson 为空,则调用 Dify Chat 分析
|
||||||
|
|
||||||
Map<String, Object> inputs = new HashMap<>();
|
Map<String, Object> inputs = new HashMap<>();
|
||||||
inputs.put("gz", this.getRiskRule(OrgConstants.PLATFORM_ORG_ID));
|
inputs.put("gz", this.getRiskRule(OrgConstants.PLATFORM_ORG_ID));
|
||||||
inputs.put("srwj",
|
inputs.put("srwj",
|
||||||
@@ -216,6 +224,47 @@ public class FileRiskServiceImpl implements FileRiskService {
|
|||||||
return fileRiskDTO;
|
return fileRiskDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 Dify 风险规则
|
||||||
|
* 上传文件到 Dify 并调用规则生成 API,返回生成的风险规则
|
||||||
|
*
|
||||||
|
* @param fileCode 文件编码
|
||||||
|
* @return Dify 返回的 answer
|
||||||
|
*/
|
||||||
|
public String getDifyRiskRule(String fileCode) {
|
||||||
|
// 1. 上传文件到 Dify
|
||||||
|
DifyFileUploadResponse uploadResponse = uploadFileToDify(fileCode, riskRuleApiKey);
|
||||||
|
String uploadFileId = uploadResponse.getId();
|
||||||
|
Assert.notBlank(uploadFileId, "上传文件到 Dify 失败,fileCode: " + fileCode);
|
||||||
|
|
||||||
|
// 2. 调用 Dify Chat API(规则生成)
|
||||||
|
Map<String, Object> inputs = new HashMap<>();
|
||||||
|
inputs.put("file",
|
||||||
|
Collections.singletonList(
|
||||||
|
DifyChatRequest.DifyFileInfo.builder()
|
||||||
|
.type("document")
|
||||||
|
.uploadFileId(uploadResponse.getId())
|
||||||
|
.build()
|
||||||
|
));
|
||||||
|
DifyChatRequest chatRequest = DifyChatRequest.builder()
|
||||||
|
.query("请分析这个文件")
|
||||||
|
.user(DIFY_USER_ID)
|
||||||
|
.inputs(inputs)
|
||||||
|
.files(Lists.emptyList())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
AlitaResult<DifyChatResponse> chatResult = DifyClient.chatMessages(riskRuleApiKey, chatRequest);
|
||||||
|
Assert.isTrue(chatResult.getSuccess(),
|
||||||
|
"调用 Dify Chat 失败,fileCode: " + fileCode + ", error: " + chatResult.getMsg());
|
||||||
|
|
||||||
|
DifyChatResponse chatResponse = chatResult.getData();
|
||||||
|
return chatResponse.getAnswer();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<FileRiskDTO> list(FileRiskQueryDTO queryDTO) {
|
public List<FileRiskDTO> list(FileRiskQueryDTO queryDTO) {
|
||||||
queryDTO.disablePage();
|
queryDTO.disablePage();
|
||||||
@@ -236,12 +285,11 @@ public class FileRiskServiceImpl implements FileRiskService {
|
|||||||
/**
|
/**
|
||||||
* 上传文件到 Dify
|
* 上传文件到 Dify
|
||||||
*
|
*
|
||||||
* @param fileRiskDTO 文件风险DTO
|
|
||||||
* @param fileCode 文件编码
|
* @param fileCode 文件编码
|
||||||
* @param apiKey API密钥
|
* @param apiKey API密钥
|
||||||
* @return DifyFileUploadResponse
|
* @return DifyFileUploadResponse
|
||||||
*/
|
*/
|
||||||
private DifyFileUploadResponse uploadFileToDify(FileRiskDTO fileRiskDTO, String fileCode, String apiKey) {
|
private DifyFileUploadResponse uploadFileToDify(String fileCode, String apiKey) {
|
||||||
// 1. 获取文件流
|
// 1. 获取文件流
|
||||||
FileRecord fileRecord = fileRecordService.find(FileRecord.builder().fileCode(fileCode).build());
|
FileRecord fileRecord = fileRecordService.find(FileRecord.builder().fileCode(fileCode).build());
|
||||||
try(InputStream fileStream = FileStorageUtils.get().download(fileRecord.getFilePath())){
|
try(InputStream fileStream = FileStorageUtils.get().download(fileRecord.getFilePath())){
|
||||||
@@ -259,17 +307,6 @@ public class FileRiskServiceImpl implements FileRiskService {
|
|||||||
"上传文件到 Dify 失败,fileCode: " + fileCode + ", error: " + uploadResult.getMsg());
|
"上传文件到 Dify 失败,fileCode: " + fileCode + ", error: " + uploadResult.getMsg());
|
||||||
|
|
||||||
DifyFileUploadResponse uploadResponse = uploadResult.getData();
|
DifyFileUploadResponse uploadResponse = uploadResult.getData();
|
||||||
String difyFileJson = JsonUtils.toJsonString(uploadResponse);
|
|
||||||
// 3. 更新 difyFileJson 字段
|
|
||||||
FileRisk updateFileRisk = FileRisk.builder()
|
|
||||||
.id(fileRiskDTO.getId())
|
|
||||||
.difyFileJson(difyFileJson)
|
|
||||||
.build();
|
|
||||||
fileRiskMapper.updateEntitySelective(updateFileRisk);
|
|
||||||
|
|
||||||
// 更新 DTO
|
|
||||||
fileRiskDTO.setDifyFileJson(difyFileJson);
|
|
||||||
|
|
||||||
return uploadResponse;
|
return uploadResponse;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new BusinessException(e);
|
throw new BusinessException(e);
|
||||||
|
|||||||
Reference in New Issue
Block a user