Add more file operations

This commit is contained in:
Andras Schmelczer 2024-12-18 20:35:36 +00:00
commit c0fd041532
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
2 changed files with 15 additions and 0 deletions

View file

@ -1,8 +1,12 @@
import { RelativePath } from "src/database/document-metadata"; import { RelativePath } from "src/database/document-metadata";
export interface FileOperations { export interface FileOperations {
listAllFiles(): Promise<RelativePath[]>;
read(path: RelativePath): Promise<Uint8Array>; read(path: RelativePath): Promise<Uint8Array>;
getModificationTime(path: RelativePath): Promise<Date>;
create(path: RelativePath, newContent: Uint8Array): Promise<void>; create(path: RelativePath, newContent: Uint8Array): Promise<void>;
// Writes new content to the file at the given path. If the file's content has changed since the expectedContent was read, the write will merge the changes. // Writes new content to the file at the given path. If the file's content has changed since the expectedContent was read, the write will merge the changes.

View file

@ -7,12 +7,23 @@ import { RelativePath } from "src/database/document-metadata";
export class ObsidianFileOperations implements FileOperations { export class ObsidianFileOperations implements FileOperations {
public constructor(private vault: Vault) {} public constructor(private vault: Vault) {}
async listAllFiles(): Promise<RelativePath[]> {
const files = this.vault.getFiles();
return files.map((file) => file.path);
}
async read(path: RelativePath): Promise<Uint8Array> { async read(path: RelativePath): Promise<Uint8Array> {
return new Uint8Array( return new Uint8Array(
await this.vault.adapter.readBinary(normalizePath(path)) await this.vault.adapter.readBinary(normalizePath(path))
); );
} }
async getModificationTime(path: RelativePath): Promise<Date> {
return new Date(
(await this.vault.adapter.stat(normalizePath(path)))!.mtime
);
}
async write( async write(
path: RelativePath, path: RelativePath,
expectedContent: Uint8Array, expectedContent: Uint8Array,