UNPKG

@kinetixarts/server-craft-it

Version:

Craft IT - Model Context Protocol (MCP) compliant Server for AI-Powered Asset Generation using Gemini

124 lines 4.54 kB
import { mkdir, writeFile } from 'fs/promises'; import path from 'path'; import { v4 as uuidv4 } from 'uuid'; import dotenv from 'dotenv'; import os from 'os'; import fs from 'fs'; dotenv.config(); /** * Find the user's Desktop directory with fallbacks for different OS and locales * @returns Path to the user's Desktop directory */ export function findDesktopPath() { const homedir = os.homedir(); // Common names for Desktop folder across different languages and OS const desktopNames = [ 'Desktop', // English 'Bureau', // French 'Escritorio', // Spanish 'Schreibtisch', // German 'デスクトップ', // Japanese 'Desktop', // macOS default 'OneDrive/Desktop', // Windows with OneDrive 'Documents/Desktop', // Alternative layout ]; // First try standard Desktop path let desktopPath = path.join(homedir, 'Desktop'); // If standard path doesn't exist, try alternatives if (!fs.existsSync(desktopPath)) { for (const name of desktopNames) { const testPath = path.join(homedir, name); if (fs.existsSync(testPath)) { desktopPath = testPath; break; } } } return desktopPath; } // Get the Desktop path export const DESKTOP_PATH = findDesktopPath(); // Get the image storage path from environment variables or use desktop default export const DEFAULT_IMAGE_PATH = process.env.CRAFT_IT_IMAGE_PATH || path.join(DESKTOP_PATH, 'assets', 'images'); // In-memory asset store for generated images export const assetStore = {}; /** * Ensure the directory exists, create it if it doesn't * @param dirPath Directory path to check/create */ export async function ensureDirectoryExists(dirPath) { try { await mkdir(dirPath, { recursive: true }); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); throw new Error(`Failed to create directory ${dirPath}: ${errorMessage}`); } } /** * Get the extension from mime type * @param mimeType The mime type string */ function getExtensionFromMimeType(mimeType) { const mimeToExt = { 'image/png': 'png', 'image/jpeg': 'jpg', 'image/jpg': 'jpg', 'image/gif': 'gif', 'image/webp': 'webp', }; return mimeToExt[mimeType] || 'png'; } /** * Save base64 image data to a file * @param base64Data Base64 encoded image data * @param mimeType MIME type of the image * @param storagePath Directory path to save the file */ export async function saveImageToFile(base64Data, mimeType, storagePath = process.env.CRAFT_IT_IMAGE_PATH || DEFAULT_IMAGE_PATH) { try { // Ensure the directory exists await ensureDirectoryExists(storagePath); // Generate a unique filename with the correct extension const extension = getExtensionFromMimeType(mimeType); const filename = `${uuidv4()}.${extension}`; const filePath = path.join(storagePath, filename); // Convert base64 to buffer and write to file const buffer = Buffer.from(base64Data, 'base64'); await writeFile(filePath, buffer); return filePath; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); throw new Error(`Failed to save image to file: ${errorMessage}`); } } /** * Store an asset both in memory and on disk * @param id Unique identifier for the asset * @param asset Asset data (base64 and mimeType) * @param storagePath Optional storage path, falls back to environment or default */ export async function storeAsset(id, asset, storagePath) { try { // Use the provided storage path, environment variable, or default const finalStoragePath = storagePath || process.env.CRAFT_IT_IMAGE_PATH || DEFAULT_IMAGE_PATH; // Save to file const filePath = await saveImageToFile(asset.base64, asset.mimeType, finalStoragePath); // Store in memory with filePath const entry = { ...asset, filePath, }; assetStore[id] = entry; return entry; } catch (error) { // If file storage fails, still store in memory but log the error const errorMessage = error instanceof Error ? error.message : String(error); console.error(`Failed to store asset on disk: ${errorMessage}`); assetStore[id] = asset; return asset; } } //# sourceMappingURL=assetStore.js.map