Skip to content

Commit

Permalink
feat(frontend): 🎸add pinata SDK and file upload script
Browse files Browse the repository at this point in the history
  • Loading branch information
markogracin committed Dec 16, 2024
1 parent 7375759 commit 1560ef2
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 2 deletions.
7 changes: 6 additions & 1 deletion frontend/.env.example
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=

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"eslint-plugin-svelte": "^2.36.0",
"globals": "^15.0.0",
"hashconnect": "^3.0.13",
"pinata-web3": "^0.5.3",
"prettier": "^3.3.2",
"prettier-plugin-css-order": "^2.1.2",
"prettier-plugin-organize-imports": "^4.1.0",
Expand Down
1 change: 0 additions & 1 deletion frontend/src/lib/index.ts

This file was deleted.

8 changes: 8 additions & 0 deletions frontend/src/lib/pinata/pinata.server.ts
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,
})
49 changes: 49 additions & 0 deletions frontend/src/routes/api/pinata/upload/+server.ts
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 })
}
}
Loading

0 comments on commit 1560ef2

Please sign in to comment.