Handle file events
This commit is contained in:
parent
b57a0ac0d4
commit
6173320f81
2 changed files with 151 additions and 41 deletions
|
|
@ -1,27 +1,106 @@
|
|||
import { TAbstractFile, TFile } from "obsidian";
|
||||
import { FileEventHandler } from "./file-event-handler";
|
||||
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 {
|
||||
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) {
|
||||
Logger.getInstance().info(`File created: ${file}`);
|
||||
this.syncer.onCreate(file.path, await file.vault.read(file));
|
||||
Logger.getInstance().info(`File created: ${file.path}`);
|
||||
|
||||
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) {
|
||||
Logger.getInstance().info(`File deleted: ${file}`);
|
||||
async onDelete(file: TAbstractFile): Promise<void> {
|
||||
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}`;
|
||||
}
|
||||
|
||||
await this.syncServer.delete({
|
||||
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`);
|
||||
}
|
||||
}
|
||||
|
||||
onRename(file: TAbstractFile, oldPath: string) {
|
||||
Logger.getInstance().info(`File renamed: ${oldPath} -> ${file}`);
|
||||
async onRename(file: TAbstractFile, oldPath: string): Promise<void> {
|
||||
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`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
onModify(file: TAbstractFile) {
|
||||
Logger.getInstance().info(`File modified: ${file}`);
|
||||
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`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue