修复知识库文件解析和删除异常
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user