@mixio-pro/kalaasetu-mcp
Version:
A powerful Model Context Protocol server providing AI tools for content generation and analysis
59 lines (50 loc) • 1.52 kB
text/typescript
import * as fs from "fs";
import * as path from "path";
import type { StorageProvider } from "./interface";
export class LocalStorageProvider implements StorageProvider {
private basePath: string;
constructor(basePath: string = process.cwd()) {
this.basePath = basePath;
}
async init(): Promise<void> {
// No-op for local
}
async readFile(filePath: string): Promise<Buffer> {
let fullPath = filePath;
if (!path.isAbsolute(filePath)) {
fullPath = path.resolve(this.basePath, filePath);
}
return fs.promises.readFile(fullPath);
}
async writeFile(filePath: string, data: Buffer | string): Promise<string> {
let fullPath = filePath;
if (!path.isAbsolute(filePath)) {
fullPath = path.resolve(this.basePath, filePath);
}
const dir = path.dirname(fullPath);
if (!fs.existsSync(dir)) {
await fs.promises.mkdir(dir, { recursive: true });
}
await fs.promises.writeFile(fullPath, data);
return fullPath;
}
async exists(filePath: string): Promise<boolean> {
let fullPath = filePath;
if (!path.isAbsolute(filePath)) {
fullPath = path.resolve(this.basePath, filePath);
}
try {
await fs.promises.access(fullPath, fs.constants.F_OK);
return true;
} catch {
return false;
}
}
async getPublicUrl(filePath: string): Promise<string> {
let fullPath = filePath;
if (!path.isAbsolute(filePath)) {
fullPath = path.resolve(this.basePath, filePath);
}
return fullPath;
}
}