2025-10-02 10:10:57 +00:00
|
|
|
import type AIAgentPlugin from "main";
|
2025-10-05 12:06:05 +00:00
|
|
|
import { TAbstractFile, TFile, TFolder, type Vault } from "obsidian";
|
2025-09-30 20:20:24 +00:00
|
|
|
import { Resolve } from "./DependencyService";
|
|
|
|
|
import { Services } from "./Services";
|
|
|
|
|
import { isValidJson } from "Helpers/Helpers";
|
2025-10-12 20:26:01 +00:00
|
|
|
import { Path } from "Enums/Path";
|
|
|
|
|
import type { VaultService } from "./VaultService";
|
2025-10-14 18:50:45 +00:00
|
|
|
import type { SearchMatch } from "../Helpers/SearchTypes";
|
2025-09-30 20:20:24 +00:00
|
|
|
|
|
|
|
|
export class FileSystemService {
|
2025-10-12 20:26:01 +00:00
|
|
|
|
|
|
|
|
private readonly vaultService: VaultService;
|
2025-09-30 20:20:24 +00:00
|
|
|
|
|
|
|
|
public constructor() {
|
2025-10-12 20:26:01 +00:00
|
|
|
this.vaultService = Resolve<VaultService>(Services.VaultService);
|
2025-09-30 20:20:24 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-08 21:06:25 +00:00
|
|
|
public getVaultFileListForMarkDown() {
|
2025-10-12 20:26:01 +00:00
|
|
|
const files: TFile[] = this.vaultService.getMarkdownFiles();
|
2025-10-08 21:06:25 +00:00
|
|
|
return files.map(file => {
|
|
|
|
|
return file.path.replace(/\.md$/, "");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-12 22:11:36 +00:00
|
|
|
public async readFile(filePath: string, allowAccessToPluginRoot: boolean = false): Promise<string | null> {
|
|
|
|
|
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
|
2025-09-30 20:20:24 +00:00
|
|
|
if (file && file instanceof TFile) {
|
2025-10-12 22:11:36 +00:00
|
|
|
return await this.vaultService.read(file, allowAccessToPluginRoot);
|
2025-09-30 20:20:24 +00:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-14 21:42:52 +00:00
|
|
|
public async writeFile(filePath: string, content: string, allowAccessToPluginRoot: boolean = false): Promise<boolean | any> {
|
2025-09-30 20:20:24 +00:00
|
|
|
try {
|
2025-10-12 22:11:36 +00:00
|
|
|
let file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
|
2025-10-05 12:06:05 +00:00
|
|
|
if (file == null || !(file instanceof TFile)) {
|
2025-10-12 22:11:36 +00:00
|
|
|
await this.createDirectories(this.vaultService, filePath, allowAccessToPluginRoot);
|
|
|
|
|
await this.vaultService.create(filePath, content, allowAccessToPluginRoot);
|
2025-10-05 12:06:05 +00:00
|
|
|
return true;
|
2025-09-30 20:20:24 +00:00
|
|
|
}
|
2025-10-12 22:11:36 +00:00
|
|
|
await this.vaultService.modify(file as TFile, content, allowAccessToPluginRoot);
|
2025-09-30 20:20:24 +00:00
|
|
|
return true;
|
2025-10-05 12:06:05 +00:00
|
|
|
}
|
|
|
|
|
catch (error) {
|
|
|
|
|
console.error("Error writing file:", error);
|
2025-10-14 21:42:52 +00:00
|
|
|
return error;
|
2025-09-30 20:20:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-15 20:06:23 +00:00
|
|
|
public async deleteFile(filePath: string, allowAccessToPluginRoot: boolean = false): Promise<boolean | any> {
|
2025-10-03 21:14:30 +00:00
|
|
|
try {
|
2025-10-12 22:11:36 +00:00
|
|
|
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
|
2025-10-03 21:14:30 +00:00
|
|
|
|
|
|
|
|
if (file && file instanceof TFile) {
|
2025-10-12 22:11:36 +00:00
|
|
|
await this.vaultService.delete(file, undefined, allowAccessToPluginRoot);
|
2025-10-03 21:14:30 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error deleting file:", error);
|
2025-10-15 20:06:23 +00:00
|
|
|
return error;
|
2025-10-03 21:14:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-12 22:11:36 +00:00
|
|
|
public async listFilesInDirectory(dirPath: string, recursive: boolean = true, allowAccessToPluginRoot: boolean = false): Promise<TFile[]> {
|
|
|
|
|
return await this.vaultService.listFilesInDirectory(dirPath, recursive, allowAccessToPluginRoot);
|
2025-10-05 12:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-12 22:11:36 +00:00
|
|
|
public async readObjectFromFile(filePath: string, allowAccessToPluginRoot: boolean = false): Promise<object | null> {
|
|
|
|
|
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
|
2025-10-05 12:06:05 +00:00
|
|
|
if (file && file instanceof TFile) {
|
2025-10-12 22:11:36 +00:00
|
|
|
const content = await this.vaultService.read(file, allowAccessToPluginRoot);
|
2025-10-05 12:06:05 +00:00
|
|
|
if (isValidJson(content) === true) {
|
|
|
|
|
return JSON.parse(content);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-12 22:11:36 +00:00
|
|
|
public async writeObjectToFile(filePath: string, data: object, allowAccessToPluginRoot: boolean = false): Promise<boolean> {
|
2025-10-05 12:06:05 +00:00
|
|
|
try {
|
2025-10-12 22:11:36 +00:00
|
|
|
let file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
|
2025-10-05 12:06:05 +00:00
|
|
|
|
|
|
|
|
if (file && file instanceof TFile) {
|
2025-10-12 22:11:36 +00:00
|
|
|
await this.vaultService.modify(file, JSON.stringify(data, null, 4), allowAccessToPluginRoot);
|
2025-10-05 12:06:05 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2025-10-12 22:11:36 +00:00
|
|
|
await this.createDirectories(this.vaultService, filePath, allowAccessToPluginRoot);
|
|
|
|
|
await this.vaultService.create(filePath, JSON.stringify(data, null, 4), allowAccessToPluginRoot);
|
2025-10-05 12:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error writing JSON file:", error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-14 18:50:45 +00:00
|
|
|
public async searchVaultFiles(searchTerm: string): Promise<SearchMatch[]> {
|
2025-10-14 17:54:16 +00:00
|
|
|
return await this.vaultService.searchVaultFiles(searchTerm);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-12 22:11:36 +00:00
|
|
|
private async createDirectories(vaultService: VaultService, filePath: string, allowAccessToPluginRoot: boolean = false) {
|
2025-09-30 20:20:24 +00:00
|
|
|
const dirPath: string = filePath.substring(0, filePath.lastIndexOf('/'));
|
|
|
|
|
|
|
|
|
|
const dirs: string[] = dirPath.split('/');
|
|
|
|
|
|
|
|
|
|
let currentPath = "";
|
|
|
|
|
for (const dir of dirs) {
|
|
|
|
|
if (dir) {
|
|
|
|
|
currentPath = currentPath ? `${currentPath}/${dir}` : dir;
|
2025-10-12 22:11:36 +00:00
|
|
|
if (vaultService.getAbstractFileByPath(currentPath, allowAccessToPluginRoot) == null) {
|
|
|
|
|
await vaultService.createFolder(currentPath, allowAccessToPluginRoot);
|
2025-09-30 20:20:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|