fix(organize): restore both source and target files after partial moves
This commit is contained in:
1 parent
09a4c9fc1e
commit
041d59405c
2 files changed
+68
-8
No files matched your search
@@ -100,7 +100,7 @@ export async function recordSources(d: FmDeps, recordId: string): Promise<FilesR
|
||||
if (!rec) throw new FilesError("not_found", "整理记录不存在");
|
||||
const mains = await db
|
||||
.selectFrom("organization_file_result")
|
||||
.select(["source_path", "target_path", "target_library_root_id"])
|
||||
.select(["source_path", "target_path", "target_library_root_id", "status"])
|
||||
.where("organization_record_id", "=", rec.id)
|
||||
.where("role", "=", "main")
|
||||
.orderBy("ordinal")
|
||||
@@ -115,14 +115,22 @@ export async function recordSources(d: FmDeps, recordId: string): Promise<FilesR
|
||||
};
|
||||
// Successful moves have removed their inputs: reorganize the existing library files.
|
||||
const moved = rec.action === "move" && rec.status === "succeeded";
|
||||
const partialMove = rec.action === "move" && rec.status === "partial";
|
||||
const targetOf = (rootId: string | null, path: string | null) => {
|
||||
const root = defs.find((d) => d.libraryRootId === rootId);
|
||||
return root && path && within(join(root.hostPath, path), root.hostPath) ? join(root.hostPath, path) : null;
|
||||
};
|
||||
let sources = moved
|
||||
? mains.map((m) => targetOf(m.target_library_root_id, m.target_path)).filter((p): p is string => !!p)
|
||||
: mains.map((m) => m.source_path);
|
||||
const dir = moved ? targetOf(rec.target_library_root_id, rec.target_path) : rec.source_path;
|
||||
const targetSources = new Set<string>();
|
||||
let sources = mains
|
||||
.map((m) => {
|
||||
if (!moved && !(partialMove && m.status === "succeeded")) return m.source_path;
|
||||
const target = targetOf(m.target_library_root_id, m.target_path);
|
||||
if (target) targetSources.add(target);
|
||||
return target;
|
||||
})
|
||||
.filter((p): p is string => !!p);
|
||||
// 部分移动的文件分布在媒体库与下载目录,不能折叠成一个目录丢掉已移动文件。
|
||||
const dir = partialMove ? null : moved ? targetOf(rec.target_library_root_id, rec.target_path) : rec.source_path;
|
||||
if (
|
||||
dir &&
|
||||
sources.length > 1 &&
|
||||
@@ -134,7 +142,7 @@ export async function recordSources(d: FmDeps, recordId: string): Promise<FilesR
|
||||
const items: FilesRecordSourcesResult["items"] = [];
|
||||
const unresolved: string[] = [];
|
||||
for (const abs of [...new Set(sources)]) {
|
||||
const ref = moved || rec.source_path_view === "local" ? toRef(abs) : null;
|
||||
const ref = moved || targetSources.has(abs) || rec.source_path_view === "local" ? toRef(abs) : null;
|
||||
const exists = ref
|
||||
? await lstat(abs).then(
|
||||
() => true,
|
||||
|
||||
Reference in new issue
Block a user