Repository navigation
feat(recording): delete a project's recordings along with the project #1381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marcgabe15
wants to merge
4
commits into
getopenscreen:main
Choose a base branch
from
marcgabe15:feat/delete-project-recordings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,096
−59
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c496187
feat(delete): add delete recordings option
marcgabe15 5f10431
fix(delete): keep a project's recordings while another project file d…
marcgabe15 1ba0138
fix(delete): accept only known recordings disposals from the renderer
marcgabe15 94acdfe
fix(delete): offer a permanent delete only when the Trash refused, an…
marcgabe15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import fs from "node:fs/promises"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { DocumentService } from "./document-service"; | ||
| import { findProjectRecordings, removeRecordingSets } from "./project-recordings"; | ||
|
|
||
| async function touch(filePath: string, content = "x"): Promise<string> { | ||
| await fs.writeFile(filePath, content); | ||
| return filePath; | ||
| } | ||
|
|
||
| describe("project recordings", () => { | ||
| let root: string; | ||
| let recordingsDir: string; | ||
| let elsewhere: string; | ||
| let documents: DocumentService; | ||
|
|
||
| beforeEach(async () => { | ||
| root = await fs.mkdtemp(path.join(os.tmpdir(), "openscreen-project-recordings-")); | ||
| recordingsDir = path.join(root, "recordings"); | ||
| elsewhere = path.join(root, "Desktop"); | ||
| await fs.mkdir(recordingsDir); | ||
| await fs.mkdir(elsewhere); | ||
| documents = new DocumentService(path.join(root, "projects"), root); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await fs.rm(root, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| async function projectWith(title: string, ...videoPaths: string[]): Promise<string> { | ||
| const doc = await documents.createProject(title); | ||
| for (const videoPath of videoPaths) { | ||
| await documents.addAsset(doc.project.id, { | ||
| path: videoPath, | ||
| label: path.basename(videoPath), | ||
| }); | ||
| } | ||
| return doc.project.id; | ||
| } | ||
|
|
||
| it("owns every file recorded beside the screen video", async () => { | ||
| const video = await touch(path.join(recordingsDir, "recording-12.mp4"), "12345"); | ||
| const companions = [ | ||
| await touch(`${video}.cursor.json`), | ||
| await touch(path.join(recordingsDir, "recording-12.session.json")), | ||
| await touch(path.join(recordingsDir, "recording-12-webcam.webm")), | ||
| ]; | ||
| await touch(path.join(recordingsDir, "recording-123.mp4")); | ||
| const id = await projectWith("Mine", video); | ||
|
|
||
| const { owned, keptFor } = await findProjectRecordings( | ||
| id, | ||
| await documents.readAllProjectsReport(), | ||
| [recordingsDir], | ||
| ); | ||
|
|
||
| expect(owned).toHaveLength(1); | ||
| expect(owned[0].files.sort()).toEqual([video, ...companions].sort()); | ||
| expect(owned[0].sizeBytes).toBe(5 + 3); | ||
| expect(keptFor).toEqual([]); | ||
| }); | ||
|
|
||
| it("keeps a recording another project uses, and names that project", async () => { | ||
| const shared = await touch(path.join(recordingsDir, "recording-1.mp4")); | ||
| const own = await touch(path.join(recordingsDir, "recording-2.mp4")); | ||
| const id = await projectWith("Mine", shared, own); | ||
| await projectWith("Demo v2", shared); | ||
|
|
||
| const { owned, keptFor } = await findProjectRecordings( | ||
| id, | ||
| await documents.readAllProjectsReport(), | ||
| [recordingsDir], | ||
| ); | ||
|
|
||
| expect(owned.map((set) => set.stem)).toEqual(["recording-2"]); | ||
| expect(keptFor).toEqual(["Demo v2"]); | ||
| }); | ||
|
|
||
| it("keeps every recording while a project file does not parse", async () => { | ||
| const video = await touch(path.join(recordingsDir, "recording-9.mp4")); | ||
| const id = await projectWith("Mine", video); | ||
| // Could be a project using recording-9: a newer schema, or a save cut short. | ||
| await touch(path.join(root, "projects", "broken.openscreen"), "{ not json"); | ||
|
|
||
| const { owned, keptFor, unreadable } = await findProjectRecordings( | ||
| id, | ||
| await documents.readAllProjectsReport(), | ||
| [recordingsDir], | ||
| ); | ||
|
|
||
| expect(owned).toEqual([]); | ||
| expect(keptFor).toEqual([]); | ||
| expect(unreadable).toBe(1); | ||
| }); | ||
|
|
||
| it("reports no unreadable projects when it has no recordings to keep", async () => { | ||
| const imported = await touch(path.join(elsewhere, "clip.mp4")); | ||
| const id = await projectWith("Imported", imported); | ||
| await touch(path.join(root, "projects", "broken.openscreen"), "{ not json"); | ||
|
|
||
| const { unreadable } = await findProjectRecordings( | ||
| id, | ||
| await documents.readAllProjectsReport(), | ||
| [recordingsDir], | ||
| ); | ||
|
|
||
| expect(unreadable).toBe(0); | ||
| }); | ||
|
|
||
| it("never owns a video outside the recordings folders", async () => { | ||
| const imported = await touch(path.join(elsewhere, "recording-5.mp4")); | ||
| const id = await projectWith("Imported", imported); | ||
|
|
||
| const { owned } = await findProjectRecordings(id, await documents.readAllProjectsReport(), [ | ||
| recordingsDir, | ||
| ]); | ||
|
|
||
| expect(owned).toEqual([]); | ||
| }); | ||
|
|
||
| it("trashes each file, or removes it for good when asked", async () => { | ||
| const video = await touch(path.join(recordingsDir, "recording-7.mp4")); | ||
| const cursor = await touch(`${video}.cursor.json`); | ||
| const id = await projectWith("Mine", video); | ||
| const { owned } = await findProjectRecordings(id, await documents.readAllProjectsReport(), [ | ||
| recordingsDir, | ||
| ]); | ||
|
|
||
| const trashItem = vi.fn(async (_filePath: string) => {}); | ||
| await removeRecordingSets(owned, "trash", trashItem); | ||
| expect(trashItem.mock.calls.map(([p]) => p).sort()).toEqual([video, cursor].sort()); | ||
| await expect(fs.access(video)).resolves.toBeUndefined(); | ||
|
|
||
| await removeRecordingSets(owned, "permanent", trashItem); | ||
| await expect(fs.access(video)).rejects.toThrow(); | ||
| await expect(fs.access(cursor)).rejects.toThrow(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import fs from "node:fs/promises"; | ||
| import path from "node:path"; | ||
| import type { AxcutDocument } from "../../src/lib/ai-edition/schema"; | ||
|
|
||
| const RECORDING_STEM = /^recording-\d+/; | ||
|
|
||
| /** One take: the screen video and every file recorded beside it (cursor, session, camera). */ | ||
| export interface RecordingSet { | ||
| dir: string; | ||
| stem: string; | ||
| files: string[]; | ||
| sizeBytes: number; | ||
| } | ||
|
|
||
| export interface ProjectRecordings { | ||
| owned: RecordingSet[]; | ||
| /** Titles of the other projects that use some of this project's recordings. */ | ||
| keptFor: string[]; | ||
| /** | ||
| * Project files that did not parse. Any of them may use these recordings, so while there | ||
| * are some the project owns none: its recordings are all kept. | ||
| */ | ||
| unreadable: number; | ||
| } | ||
|
|
||
| export type RecordingsDisposal = "keep" | "trash" | "permanent"; | ||
|
|
||
| function documentMediaPaths(doc: AxcutDocument): string[] { | ||
| return doc.assets.flatMap((asset) => | ||
| [asset.originalPath, asset.proxyPath, asset.waveformPath, asset.cameraTrack?.sourcePath].filter( | ||
| (p): p is string => typeof p === "string" && p.length > 0, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| // `path.relative` compares case-insensitively on Windows, which a string compare would not. | ||
| function sameDir(a: string, b: string): boolean { | ||
| return path.relative(path.resolve(a), path.resolve(b)) === ""; | ||
| } | ||
|
|
||
| function recordingKey(filePath: string, recordingsDirs: readonly string[]): string | null { | ||
| const dir = path.dirname(path.resolve(filePath)); | ||
| const managedDir = recordingsDirs.find((candidate) => sameDir(candidate, dir)); | ||
| if (!managedDir) return null; | ||
| const stem = RECORDING_STEM.exec(path.basename(filePath))?.[0]; | ||
| return stem ? path.join(path.resolve(managedDir), stem) : null; | ||
| } | ||
|
|
||
| function documentRecordingKeys(doc: AxcutDocument, recordingsDirs: readonly string[]): Set<string> { | ||
| const keys = new Set<string>(); | ||
| for (const mediaPath of documentMediaPaths(doc)) { | ||
| const key = recordingKey(mediaPath, recordingsDirs); | ||
| if (key) keys.add(key); | ||
| } | ||
| return keys; | ||
| } | ||
|
|
||
| async function readRecordingSet(key: string): Promise<RecordingSet | null> { | ||
| const dir = path.dirname(key); | ||
| const stem = path.basename(key); | ||
| let names: string[]; | ||
| try { | ||
| names = await fs.readdir(dir); | ||
| } catch { | ||
| return null; | ||
| } | ||
| const files: string[] = []; | ||
| let sizeBytes = 0; | ||
| for (const name of names) { | ||
| // `recording-12` must not pick up `recording-123`. | ||
| if (name !== stem && !name.startsWith(`${stem}.`) && !name.startsWith(`${stem}-`)) continue; | ||
| const filePath = path.join(dir, name); | ||
| try { | ||
| const stat = await fs.stat(filePath); | ||
| if (!stat.isFile()) continue; | ||
| files.push(filePath); | ||
| sizeBytes += stat.size; | ||
| } catch { | ||
| // Gone between the listing and the stat: nothing left to delete. | ||
| } | ||
| } | ||
| return files.length > 0 ? { dir, stem, files, sizeBytes } : null; | ||
| } | ||
|
|
||
| /** | ||
| * The recordings `projectId` owns: takes in one of OpenScreen's recordings folders that no | ||
| * other project uses. A video imported from anywhere else is never the project's to delete. | ||
| */ | ||
| export async function findProjectRecordings( | ||
| projectId: string, | ||
| projects: { documents: readonly AxcutDocument[]; unreadable: readonly string[] }, | ||
| recordingsDirs: readonly string[], | ||
| ): Promise<ProjectRecordings> { | ||
| const { documents } = projects; | ||
| const target = documents.find((doc) => doc.project.id === projectId); | ||
| if (!target) return { owned: [], keptFor: [], unreadable: 0 }; | ||
|
|
||
| const keys = documentRecordingKeys(target, recordingsDirs); | ||
| if (keys.size > 0 && projects.unreadable.length > 0) { | ||
| return { owned: [], keptFor: [], unreadable: projects.unreadable.length }; | ||
| } | ||
| const keptFor = new Set<string>(); | ||
| for (const doc of documents) { | ||
| if (doc.project.id === projectId) continue; | ||
| const otherKeys = documentRecordingKeys(doc, recordingsDirs); | ||
| for (const key of keys) { | ||
| if (otherKeys.has(key)) { | ||
| keys.delete(key); | ||
| keptFor.add(doc.project.title); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const owned: RecordingSet[] = []; | ||
| for (const key of keys) { | ||
| const set = await readRecordingSet(key); | ||
| if (set) owned.push(set); | ||
| } | ||
| return { owned, keptFor: [...keptFor], unreadable: 0 }; | ||
| } | ||
|
|
||
| /** Throws on the first file that cannot be removed, so a failed trash can be retried permanently. */ | ||
| export async function removeRecordingSets( | ||
| sets: readonly RecordingSet[], | ||
| disposal: Exclude<RecordingsDisposal, "keep">, | ||
| trashItem: (filePath: string) => Promise<void>, | ||
| ): Promise<void> { | ||
| for (const set of sets) { | ||
| for (const filePath of set.files) { | ||
| if (disposal === "trash") { | ||
| await trashItem(filePath); | ||
| } else { | ||
| await fs.rm(filePath, { force: true }); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.