@mixio-pro/kalaasetu-mcp
Version:
A powerful Model Context Protocol server providing AI tools for content generation and analysis
117 lines (99 loc) • 3.32 kB
text/typescript
import { GoogleAuth } from "google-auth-library";
import type { StorageProvider } from "./interface";
import * as path from "path";
export class GCSStorageProvider implements StorageProvider {
private bucket: string;
private auth: GoogleAuth;
constructor(bucket: string) {
this.bucket = bucket;
this.auth = new GoogleAuth({
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
});
}
async init(): Promise<void> {
console.log(
`Initializing GCS Storage Provider with bucket: ${this.bucket}`
);
// Verify we can get credentials
try {
await this.auth.getClient();
} catch (error) {
console.warn(`Warning: Could not initialize GCS client: ${error}`);
}
}
private async getAccessToken(): Promise<string> {
const client = await this.auth.getClient();
const token = await client.getAccessToken();
if (!token.token) {
throw new Error("Failed to get GCS access token");
}
return token.token;
}
async readFile(filePath: string): Promise<Buffer> {
const objectName = path.basename(filePath);
const url = `https://storage.googleapis.com/storage/v1/b/${
this.bucket
}/o/${encodeURIComponent(objectName)}?alt=media`;
const token = await this.getAccessToken();
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${token}`,
},
});
if (!response.ok) {
throw new Error(
`Failed to read file from GCS: ${response.status} ${response.statusText}`
);
}
const arrayBuffer = await response.arrayBuffer();
return Buffer.from(arrayBuffer);
}
async writeFile(filePath: string, data: Buffer | string): Promise<string> {
const objectName = path.basename(filePath);
const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
// Upload using JSON API
const url = `https://storage.googleapis.com/upload/storage/v1/b/${
this.bucket
}/o?uploadType=media&name=${encodeURIComponent(objectName)}`;
const token = await this.getAccessToken();
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/octet-stream",
"Content-Length": buffer.length.toString(),
},
body: buffer,
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(
`Failed to upload to GCS: ${response.status} ${errorText}`
);
}
// Return public URL
return `https://storage.googleapis.com/${this.bucket}/${objectName}`;
}
async exists(filePath: string): Promise<boolean> {
try {
const objectName = path.basename(filePath);
const url = `https://storage.googleapis.com/storage/v1/b/${
this.bucket
}/o/${encodeURIComponent(objectName)}`;
const token = await this.getAccessToken();
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
},
});
return response.ok;
} catch {
return false;
}
}
async getPublicUrl(filePath: string): Promise<string> {
const objectName = path.basename(filePath);
return `https://storage.googleapis.com/${this.bucket}/${objectName}`;
}
}