This commit is contained in:
Andras Schmelczer 2024-12-20 18:40:09 +00:00
parent 818812aa2d
commit 395b8b6784
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
5 changed files with 79 additions and 55 deletions

View file

@ -33,6 +33,7 @@ export class ObsidianFileOperations implements FileOperations {
newContent: Uint8Array
): Promise<Uint8Array> {
if (!(await this.vault.adapter.exists(normalizePath(path)))) {
// The caller assumed the file exists, but it doesn't, let's not recreate it
return new Uint8Array(0);
}
@ -62,6 +63,7 @@ export class ObsidianFileOperations implements FileOperations {
return;
}
await this.createParentDirectories(normalizePath(path));
await this.vault.adapter.writeBinary(normalizePath(path), newContent);
}
@ -84,4 +86,17 @@ export class ObsidianFileOperations implements FileOperations {
normalizePath(newPath)
);
}
private async createParentDirectories(path: string): Promise<void> {
const components = path.split("/");
if (components.length === 1) {
return;
}
for (let i = 1; i < components.length; i++) {
const parentDir = components.slice(0, i).join("/");
if (!(await this.vault.adapter.exists(parentDir))) {
await this.vault.adapter.mkdir(parentDir);
}
}
}
}