|
|
@@ -25,9 +25,11 @@ import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.net.URI;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
+import java.nio.file.StandardCopyOption;
|
|
|
import java.security.MessageDigest;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.time.LocalDate;
|
|
|
@@ -61,6 +63,13 @@ public class JdCloudStorageService implements StorageService {
|
|
|
@Value("${jdcloud.region:cn-north-1}")
|
|
|
private String region;
|
|
|
|
|
|
+ /**
|
|
|
+ * 本地共享目录。京东云对象在供本地进程(如 cfc-langgraph 容器)按路径读取时,
|
|
|
+ * 会被下载到该目录下的缓存子目录,确保 LangGraph 能读到文件。
|
|
|
+ */
|
|
|
+ @Value("${upload.base-dir:/data/cfc-uploads}")
|
|
|
+ private String uploadBaseDir;
|
|
|
+
|
|
|
@Resource
|
|
|
private FileRecordMapper fileRecordMapper;
|
|
|
|
|
|
@@ -153,25 +162,35 @@ public class JdCloudStorageService implements StorageService {
|
|
|
|
|
|
@Override
|
|
|
public String getFilePath(String filename) {
|
|
|
- // 京东云文件不在本地磁盘,下载到临时目录供本地解析使用
|
|
|
+ // 京东云文件不在本地磁盘:下载到共享卷 upload.base-dir 下的缓存目录,
|
|
|
+ // 供本地进程(含 cfc-langgraph 容器)按本地路径读取;缓存目录需可被其它用户遍历/读取。
|
|
|
try {
|
|
|
String key = extractKey(filename);
|
|
|
- S3Object object = s3Client.getObject(bucket, key);
|
|
|
-
|
|
|
- Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"), "cfc-jdcloud");
|
|
|
- Files.createDirectories(tempDir);
|
|
|
- String localName = key.substring(key.lastIndexOf('/') + 1);
|
|
|
- Path tempFile = tempDir.resolve(UUID.randomUUID().toString() + "-" + localName);
|
|
|
+ Path cacheDir = Paths.get(uploadBaseDir, ".jdcloud-cache").toAbsolutePath().normalize();
|
|
|
+ Files.createDirectories(cacheDir);
|
|
|
+ cacheDir.toFile().setReadable(true, false);
|
|
|
+ cacheDir.toFile().setExecutable(true, false);
|
|
|
+
|
|
|
+ String localName = sha256(key.getBytes(StandardCharsets.UTF_8))
|
|
|
+ + "-" + key.substring(key.lastIndexOf('/') + 1);
|
|
|
+ Path cached = cacheDir.resolve(localName);
|
|
|
+ if (Files.exists(cached) && Files.size(cached) > 0) {
|
|
|
+ return cached.toString();
|
|
|
+ }
|
|
|
|
|
|
+ S3Object object = s3Client.getObject(bucket, key);
|
|
|
+ Path tmp = cacheDir.resolve(localName + ".part");
|
|
|
try (InputStream in = object.getObjectContent();
|
|
|
- FileOutputStream out = new FileOutputStream(tempFile.toFile())) {
|
|
|
+ FileOutputStream out = new FileOutputStream(tmp.toFile())) {
|
|
|
byte[] buffer = new byte[8192];
|
|
|
int len;
|
|
|
while ((len = in.read(buffer)) != -1) {
|
|
|
out.write(buffer, 0, len);
|
|
|
}
|
|
|
}
|
|
|
- return tempFile.toString();
|
|
|
+ Files.move(tmp, cached, StandardCopyOption.REPLACE_EXISTING);
|
|
|
+ cached.toFile().setReadable(true, false);
|
|
|
+ return cached.toString();
|
|
|
} catch (IOException e) {
|
|
|
throw new RuntimeException("下载京东云文件失败: " + filename, e);
|
|
|
}
|