-
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.
feat(frontend): 🎸add pinata SDK and file upload script
- Loading branch information
1 parent
7375759
commit 1560ef2
Showing
6 changed files
with
263 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
HEDERA_ACCOUNT_ID= | ||
HEDERA_PRIVATE_KEY= | ||
|
||
# testnet / mainnet | ||
HEDERA_ENVIRONMENT= | ||
# the reown (formerly walletconnect) project id used for hashpack | ||
PUBLIC_HASHCONNECT_PROJECT_ID=3918c43fa2467261721ed00df472e8be | ||
|
||
# pinata | ||
PINATA_JWT= | ||
PINATA_GROUP_ID= | ||
PUBLIC_GATEWAY_URL= | ||
|
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 was deleted.
Oops, something went wrong.
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,8 @@ | ||
import { PINATA_JWT } from '$env/static/private' | ||
import { PUBLIC_GATEWAY_URL } from '$env/static/public' | ||
import { PinataSDK } from 'pinata-web3' | ||
|
||
export const pinata = new PinataSDK({ | ||
pinataJwt: PINATA_JWT, | ||
pinataGateway: PUBLIC_GATEWAY_URL, | ||
}) |
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,49 @@ | ||
// https://docs.pinata.cloud/web3/sdk/getting-started | ||
|
||
import { pinata } from '$lib/pinata/pinata.server' | ||
import { json } from '@sveltejs/kit' | ||
import type { RequestHandler } from './$types' | ||
|
||
import { PINATA_GROUP_ID } from '$env/static/private' | ||
|
||
export const POST: RequestHandler = async ({ request }) => { | ||
try { | ||
const formData = await request.formData() | ||
|
||
// todo we wont need file uplaod in the future, as the image will be generated randomly! | ||
const uploadedFile = formData.get('file') as File | ||
const metadata = JSON.parse(formData.get('metadata') as string) | ||
|
||
if (!uploadedFile?.name || uploadedFile.size === 0) { | ||
return json({ error: 'You must provide a file to upload' }, { status: 400 }) | ||
} | ||
|
||
// Upload image todo for some reason the group id gets ignored? | ||
const imageUpload = await pinata.upload.file(uploadedFile).group(PINATA_GROUP_ID) | ||
|
||
const imageIpfsUrl = `ipfs://${imageUpload.IpfsHash}` | ||
|
||
// Create and upload metadata (containing IPFS image url) | ||
const fullMetadata = { | ||
...metadata, | ||
type: 'image/jpg', | ||
format: 'none', | ||
image: imageIpfsUrl, | ||
} | ||
|
||
// Upload metadata to IPFS | ||
const metadataUpload = await pinata.upload.json(fullMetadata) | ||
const metadataUrl = `ipfs://${metadataUpload.IpfsHash}` | ||
|
||
return json({ | ||
imageUrl: await pinata.gateways.convert(imageUpload.IpfsHash), | ||
metadataUrl, // this is the only thing that matters id say, and we will pass this as metadata on mint process | ||
imageIpfsHash: imageUpload.IpfsHash, | ||
metadataIpfsHash: metadataUpload.IpfsHash, | ||
}) | ||
} catch (throwable) { | ||
console.error('Upload failed:', throwable) | ||
const error = throwable instanceof Error ? throwable : new Error(String(throwable)) | ||
return json({ error: error.message }, { status: 500 }) | ||
} | ||
} |
Oops, something went wrong.