修复知识库文件解析和删除异常

This commit is contained in:
baiyanyun
2026-07-13 09:08:07 +08:00
parent f8ee4023ff
commit a69bb4b8a8
10 changed files with 175 additions and 62 deletions

View File

@@ -12,8 +12,11 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Objects;
import java.util.UUID;
@@ -150,6 +153,26 @@ public class FileAccessServiceImpl implements IFileAccessService {
}
}
@Override
public InputStream openFileStream(String fileUrl) throws IOException {
String fileKey = extractFileKeyFromUrl(fileUrl);
if (StringUtils.isNotBlank(fileKey)) {
FileRecordDomain fileRecord = fileManagementService.getFileByKey(fileKey);
if (fileRecord != null) {
try {
return fileManagementService.downloadFile(fileKey);
} catch (RuntimeException e) {
throw new IOException("Failed to open stored file: " + fileKey, e);
}
}
}
URLConnection connection = new URL(fileUrl).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(30000);
return connection.getInputStream();
}
private static String getUrlPath(String fileUrl) throws MalformedURLException {
URL url;
url = new URL(fileUrl);
@@ -172,4 +195,20 @@ public class FileAccessServiceImpl implements IFileAccessService {
return null;
}
private String extractFileKeyFromUrl(String fileUrl) {
if (StringUtils.isBlank(fileUrl)) {
return null;
}
int apiIndex = fileUrl.indexOf("/api/f/");
if (apiIndex < 0) {
return null;
}
String fileKey = fileUrl.substring(apiIndex + "/api/f/".length());
int queryIndex = fileKey.indexOf('?');
if (queryIndex >= 0) {
fileKey = fileKey.substring(0, queryIndex);
}
return StringUtils.isBlank(fileKey) ? null : fileKey;
}
}