fix: missing media files when renaming + saving the project
This commit is contained in:
@@ -7,13 +7,14 @@ import { OPFS_CONSTANTS } from '../../constants/coreConstants';
|
||||
export class KGAudioFileStorage {
|
||||
/**
|
||||
* Store an audio file in the project's media/ directory.
|
||||
* Creates the project and media directories if they don't exist yet.
|
||||
*/
|
||||
public static async storeAudioFile(
|
||||
projectName: string,
|
||||
fileId: string,
|
||||
file: File
|
||||
): Promise<void> {
|
||||
const mediaDir = await KGAudioFileStorage.getMediaDir(projectName);
|
||||
const mediaDir = await KGAudioFileStorage.getOrCreateMediaDir(projectName);
|
||||
const fileHandle = await mediaDir.getFileHandle(fileId, { create: true });
|
||||
const writable = await fileHandle.createWritable();
|
||||
await writable.write(await file.arrayBuffer());
|
||||
@@ -61,7 +62,8 @@ export class KGAudioFileStorage {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the media directory handle for a project.
|
||||
* Get the media directory handle for an existing project (read/delete path).
|
||||
* Does NOT create directories — fails if project folder doesn't exist.
|
||||
*/
|
||||
private static async getMediaDir(projectName: string): Promise<FileSystemDirectoryHandle> {
|
||||
const root = await navigator.storage.getDirectory();
|
||||
@@ -69,4 +71,15 @@ export class KGAudioFileStorage {
|
||||
const projectDir = await projectsDir.getDirectoryHandle(projectName);
|
||||
return projectDir.getDirectoryHandle(OPFS_CONSTANTS.MEDIA_DIR, { create: true });
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create the media directory handle for a project (write path).
|
||||
* Creates project and media directories if they don't exist.
|
||||
*/
|
||||
private static async getOrCreateMediaDir(projectName: string): Promise<FileSystemDirectoryHandle> {
|
||||
const root = await navigator.storage.getDirectory();
|
||||
const projectsDir = await root.getDirectoryHandle(OPFS_CONSTANTS.ROOT_DIR, { create: true });
|
||||
const projectDir = await projectsDir.getDirectoryHandle(projectName, { create: true });
|
||||
return projectDir.getDirectoryHandle(OPFS_CONSTANTS.MEDIA_DIR, { create: true });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user