|
1 | 1 | import { Handler } from "@netlify/functions";
|
2 |
| -import fetch from "node-fetch"; |
3 |
| - |
4 |
| -const ESTUARI_API_KEY = process.env["ESTUARY_API_KEY"]; |
5 |
| -const ESTUARI_URL = process.env["ESTUARY_GATEWAY"]; |
6 |
| - |
7 |
| -export const handler: Handler = async (event, context) => { |
8 |
| - context.callbackWaitsForEmptyEventLoop = false; |
9 |
| - if (event.body) { |
10 |
| - const newHeaders = event.headers; |
11 |
| - delete newHeaders.host; |
12 |
| - const response = await fetch(ESTUARI_URL, { |
13 |
| - method: "POST", |
14 |
| - headers: { |
15 |
| - Authorization: `Bearer ${ESTUARI_API_KEY}`, |
16 |
| - ...newHeaders, |
17 |
| - }, |
18 |
| - body: Buffer.from(event.body, "base64"), |
19 |
| - }); |
20 |
| - |
21 |
| - const parsedResponse = await response.json(); |
| 2 | +import { File, FilebaseClient } from "@filebase/client"; |
| 3 | +import amqp, { Connection } from "amqplib"; |
| 4 | +import busboy from "busboy"; |
| 5 | + |
| 6 | +const { FILEBASE_TOKEN, RABBITMQ_URL, FILEBASE_API_WRAPPER } = process.env; |
| 7 | +const filebase = new FilebaseClient({ token: FILEBASE_TOKEN ?? "" }); |
| 8 | + |
| 9 | +type FormElement = |
| 10 | + | { isFile: true; filename: string; mimeType: string; content: Buffer } |
| 11 | + | { isFile: false; content: string }; |
| 12 | +type FormData = { [key: string]: FormElement }; |
| 13 | + |
| 14 | +const emitRabbitMQLog = async (cid: string, operation: string) => { |
| 15 | + let connection: Connection | undefined; |
| 16 | + try { |
| 17 | + connection = await amqp.connect(RABBITMQ_URL ?? ""); |
| 18 | + const channel = await connection.createChannel(); |
| 19 | + |
| 20 | + await channel.assertExchange("ipfs", "topic"); |
| 21 | + channel.publish("ipfs", operation, Buffer.from(cid)); |
| 22 | + |
| 23 | + //eslint-disable-next-line no-console |
| 24 | + console.log(`Sent IPFS CID '${cid}' to exchange 'ipfs'`); |
| 25 | + } catch (err) { |
| 26 | + console.warn(err); |
| 27 | + } finally { |
| 28 | + if (typeof connection !== "undefined") await connection.close(); |
| 29 | + } |
| 30 | +}; |
| 31 | + |
| 32 | +const parseMultipart = ({ headers, body, isBase64Encoded }) => |
| 33 | + new Promise<FormData>((resolve, reject) => { |
| 34 | + const fields: FormData = {}; |
| 35 | + |
| 36 | + const bb = busboy({ headers }); |
| 37 | + |
| 38 | + bb.on("file", (name, file, { filename, mimeType }) => |
| 39 | + file.on("data", (content) => { |
| 40 | + fields[name] = { isFile: true, filename, mimeType, content }; |
| 41 | + }) |
| 42 | + ) |
| 43 | + .on("field", (name, value) => { |
| 44 | + if (value) fields[name] = { isFile: false, content: value }; |
| 45 | + }) |
| 46 | + .on("close", () => resolve(fields)) |
| 47 | + .on("error", (err) => reject(err)); |
| 48 | + |
| 49 | + bb.write(body, isBase64Encoded ? "base64" : "binary"); |
| 50 | + bb.end(); |
| 51 | + }); |
| 52 | + |
| 53 | +const pinToFilebase = async (data: FormData, dapp: string, operation: string): Promise<Array<string>> => { |
| 54 | + const cids = new Array<string>(); |
| 55 | + for (const [_, dataElement] of Object.entries(data)) { |
| 56 | + if (dataElement.isFile) { |
| 57 | + const { filename, mimeType, content } = dataElement; |
| 58 | + const path = `${filename}`; |
| 59 | + const cid = await filebase.storeDirectory([new File([content], path, { type: mimeType })]); |
| 60 | + await emitRabbitMQLog(cid, operation); |
| 61 | + cids.push(cid); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return cids; |
| 66 | +}; |
| 67 | + |
| 68 | +export const handler: Handler = async (event) => { |
| 69 | + const { queryStringParameters } = event; |
| 70 | + |
| 71 | + if ( |
| 72 | + !queryStringParameters || |
| 73 | + !queryStringParameters.dapp || |
| 74 | + !queryStringParameters.key || |
| 75 | + !queryStringParameters.operation |
| 76 | + ) { |
| 77 | + return { |
| 78 | + statusCode: 400, |
| 79 | + body: JSON.stringify({ message: "Invalid query parameters" }), |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + const { dapp, key, operation } = queryStringParameters; |
| 84 | + |
| 85 | + if (key !== FILEBASE_API_WRAPPER) { |
| 86 | + return { |
| 87 | + statusCode: 403, |
| 88 | + body: JSON.stringify({ message: "Invalid API key" }), |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + try { |
| 93 | + const parsed = await parseMultipart(event); |
| 94 | + const cids = await pinToFilebase(parsed, dapp, operation); |
| 95 | + |
| 96 | + return { |
| 97 | + statusCode: 200, |
| 98 | + body: JSON.stringify({ |
| 99 | + message: "File has been stored successfully", |
| 100 | + cids, |
| 101 | + }), |
| 102 | + }; |
| 103 | + } catch (err: any) { |
22 | 104 | return {
|
23 |
| - statusCode: response.status, |
24 |
| - body: JSON.stringify(parsedResponse), |
| 105 | + statusCode: 500, |
| 106 | + body: JSON.stringify({ message: err.message }), |
25 | 107 | };
|
26 | 108 | }
|
27 |
| - return { |
28 |
| - statusCode: 500, |
29 |
| - body: JSON.stringify({ message: "Invalid body format" }), |
30 |
| - }; |
31 | 109 | };
|
0 commit comments