diff --git a/src/server/routes/pengaduan_route.ts b/src/server/routes/pengaduan_route.ts index 314afb9..ef62c64 100644 --- a/src/server/routes/pengaduan_route.ts +++ b/src/server/routes/pengaduan_route.ts @@ -471,23 +471,23 @@ const PengaduanRoute = new Elysia({ } // Konversi file ke base64 - const buffer = await file.arrayBuffer(); - const base64String = Buffer.from(buffer).toString("base64"); + // const buffer = await file.arrayBuffer(); + // const base64String = Buffer.from(buffer).toString("base64"); // (Opsional) jika perlu dikirim ke Seafile sebagai base64 - const result = await uploadFileBase64(defaultConfigSF, { name: file.name, data: base64String }); + const result = await uploadFileBase64(defaultConfigSF, { name: 'contoh', data: file }); return { success: true, message: "Upload berhasil", - filename: file.name, - size: file.size, - base64Preview: base64String.slice(0, 100) + "...", // hanya preview + // filename: file.name, + // size: file.size, + // base64Preview: base64String.slice(0, 100) + "...", // hanya preview seafileResult: result }; }, { body: t.Object({ - file: t.File({ format: "binary" }) + file: t.String() }), detail: { summary: "Upload File (Base64)", diff --git a/upload_base64.sh b/upload_base64.sh index 19b94c1..fc15ec1 100644 --- a/upload_base64.sh +++ b/upload_base64.sh @@ -1,2 +1,5 @@ +IMAGE_BASE64=$(base64 image.png | tr -d '\n') + curl -X POST http://localhost:3000/api/pengaduan/upload-base64 \ - -F file=@package.json + -H "Content-Type: application/json" \ + -d "{\"file\": \"$IMAGE_BASE64\"}" diff --git a/x.ts b/x.ts index 787484d..eecb117 100644 --- a/x.ts +++ b/x.ts @@ -1,133 +1,38 @@ -/** - * src/utils/swagger-to-mcp.ts - * - * Auto-converter: Swagger (OpenAPI) → MCP manifest (real-time) - * - * - Fetch swagger JSON dynamically from process.env.BUN_PUBLIC_BASE_URL + "/docs/json" - * - Generate MCP manifest for AI discovery (/.well-known/mcp.json) - * - Can be used as Bun CLI or integrated in Elysia route - */ +import fs from "fs"; -import { writeFileSync } from "fs" +// 1️⃣ File yang mau diupload +const filePath = "image.png"; +const apiUrl = "http://localhost:3000/api/pengaduan/upload-base64"; -interface OpenAPI { - info: { title?: string; description?: string; version?: string } - paths: Record -} +// 2️⃣ Baca file dan ubah ke base64 +const fileBuffer = fs.readFileSync(filePath); +const base64Data = fileBuffer.toString("base64"); -interface McpManifest { - schema_version: string - name: string - description: string - version?: string - endpoints: Record - capabilities: Record - contact?: { email?: string } -} +// 3️⃣ Buat payload JSON +const payload = { + file: base64Data, +}; -/** - * Convert OpenAPI JSON to MCP manifest format - */ -async function convertOpenApiToMcp(baseUrl: string): Promise { - const res = await fetch(`${baseUrl}/docs/json`) - if (!res.ok) throw new Error(`Failed to fetch Swagger JSON from ${baseUrl}/docs/json`) +// 4️⃣ Kirim ke server pakai fetch +async function uploadBase64() { + try { + const res = await fetch(apiUrl, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(payload), + }); - const openapi: OpenAPI = await res.json() - - const manifest: McpManifest = { - schema_version: "1.0", - name: openapi.info?.title ?? "MCP Server", - description: openapi.info?.description ?? "Auto-generated MCP manifest from Swagger", - version: openapi.info?.version ?? "0.0.0", - endpoints: { - openapi: `${baseUrl}/docs/json`, - mcp: `${baseUrl}/.well-known/mcp.json` - }, - capabilities: {} + if (!res.ok) { + throw new Error(`Request failed: ${res.status} ${res.statusText}`); } - for (const [path, methods] of Object.entries(openapi.paths || {})) { - for (const [method, def] of Object.entries(methods)) { - const tags = def.tags || ["default"] - const tag = tags[0] - const operationId = def.operationId || `${method}_${path.replace(/[\/{}]/g, "_")}` - - manifest.capabilities[tag] ??= {} - - // Extract parameters and body schema - const params: Record = {} - const required: string[] = [] - - if (Array.isArray(def.parameters)) { - for (const p of def.parameters) { - const type = p.schema?.type || "string" - params[p.name] = type - if (p.required) required.push(p.name) - } - } - - const bodySchema = def.requestBody?.content?.["application/json"]?.schema - if (bodySchema?.properties) { - for (const [key, prop] of Object.entries(bodySchema.properties)) { - params[key] = prop.type || "string" - } - if (Array.isArray(bodySchema.required)) - required.push(...bodySchema.required) - } - - // Generate example cURL - const sampleCurl = [ - `curl -X ${method.toUpperCase()} ${baseUrl}${path}`, - Object.keys(params).length > 0 - ? ` -H 'Content-Type: application/json' -d '${JSON.stringify( - Object.fromEntries(Object.keys(params).map(k => [k, params[k] === "string" ? k : "value"])) - )}'` - : "" - ] - .filter(Boolean) - .join(" \\\n") - - manifest.capabilities[tag][operationId] = { - method: method.toUpperCase(), - path, - summary: def.summary || def.description || "", - parameters: Object.keys(params).length > 0 ? params : undefined, - required: required.length > 0 ? required : undefined, - command: sampleCurl - } - } - } - - return manifest + const result = await res.json(); + console.log("✅ Upload sukses:", result); + } catch (err) { + console.error("❌ Upload gagal:", err); + } } -/** - * CLI entry - * bun run src/utils/swagger-to-mcp.ts - */ -if (import.meta.main) { - const baseUrl = process.env.BUN_PUBLIC_BASE_URL - if (!baseUrl) { - console.error("❌ Missing BUN_PUBLIC_BASE_URL environment variable.") - process.exit(1) - } - - convertOpenApiToMcp(baseUrl) - .then(manifest => { - writeFileSync(".well-known/mcp.json", JSON.stringify(manifest, null, 2)) - console.log("✅ Generated .well-known/mcp.json") - }) - .catch(err => console.error("❌ Failed to convert Swagger → MCP:", err)) -} - -/** - * Optional: Elysia integration - * Automatically serve /.well-known/mcp.json - */ -// import Elysia from "elysia" -// new Elysia() -// .get("/.well-known/mcp.json", async () => { -// const baseUrl = process.env.BUN_PUBLIC_BASE_URL! -// return await convertOpenApiToMcp(baseUrl) -// }) -// .listen(3000) +uploadBase64();