|
|
@@ -211,30 +211,49 @@ public class CaseInfoService extends ServiceImpl<CaseInfoMapper, CaseInfo> {
|
|
|
Path targetDb = storageLayout.aideskDb(caseId);
|
|
|
|
|
|
Object lock = workspaceCreationLocks.computeIfAbsent(caseId, key -> new Object());
|
|
|
- int copied;
|
|
|
+ CopyStats stats;
|
|
|
+ int dbSkipped;
|
|
|
+ boolean dbExported = false;
|
|
|
synchronized (lock) {
|
|
|
try {
|
|
|
- // 每次刷新:清掉上一次的导入副本,避免 AI 端读到已删除/旧文件
|
|
|
- FileUtil.del(importDir.toFile());
|
|
|
+ // 每次调用都重新复制:只清 import/ 和 db/ 两个目录,aidesk 下其它文件
|
|
|
+ // (可能是 AI 客户端自己生成的)一律不碰。
|
|
|
+ // 被 AI 客户端占用的旧文件删不掉时跳过,保留旧副本。
|
|
|
Files.createDirectories(importDir);
|
|
|
- copied = copyImportFiles(caseId, batchId, importDir);
|
|
|
+ int importSkippedOnClear = clearDirectoryQuietly(importDir);
|
|
|
+ stats = copyImportFiles(caseId, batchId, importDir);
|
|
|
Files.createDirectories(dbDir);
|
|
|
- boolean exported = caseDataSourceRegistry.isOpen(caseId)
|
|
|
- ? caseDataSourceRegistry.exportSnapshot(caseId, targetDb)
|
|
|
- : exportStandaloneSnapshot(sourceDb, targetDb)
|
|
|
- || caseDataSourceRegistry.exportSnapshot(caseId, targetDb);
|
|
|
- if (!exported) {
|
|
|
+ dbSkipped = clearDirectoryQuietly(dbDir);
|
|
|
+ if (Files.exists(targetDb)) {
|
|
|
+ // 旧库副本被占用(删不掉),本次跳过导出,AI 客户端继续用旧副本
|
|
|
+ log.info("aidesk 数据库副本被占用,跳过本次导出: {}", targetDb);
|
|
|
+ } else {
|
|
|
+ dbExported = caseDataSourceRegistry.isOpen(caseId)
|
|
|
+ ? caseDataSourceRegistry.exportSnapshot(caseId, targetDb)
|
|
|
+ : exportStandaloneSnapshot(sourceDb, targetDb)
|
|
|
+ || caseDataSourceRegistry.exportSnapshot(caseId, targetDb);
|
|
|
+ if (!dbExported) {
|
|
|
+ log.warn("aidesk 数据库副本导出失败(可能被占用): {}", targetDb);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!Files.isRegularFile(targetDb)) {
|
|
|
throw new ServerException(500,
|
|
|
- "AI 客户端数据库副本创建失败:请确认 aidesk 目录(尤其 db/case.duckdb)"
|
|
|
- + "未被其他进程占用,且案件原库未被其他进程锁住后重试");
|
|
|
+ "AI 客户端数据库副本不可用:请关闭占用 aidesk/db/case.duckdb 的进程后重试");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ writeAideskCaseJson(caseId, caseInfo, stats.copied());
|
|
|
+ } catch (IOException e) {
|
|
|
+ // case.json 被占用时不影响副本本身,跳过本次说明文件刷新
|
|
|
+ log.info("aidesk/case.json 写入失败(可能被占用),跳过: {}", e.getMessage());
|
|
|
}
|
|
|
- writeAideskCaseJson(caseId, caseInfo, copied);
|
|
|
+ log.info("AI 客户端工作区已刷新: caseId={}, batchId={}, importCopied={}, "
|
|
|
+ + "importSkipped={}, dbExported={}, dbSkipped={}",
|
|
|
+ caseId, batchId, stats.copied(), stats.skipped() + importSkippedOnClear,
|
|
|
+ dbExported, dbSkipped);
|
|
|
} catch (IOException e) {
|
|
|
throw new ServerException(500, "AI 客户端工作区创建失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
- log.info("AI 客户端工作区已创建: caseId={}, batchId={}, importFiles={}, db={}",
|
|
|
- caseId, batchId, copied, targetDb);
|
|
|
return PublicAideskVO.of(caseInfo, aideskDir);
|
|
|
}
|
|
|
|
|
|
@@ -274,12 +293,12 @@ public class CaseInfoService extends ServiceImpl<CaseInfoMapper, CaseInfo> {
|
|
|
* 复制上传原件到 {@code aidesk/import}。
|
|
|
*
|
|
|
* <p>上传目录下的文件都是平铺的;指定批次只复制该目录,未指定时遍历全部批次。
|
|
|
- * 跨批次同名时追加 {@code -N},避免平铺覆盖(新命名带雪花 ID,正常不会撞名)。
|
|
|
+ * 目标文件被 AI 客户端占用(删不掉/覆盖不了)时跳过该文件,不影响其它文件复制。
|
|
|
*/
|
|
|
- private int copyImportFiles(Long caseId, Long batchId, Path targetDir) throws IOException {
|
|
|
+ private CopyStats copyImportFiles(Long caseId, Long batchId, Path targetDir) throws IOException {
|
|
|
Path importsRoot = storageLayout.caseImportsDir(caseId);
|
|
|
if (!Files.isDirectory(importsRoot)) {
|
|
|
- return 0;
|
|
|
+ return new CopyStats(0, 0);
|
|
|
}
|
|
|
List<Path> batchDirs;
|
|
|
if (batchId != null) {
|
|
|
@@ -296,39 +315,77 @@ public class CaseInfoService extends ServiceImpl<CaseInfoMapper, CaseInfo> {
|
|
|
.toList();
|
|
|
}
|
|
|
}
|
|
|
- int count = 0;
|
|
|
+ int copied = 0;
|
|
|
+ int skipped = 0;
|
|
|
for (Path batchDir : batchDirs) {
|
|
|
+ List<Path> files;
|
|
|
try (Stream<Path> stream = Files.list(batchDir)) {
|
|
|
- List<Path> files = stream.filter(Files::isRegularFile)
|
|
|
+ files = stream.filter(Files::isRegularFile)
|
|
|
.filter(path -> !Files.isSymbolicLink(path))
|
|
|
.sorted()
|
|
|
.toList();
|
|
|
- for (Path file : files) {
|
|
|
- Path target = targetDir.resolve(file.getFileName().toString());
|
|
|
- if (Files.exists(target)) {
|
|
|
- target = uniqueCopyTarget(targetDir, file.getFileName().toString());
|
|
|
- }
|
|
|
+ } catch (IOException e) {
|
|
|
+ skipped++;
|
|
|
+ log.info("aidesk/import 批次目录读取失败,跳过: dir={}, error={}",
|
|
|
+ batchDir, e.getMessage());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (Path file : files) {
|
|
|
+ Path target = targetDir.resolve(file.getFileName().toString());
|
|
|
+ try {
|
|
|
Files.copy(file, target, StandardCopyOption.REPLACE_EXISTING);
|
|
|
- count++;
|
|
|
+ copied++;
|
|
|
+ } catch (IOException e) {
|
|
|
+ // 目标文件被 AI 客户端占用(Windows 不允许覆盖)时保留旧文件,跳过
|
|
|
+ skipped++;
|
|
|
+ log.info("aidesk/import 文件被占用或复制失败,跳过: source={}, error={}",
|
|
|
+ file, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return new CopyStats(copied, skipped);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清空目录内容(保留目录本身);被占用的文件跳过并计数。
|
|
|
+ *
|
|
|
+ * <p>只用于 {@code aidesk/import} 与 {@code aidesk/db}:{@code aidesk} 下
|
|
|
+ * 其它文件可能是 AI 客户端生成的,不能整体删除。
|
|
|
+ */
|
|
|
+ private int clearDirectoryQuietly(Path dir) {
|
|
|
+ if (!Files.isDirectory(dir)) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ int skipped = 0;
|
|
|
+ try (Stream<Path> stream = Files.list(dir)) {
|
|
|
+ for (Path child : stream.toList()) {
|
|
|
+ try {
|
|
|
+ deleteRecursively(child);
|
|
|
+ } catch (IOException e) {
|
|
|
+ skipped++;
|
|
|
+ log.info("aidesk 副本被占用,跳过删除: {}", child);
|
|
|
}
|
|
|
}
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("读取 aidesk 副本目录失败: dir={}, error={}", dir, e.getMessage());
|
|
|
}
|
|
|
- return count;
|
|
|
+ return skipped;
|
|
|
}
|
|
|
|
|
|
- /** 平铺复制时的同名兜底:{@code name-1.ext}、{@code name-2.ext}… */
|
|
|
- private static Path uniqueCopyTarget(Path dir, String name) {
|
|
|
- int dot = name.lastIndexOf('.');
|
|
|
- String base = dot > 0 ? name.substring(0, dot) : name;
|
|
|
- String ext = dot > 0 ? name.substring(dot) : "";
|
|
|
- for (int i = 1; i < 1000; i++) {
|
|
|
- Path candidate = dir.resolve(base + "-" + i + ext);
|
|
|
- if (!Files.exists(candidate)) {
|
|
|
- return candidate;
|
|
|
+ private static void deleteRecursively(Path path) throws IOException {
|
|
|
+ if (!Files.isDirectory(path)) {
|
|
|
+ Files.deleteIfExists(path);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try (Stream<Path> walk = Files.walk(path)) {
|
|
|
+ for (Path item : walk.sorted(java.util.Comparator.reverseOrder()).toList()) {
|
|
|
+ Files.deleteIfExists(item);
|
|
|
}
|
|
|
}
|
|
|
- return dir.resolve(base + "-"
|
|
|
- + UUID.randomUUID().toString().replace("-", "").substring(0, 8) + ext);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 本次 import 复制统计:成功/跳过(被占用或读取失败)。 */
|
|
|
+ private record CopyStats(int copied, int skipped) {
|
|
|
}
|
|
|
|
|
|
/**
|