Handle file events

This commit is contained in:
Andras Schmelczer 2024-12-10 21:37:45 +00:00
commit 6173320f81
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
2 changed files with 151 additions and 41 deletions

View file

@ -1,27 +1,106 @@
import { TAbstractFile, TFile } from "obsidian"; import { TAbstractFile, TFile } from "obsidian";
import { FileEventHandler } from "./file-event-handler"; import { FileEventHandler } from "./file-event-handler";
import { Logger } from "src/logger"; import { Logger } from "src/logger";
import { Syncer } from "src/syncer/syncer"; import { SyncServer } from "src/services/sync_service";
import { Database } from "src/database/database";
export class SyncEventHandler implements FileEventHandler { export class SyncEventHandler implements FileEventHandler {
constructor(private syncer: Syncer) {} constructor(private database: Database, private syncServer: SyncServer) {}
async onCreate(file: TAbstractFile) { async onCreate(file: TAbstractFile): Promise<void> {
if (file instanceof TFile) { if (file instanceof TFile) {
Logger.getInstance().info(`File created: ${file}`); Logger.getInstance().info(`File created: ${file.path}`);
this.syncer.onCreate(file.path, await file.vault.read(file));
const result = await this.syncServer.create({
relativePath: file.path,
content: await file.vault.readBinary(file),
createdDate: new Date(file.stat.ctime),
});
await this.database.setDocument({
relativePath: file.path,
documentId: result.documentId,
parentVersionId: result.versionId,
});
} else {
Logger.getInstance().info(`Folder created: ${file.path}, ignored`);
} }
} }
onDelete(file: TAbstractFile) { async onDelete(file: TAbstractFile): Promise<void> {
Logger.getInstance().info(`File deleted: ${file}`); if (file instanceof TFile) {
Logger.getInstance().info(`File deleted: ${file.path}`);
const metadata = this.database.getDocument(file.path);
if (!metadata) {
throw `Document metadata not found for ${file.path}`;
} }
onRename(file: TAbstractFile, oldPath: string) { await this.syncServer.delete({
Logger.getInstance().info(`File renamed: ${oldPath} -> ${file}`); documentId: metadata.documentId,
createdDate: new Date(), // We got the event now, so it must have been deleted now
});
await this.database.removeDocument(file.path);
} else {
Logger.getInstance().info(`Folder deleted: ${file.path}, ignored`);
}
} }
onModify(file: TAbstractFile) { async onRename(file: TAbstractFile, oldPath: string): Promise<void> {
Logger.getInstance().info(`File modified: ${file}`); Logger.getInstance().info(`File renamed: ${oldPath} -> ${file.path}`);
if (file instanceof TFile) {
const metadata = this.database.getDocument(oldPath);
if (!metadata) {
throw `Document metadata not found for ${oldPath}`;
}
const response = await this.syncServer.update({
documentId: metadata.documentId,
parentVersionId: metadata.parentVersionId,
relativePath: file.path,
content: await file.vault.readBinary(file),
createdDate: new Date(file.stat.ctime),
});
await this.database.moveDocument({
oldRelativePath: oldPath,
relativePath: file.path,
documentId: response.documentId,
parentVersionId: response.versionId,
});
} else {
Logger.getInstance().info(
`Folder renamed: ${oldPath} -> ${file.path}, ignored`
);
}
}
async onModify(file: TAbstractFile): Promise<void> {
Logger.getInstance().info(`File modified: ${file.path}`);
if (file instanceof TFile) {
const metadata = this.database.getDocument(file.path);
if (!metadata) {
throw `Document metadata not found for ${file.path}`;
}
const response = await this.syncServer.update({
documentId: metadata.documentId,
parentVersionId: metadata.parentVersionId,
relativePath: file.path,
content: await file.vault.readBinary(file),
createdDate: new Date(file.stat.ctime),
});
await this.database.setDocument({
relativePath: file.path,
documentId: response.documentId,
parentVersionId: response.versionId,
});
} else {
Logger.getInstance().info(`Folder modified: ${file.path}, ignored`);
}
} }
} }

View file

@ -1,22 +1,22 @@
import {
SettingsContainer,
SyncSettings,
} from "src/database/settings/settings.js";
import * as plugin from "../../../backend/sync_lib/pkg/sync_lib.js"; import * as plugin from "../../../backend/sync_lib/pkg/sync_lib.js";
import createClient, { Client } from "openapi-fetch"; import createClient, { Client } from "openapi-fetch";
import type { components, paths } from "./types.js"; // generated by openapi-typescript import type { components, paths } from "./types.js"; // generated by openapi-typescript
import { Logger } from "src/logger.js"; import { Logger } from "src/logger.js";
import { DocumentId, DocumentVersionId } from "src/database/database.js"; import {
Database,
DocumentId,
DocumentVersionId,
SyncSettings,
} from "src/database/database.js";
export class SyncServer { export class SyncServer {
private static VAULT_ID = "default"; private static VAULT_ID = "default";
private client: Client<paths>; private client: Client<paths>;
public constructor(private settings: SettingsContainer) { public constructor(private database: Database) {
this.createClient(settings.getSettings()); this.createClient(database.getSettings());
settings.onChange((s) => this.createClient(s)); database.addOnSettingsChangeHandlers((s) => this.createClient(s));
} }
private createClient(settings: SyncSettings) { private createClient(settings: SyncSettings) {
@ -25,6 +25,20 @@ export class SyncServer {
}); });
} }
public async ping(): Promise<components["schemas"]["PingResponse"]> {
const response = await this.client.GET("/ping");
Logger.getInstance().info(
"Ping response: " + JSON.stringify(response.data)
);
if (!response.data) {
throw new Error(`Failed to ping server: ${response.error}`);
}
return response.data;
}
public async create({ public async create({
relativePath, relativePath,
content, content,
@ -33,16 +47,14 @@ export class SyncServer {
content: ArrayBuffer; content: ArrayBuffer;
relativePath: string; relativePath: string;
createdDate: Date; createdDate: Date;
}): Promise< }): Promise<components["schemas"]["DocumentVersionWithoutContent"]> {
components["schemas"]["DocumentVersionWithoutContent"] | undefined
> {
let contentBytes = new Uint8Array(content); let contentBytes = new Uint8Array(content);
let response = await this.client.POST("/vaults/{vault_id}/documents", { let response = await this.client.POST("/vaults/{vault_id}/documents", {
params: { params: {
path: { vaultId: SyncServer.VAULT_ID }, path: { vault_id: SyncServer.VAULT_ID },
header: { header: {
authorization: authorization:
"Bearer " + this.settings.getSettings().token, "Bearer " + this.database.getSettings().token,
}, },
}, },
body: { body: {
@ -53,6 +65,10 @@ export class SyncServer {
}, },
}); });
if (!response.data) {
throw new Error(`Failed to create document: ${response.error}`);
}
Logger.getInstance().info( Logger.getInstance().info(
"Created document " + JSON.stringify(response.data) "Created document " + JSON.stringify(response.data)
); );
@ -72,9 +88,7 @@ export class SyncServer {
relativePath: string; relativePath: string;
content: ArrayBuffer; content: ArrayBuffer;
createdDate: Date; createdDate: Date;
}): Promise< }): Promise<components["schemas"]["DocumentVersionWithoutContent"]> {
components["schemas"]["DocumentVersionWithoutContent"] | undefined
> {
let contentBytes = new Uint8Array(content); let contentBytes = new Uint8Array(content);
let response = await this.client.PUT( let response = await this.client.PUT(
@ -82,12 +96,12 @@ export class SyncServer {
{ {
params: { params: {
path: { path: {
vaultId: SyncServer.VAULT_ID, vault_id: SyncServer.VAULT_ID,
documentId, document_id: documentId,
}, },
header: { header: {
authorization: authorization:
"Bearer " + this.settings.getSettings().token, "Bearer " + this.database.getSettings().token,
}, },
}, },
body: { body: {
@ -100,6 +114,10 @@ export class SyncServer {
} }
); );
if (!response.data) {
throw new Error(`Failed to create document: ${response.error}`);
}
Logger.getInstance().info( Logger.getInstance().info(
"Updated document " + JSON.stringify(response.data) "Updated document " + JSON.stringify(response.data)
); );
@ -119,12 +137,12 @@ export class SyncServer {
{ {
params: { params: {
path: { path: {
vaultId: SyncServer.VAULT_ID, vault_id: SyncServer.VAULT_ID,
documentId, document_id: documentId,
}, },
header: { header: {
authorization: authorization:
"Bearer " + this.settings.getSettings().token, "Bearer " + this.database.getSettings().token,
}, },
}, },
body: { body: {
@ -133,6 +151,11 @@ export class SyncServer {
} }
); );
// Response will be empty if successful
// if (!response.data) {
// throw new Error(`Failed to delete document: ${response.error}`);
// }
Logger.getInstance().info( Logger.getInstance().info(
"Updated document " + JSON.stringify(response.data) "Updated document " + JSON.stringify(response.data)
); );
@ -144,23 +167,27 @@ export class SyncServer {
documentId, documentId,
}: { }: {
documentId: DocumentId; documentId: DocumentId;
}): Promise<components["schemas"]["DocumentVersion"] | undefined> { }): Promise<components["schemas"]["DocumentVersion"]> {
const response = await this.client.GET( const response = await this.client.GET(
"/vaults/{vault_id}/documents/{document_id}", "/vaults/{vault_id}/documents/{document_id}",
{ {
params: { params: {
path: { path: {
vaultId: SyncServer.VAULT_ID, vault_id: SyncServer.VAULT_ID,
documentId, document_id: documentId,
}, },
header: { header: {
authorization: authorization:
"Bearer " + this.settings.getSettings().token, "Bearer " + this.database.getSettings().token,
}, },
}, },
} }
); );
if (!response.data) {
throw new Error(`Failed to get document: ${response.error}`);
}
Logger.getInstance().info( Logger.getInstance().info(
"Get document " + JSON.stringify(response.data) "Get document " + JSON.stringify(response.data)
); );
@ -169,20 +196,24 @@ export class SyncServer {
} }
public async getAll(): Promise< public async getAll(): Promise<
components["schemas"]["DocumentVersionWithoutContent"][] | undefined components["schemas"]["DocumentVersionWithoutContent"][]
> { > {
const response = await this.client.GET("/vaults/{vault_id}/documents", { const response = await this.client.GET("/vaults/{vault_id}/documents", {
params: { params: {
path: { path: {
vaultId: SyncServer.VAULT_ID, vault_id: SyncServer.VAULT_ID,
}, },
header: { header: {
authorization: authorization:
"Bearer " + this.settings.getSettings().token, "Bearer " + this.database.getSettings().token,
}, },
}, },
}); });
if (!response.data) {
throw new Error(`Failed to get documents: ${response.error}`);
}
Logger.getInstance().info( Logger.getInstance().info(
"Get document " + JSON.stringify(response.data) "Get document " + JSON.stringify(response.data)
); );