Created
November 22, 2023 23:25
-
-
Save Jerga99/c8165c10bba6cd6d220d6fe5753d4776 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { v4 as uuidv4 } from "uuid"; | |
import { NextApiRequest, NextApiResponse } from "next"; | |
import { Session } from "next-iron-session"; | |
import { addressCheckMiddleware, pinataApiKey, pinataSecretApiKey, withSession } from "./utils"; | |
import FormData from "form-data"; | |
import axios from "axios"; | |
export const config = { | |
api: { | |
bodyParser: { | |
sizeLimit: '4mb' // Set desired value here | |
} | |
} | |
} | |
type FileReq = { | |
bytes: Uint8Array; | |
contentType: string; | |
fileName: string; | |
} | |
export default withSession(async ( | |
req: NextApiRequest & {session: Session}, | |
res: NextApiResponse | |
) => { | |
if (req.method === "POST") { | |
const { | |
bytes, | |
fileName, | |
contentType | |
} = req.body as FileReq; | |
if (!bytes || !fileName || !contentType) { | |
return res.status(422).send({message: "Image data are missing"}); | |
} | |
await addressCheckMiddleware(req, res); | |
const buffer = Buffer.from(Object.values(bytes)); | |
const formData = new FormData(); | |
formData.append( | |
"file", | |
buffer, { | |
contentType, | |
filename: fileName + "-" + uuidv4() | |
} | |
); | |
const fileRes = await axios.post("https://api.pinata.cloud/pinning/pinFileToIPFS", formData, { | |
maxBodyLength: Infinity, | |
headers: { | |
"Content-Type": `multipart/form-data; boundary=${formData.getBoundary()}`, | |
pinata_api_key: pinataApiKey, | |
pinata_secret_api_key: pinataSecretApiKey | |
} | |
}); | |
console.log('SUCCES') | |
console.log(fileRes) | |
return res.status(200).send(fileRes.data); | |
} else { | |
return res.status(422).send({message: "Invalid endpoint"}); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment