2025-11-10 13:44:16 +00:00
|
|
|
import { TAbstractFile, TFile, TFolder } from "obsidian";
|
2025-09-30 20:20:24 +00:00
|
|
|
import { Resolve } from "./DependencyService";
|
|
|
|
|
import { Services } from "./Services";
|
2025-10-12 20:26:01 +00:00
|
|
|
import type { VaultService } from "./VaultService";
|
2025-10-29 19:35:19 +00:00
|
|
|
import type { ISearchMatch } from "../Helpers/SearchTypes";
|
2025-11-17 19:02:15 +00:00
|
|
|
import { Exception } from "Helpers/Exception";
|
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-11-17 19:02:15 +00:00
|
|
|
public async readFile(filePath: string, allowAccessToPluginRoot: boolean = false): Promise<string | Error> {
|
2025-10-12 22:11:36 +00:00
|
|
|
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
|
|
|
}
|
2025-11-17 19:02:15 +00:00
|
|
|
return Exception.new(`Path is a folder, not a file: ${filePath}`);
|
2025-09-30 20:20:24 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
public async writeFile(filePath: string, content: string, allowAccessToPluginRoot: boolean = false): Promise<TFile | Error> {
|
2025-09-30 20:20:24 +00:00
|
|
|
try {
|
2025-10-17 23:28:23 +00:00
|
|
|
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
|
2025-10-05 12:06:05 +00:00
|
|
|
if (file == null || !(file instanceof TFile)) {
|
2025-11-17 19:02:15 +00:00
|
|
|
return await this.vaultService.create(filePath, content, allowAccessToPluginRoot);
|
2025-09-30 20:20:24 +00:00
|
|
|
}
|
2025-11-17 19:02:15 +00:00
|
|
|
return await this.vaultService.modify(file, content, allowAccessToPluginRoot);
|
2025-10-05 12:06:05 +00:00
|
|
|
}
|
|
|
|
|
catch (error) {
|
2025-11-17 19:02:15 +00:00
|
|
|
Exception.log(error);
|
|
|
|
|
return Exception.new(error);
|
2025-09-30 20:20:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
public async deleteFile(filePath: string, allowAccessToPluginRoot: boolean = false): Promise<Error | void> {
|
2025-10-16 21:58:30 +00:00
|
|
|
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
|
2025-10-03 21:14:30 +00:00
|
|
|
|
2025-11-03 21:37:47 +00:00
|
|
|
if (!file) {
|
2025-11-17 19:02:15 +00:00
|
|
|
return Exception.new(`File does not exist: ${filePath}`);
|
2025-10-03 21:14:30 +00:00
|
|
|
}
|
2025-10-16 21:58:30 +00:00
|
|
|
|
2025-11-03 21:37:47 +00:00
|
|
|
return await this.vaultService.delete(file, allowAccessToPluginRoot);
|
2025-10-16 21:58:30 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
public async moveFile(sourcePath: string, destinationPath: string, allowAccessToPluginRoot: boolean = false): Promise<void | Error> {
|
2025-10-17 15:18:23 +00:00
|
|
|
return await this.vaultService.move(sourcePath, destinationPath, allowAccessToPluginRoot);
|
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-11-01 11:43:32 +00:00
|
|
|
public async listFoldersInDirectory(dirPath: string, recursive: boolean = true, allowAccessToPluginRoot: boolean = false): Promise<TFolder[]> {
|
|
|
|
|
return await this.vaultService.listFoldersInDirectory(dirPath, recursive, allowAccessToPluginRoot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async listDirectoryContents(dirPath: string, recursive: boolean = true, allowAccessToPluginRoot: boolean = false): Promise<TAbstractFile[]> {
|
|
|
|
|
return await this.vaultService.listDirectoryContents(dirPath, recursive, allowAccessToPluginRoot);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
public async readObjectFromFile(filePath: string, allowAccessToPluginRoot: boolean = false): Promise<object | Error> {
|
2025-10-12 22:11:36 +00:00
|
|
|
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
|
2025-10-05 12:06:05 +00:00
|
|
|
if (file && file instanceof TFile) {
|
2025-11-17 19:02:15 +00:00
|
|
|
const result = await this.vaultService.read(file, allowAccessToPluginRoot);
|
|
|
|
|
return typeof result === "string" ? JSON.parse(result) as object : result;
|
2025-10-05 12:06:05 +00:00
|
|
|
}
|
2025-11-17 19:02:15 +00:00
|
|
|
return Exception.new(`File not found: ${filePath}`);
|
2025-10-05 12:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
public async writeObjectToFile(filePath: string, data: object, allowAccessToPluginRoot: boolean = false): Promise<TFile | Error> {
|
|
|
|
|
const file: TAbstractFile | null = this.vaultService.getAbstractFileByPath(filePath, allowAccessToPluginRoot);
|
2025-10-05 12:06:05 +00:00
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
let result: TFile | Error;
|
2025-10-05 12:06:05 +00:00
|
|
|
if (file && file instanceof TFile) {
|
2025-11-17 19:02:15 +00:00
|
|
|
result = await this.vaultService.modify(file, JSON.stringify(data, null, 4), allowAccessToPluginRoot);
|
2025-10-05 12:06:05 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2025-11-17 19:02:15 +00:00
|
|
|
result = await this.vaultService.create(filePath, JSON.stringify(data, null, 4), allowAccessToPluginRoot);
|
2025-10-05 12:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-17 19:02:15 +00:00
|
|
|
return result;
|
2025-10-05 12:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-01 11:43:32 +00:00
|
|
|
public async searchVaultFiles(searchTerm: string, allowAccessToPluginRoot: boolean = false): Promise<ISearchMatch[]> {
|
|
|
|
|
return await this.vaultService.searchVaultFiles(searchTerm, allowAccessToPluginRoot);
|
2025-10-14 17:54:16 +00:00
|
|
|
}
|
2025-09-30 20:20:24 +00:00
|
|
|
}
|