-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from mediolano-app/ipfs
Ipfs
- Loading branch information
Showing
11 changed files
with
796 additions
and
85 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { IP } from '~~/app/registerIP/page'; | ||
import { pinataClient } from '~~/utils/simpleNFT/pinataClient'; | ||
|
||
// export const config = { | ||
// api: { | ||
// bodyParser: false, | ||
// }, | ||
// }; | ||
|
||
export async function POST(request: NextRequest){ | ||
try{ | ||
// const mockedData = { | ||
// title: 'mocktitle', | ||
// description: 'mockdescription', | ||
// authors: 'mockauthors', | ||
// ipType: 'mockiptype', | ||
// uploadFile:'' | ||
// }; | ||
|
||
// const data4 = JSON.parse(await request.json()); | ||
// console.log(data4); | ||
|
||
const data = await request.formData(); | ||
|
||
const title = data.get('title') as unknown as string; | ||
const description = data.get('description') as unknown as string; | ||
const authors = data.getAll('authors'); | ||
const ipType = data.get('ipType') as string; | ||
const uploadFile = data.get('uploadFile') as File | null; | ||
|
||
// const data2 = request.body; | ||
// console.log(data2); | ||
|
||
// console.log("--------------"); | ||
// const data3 = request.text(); | ||
// console.log(data3); | ||
|
||
//const file: File | null = data.get("file") as unknown as File; | ||
|
||
// const uploadMockedData = await pinataClient.upload.json(mockedData); | ||
// const mockedUrl = await pinataClient.gateways.convert(uploadMockedData.IpfsHash); | ||
// console.log(mockedUrl); | ||
|
||
// console.log('Received data:', { title, description, authors, ipType, uploadFile: uploadFile?.name }); | ||
|
||
const userObject = { | ||
title, | ||
description, | ||
authors, | ||
ipType, | ||
uploadFile: uploadFile ? uploadFile.name : null, | ||
}; | ||
|
||
// console.log('Attempting to upload to Pinata:', userObject); | ||
|
||
const uploadData = await pinataClient.upload.json(userObject); | ||
// console.log('Pinata upload successful:', uploadData); | ||
const url = await pinataClient.gateways.convert(uploadData.IpfsHash); | ||
// console.log('Converted IPFS URL:', url); | ||
|
||
//aqui eu chamo a função de MINTAR | ||
return NextResponse.json({ ipfsHash: uploadData.IpfsHash, url }, { status: 200 }); | ||
// const data = await request.formData(); | ||
} catch (e) { | ||
console.log(e); | ||
return NextResponse.json( | ||
{ error: "Internal Server Error" }, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,70 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { ipfsClient } from '~~/utils/simpleNFT/ipfs'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
// import { ipfsClient } from '~~/utils/simpleNFT/ipfs'; | ||
import { IP } from '~~/app/registerIP/page'; | ||
import { pinataClient } from '~~/utils/simpleNFT/pinataClient'; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (req.method === 'POST') { | ||
const { title, description, authors, ipType }: IP = req.body; | ||
export const config = { | ||
api: { | ||
bodyParser: false, | ||
}, | ||
}; | ||
|
||
export async function POST(request: NextRequest){ | ||
try{ | ||
|
||
const data = await request.formData(); | ||
const file: File | null = data.get("file") as unknown as File; | ||
const uploadData = await pinataClient.upload.file(file); | ||
const url = await pinataClient.gateways.convert(uploadData.IpfsHash); | ||
|
||
console.log(url); | ||
return NextResponse.json(url, { status: 200 }); | ||
} catch (e) { | ||
console.log(e); | ||
return NextResponse.json( | ||
{ error: "Internal Server Error" }, | ||
{ status: 500 } | ||
); | ||
} | ||
} | ||
|
||
// export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
// if (req.method === 'POST') { | ||
// const { title, description, authors, ipType }: IP = req.body; | ||
|
||
try { | ||
// Upload intellectual property metadata to IPFS | ||
const ipMetadata = { | ||
title, | ||
description, | ||
authors, | ||
ipType, | ||
}; | ||
// try { | ||
// // Upload intellectual property metadata to IPFS | ||
// const ipMetadata = { | ||
// title, | ||
// description, | ||
// authors, | ||
// ipType, | ||
// }; | ||
|
||
const added = await ipfsClient.add(JSON.stringify(ipMetadata)); | ||
const ipfsHash = added.path; | ||
// const added = await ipfsClient.add(JSON.stringify(ipMetadata)); | ||
// const ipfsHash = added.path; | ||
|
||
// Generate NFT metadata | ||
const nftMetadata = { | ||
name: title, | ||
description: description, | ||
authors: authors, | ||
ipfsHash, | ||
image: `https://ipfs.infura.io/ipfs/${ipfsHash}`, // Optional: if file is an image | ||
}; | ||
// // Generate NFT metadata | ||
// const nftMetadata = { | ||
// name: title, | ||
// description: description, | ||
// authors: authors, | ||
// ipfsHash, | ||
// image: `https://ipfs.infura.io/ipfs/${ipfsHash}`, // Optional: if file is an image | ||
// }; | ||
|
||
// Optionally: Upload NFT metadata to IPFS | ||
const metadataAdded = await ipfsClient.add(JSON.stringify(nftMetadata)); | ||
const metadataHash = metadataAdded.path; | ||
// // Optionally: Upload NFT metadata to IPFS | ||
// const metadataAdded = await ipfsClient.add(JSON.stringify(nftMetadata)); | ||
// const metadataHash = metadataAdded.path; | ||
|
||
res.status(200).json({ metadataHash, nftMetadata }); | ||
} catch (error) { | ||
console.error('Error uploading to IPFS:', error); | ||
res.status(500).json({ error: 'Failed to upload to IPFS' }); | ||
} | ||
} else { | ||
res.setHeader('Allow', ['POST']); | ||
res.status(405).end(`Method ${req.method} Not Allowed`); | ||
} | ||
} | ||
// res.status(200).json({ metadataHash, nftMetadata }); | ||
// } catch (error) { | ||
// console.error('Error uploading to IPFS:', error); | ||
// res.status(500).json({ error: 'Failed to upload to IPFS' }); | ||
// } | ||
// } else { | ||
// res.setHeader('Allow', ['POST']); | ||
// res.status(405).end(`Method ${req.method} Not Allowed`); | ||
// } | ||
// } |
Empty file.
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
Oops, something went wrong.