Skip to content

Commit cc92d35

Browse files
committed
fix(delete): offer a permanent delete only when the Trash refused, and never strand a project
trashFailed was set for any error in the disposal block, so a failed project scan offered "Delete them permanently?". It is now set only when removing the recordings throws. The project file is moved aside before any recording is removed: a project that cannot be deleted fails before anything irreversible, and it is put back if the Trash refuses.
1 parent afd097a commit cc92d35

3 files changed

Lines changed: 64 additions & 12 deletions

File tree

‎electron/ai-edition/document-service.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,28 @@ export class DocumentService {
587587
}
588588
}
589589

590+
/**
591+
* Deletes the project once `removeMedia` has run. The file is moved aside first, so a project
592+
* that cannot be deleted fails before anything irreversible, and it comes back if
593+
* `removeMedia` throws.
594+
*/
595+
async deleteProjectAfter(projectId: string, removeMedia: () => Promise<void>): Promise<void> {
596+
const filePath = this.fileFor(projectId);
597+
const parked = `${filePath}.deleting`;
598+
await renameWithRetry(filePath, parked);
599+
try {
600+
await removeMedia();
601+
} catch (error) {
602+
await renameWithRetry(parked, filePath);
603+
throw error;
604+
}
605+
// The project is gone from every list already: a parked file left behind is only clutter.
606+
await fs
607+
.rm(parked, { force: true })
608+
.catch((error) => console.warn(`[ai-edition] could not remove ${parked}:`, error));
609+
await this.deleteProject(projectId);
610+
}
611+
590612
async addAsset(projectId: string, input: AddAssetInput): Promise<AxcutDocument> {
591613
const doc = await this.getProject(projectId);
592614
const unsupported = unsupportedAssetPath(input);

‎electron/native-bridge/services/aiEditionService.deleteRecordings.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,27 @@ describe("AiEditionService — deleting a project's recordings", () => {
9090
await expect(documents.getProject(projectId)).resolves.toBeDefined();
9191
});
9292

93+
it("does not offer a permanent delete when the scan failed, not the Trash", async () => {
94+
vi.spyOn(documents, "readAllProjectsReport").mockRejectedValueOnce(new Error("EACCES"));
95+
96+
const result = await service().deleteProject(projectId, "trash");
97+
expect(result).toMatchObject({ success: false });
98+
expect(result.trashFailed).toBeFalsy();
99+
expect(trashItem).not.toHaveBeenCalled();
100+
});
101+
102+
it("removes no recording when the project file cannot be deleted", async () => {
103+
// A directory in the way makes moving the project file aside fail.
104+
await fs.mkdir(path.join(root, "projects", `${projectId}.openscreen.deleting`));
105+
106+
const result = await service().deleteProject(projectId, "trash");
107+
expect(result).toMatchObject({ success: false });
108+
expect(result.trashFailed).toBeFalsy();
109+
expect(trashItem).not.toHaveBeenCalled();
110+
await expect(fs.access(video)).resolves.toBeUndefined();
111+
await expect(documents.getProject(projectId)).resolves.toBeDefined();
112+
});
113+
93114
it("keeps everything when the Trash refuses, then deletes permanently on request", async () => {
94115
trashItem.mockRejectedValueOnce(new Error("no trash on this filesystem"));
95116

‎electron/native-bridge/services/aiEditionService.ts‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,10 @@ export class AiEditionService {
190190
disposal: AiEditionRecordingsDisposal = "keep",
191191
): Promise<AiEditionDeleteResult> {
192192
const recordings = this.options.recordings;
193-
// The recordings go first: a project whose files could not be removed is kept, so
194-
// the user can retry, instead of leaving takes that nothing points at any more.
193+
// The recordings go before the project: a project whose files could not be removed is
194+
// kept, so the user can retry, instead of leaving takes that nothing points at any more.
195195
if (disposal !== "keep" && recordings) {
196+
let removing = false;
196197
try {
197198
const { owned, unreadable } = await findProjectRecordings(
198199
projectId,
@@ -207,26 +208,34 @@ export class AiEditionService {
207208
error: `${unreadable} project file(s) could not be read, so recordings they may use were not deleted`,
208209
};
209210
}
210-
await removeRecordingSets(owned, disposal, recordings.trashItem);
211+
// The project file is moved aside before any recording goes, so a project that
212+
// cannot be deleted never ends up pointing at recordings that are gone.
213+
await this.options.documents.deleteProjectAfter(projectId, async () => {
214+
removing = true;
215+
await removeRecordingSets(owned, disposal, recordings.trashItem);
216+
removing = false;
217+
});
211218
await forgetMediaLinks(
212219
recordings.registryDir(),
213220
owned.flatMap((set) => set.files),
214221
).catch((error) => console.warn("[ai-edition] could not update media links:", error));
215222
} catch (error) {
216223
return {
217224
success: false,
218-
trashFailed: disposal === "trash",
225+
// Only the Trash refusing earns the offer to delete permanently, not a failed scan.
226+
trashFailed: removing && disposal === "trash",
227+
error: error instanceof Error ? error.message : String(error),
228+
};
229+
}
230+
} else {
231+
try {
232+
await this.options.documents.deleteProject(projectId);
233+
} catch (error) {
234+
return {
235+
success: false,
219236
error: error instanceof Error ? error.message : String(error),
220237
};
221238
}
222-
}
223-
try {
224-
await this.options.documents.deleteProject(projectId);
225-
} catch (error) {
226-
return {
227-
success: false,
228-
error: error instanceof Error ? error.message : String(error),
229-
};
230239
}
231240
try {
232241
await this.options.deleteChatHistory?.(projectId);

0 commit comments

Comments
 (0)