Skip to content

Commit

Permalink
Merge pull request #129 from raid-guild/add-pinata-pinning
Browse files Browse the repository at this point in the history
Switch to Pinata IPFS pinning
  • Loading branch information
ECWireless authored Jan 8, 2024
2 parents 8a9c84a + 181534e commit e83bfb6
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 26 deletions.
62 changes: 54 additions & 8 deletions lib/fileStorage.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,64 @@
import pinataSDK, { PinataPinOptions } from '@pinata/sdk';
import { Readable } from 'stream';
import { Web3Storage } from 'web3.storage';

const WEB3STORAGE_TOKEN = process.env.WEB3STORAGE_TOKEN;
const PINATA_JWT = process.env.PINATA_JWT;

if (!WEB3STORAGE_TOKEN) {
throw new Error(`Invalid/Missing environment variable: "WEB3STORAGE_TOKEN"`);
}

if (!PINATA_JWT) {
throw new Error(`Invalid/Missing environment variable: "PINATA_JWT"`);
}

export const uploadToWeb3Storage = async (file: File): Promise<string> => {
const client = new Web3Storage({
token: WEB3STORAGE_TOKEN,
});

return await client.put([file], {
maxRetries: 3,
wrapWithDirectory: false,
});
try {
const client = new Web3Storage({
token: WEB3STORAGE_TOKEN,
});

return await client.put([file], {
maxRetries: 3,
wrapWithDirectory: false,
});
} catch (error) {
console.error(error);
return '';
}
};

const bufferToStream = (buffer: Buffer) => {
const readable = new Readable();
readable._read = () => {};
readable.push(buffer);
readable.push(null);
return readable;
};

export const uploadToPinata = async (
file: Buffer,
fileName: string,
): Promise<string> => {
try {
const pinata = new pinataSDK({ pinataJWTKey: PINATA_JWT });
const readableStreamForFile = bufferToStream(file);
const options: PinataPinOptions = {
pinataMetadata: {
name: fileName,
},
};

const response = await pinata.pinFileToIPFS(readableStreamForFile, options);
const { IpfsHash } = response;
if (!IpfsHash) {
throw new Error('Error pinning file to IPFS');
}

return IpfsHash;
} catch (error) {
console.error(error);
return '';
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@fontsource/tektur": "^5.0.3",
"@fontsource/unbounded": "^5.0.15",
"@openzeppelin/merkle-tree": "^1.0.5",
"@pinata/sdk": "^2.1.0",
"@rainbow-me/rainbowkit": "^1.1.1",
"@types/node": "20.8.4",
"@types/react": "18.2.28",
Expand Down
10 changes: 6 additions & 4 deletions pages/api/uploadFile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import formidable from 'formidable';
import Jimp from 'jimp';
import type { NextApiRequest, NextApiResponse, PageConfig } from 'next';
import { File } from 'web3.storage';

import { uploadToWeb3Storage } from '@/lib/fileStorage';
import { uploadToPinata } from '@/lib/fileStorage';

export const config: PageConfig = {
api: {
Expand Down Expand Up @@ -41,8 +40,11 @@ export default async function uploadFile(
const fileContents = await image
.resize(700, Jimp.AUTO)
.getBufferAsync(Jimp.MIME_PNG);
const file = new File([fileContents], `${fileName}.png`);
const cid = await uploadToWeb3Storage(file);

const cid = await uploadToPinata(fileContents, `${fileName}.png`);
if (!cid) {
return res.status(500).json({ error: 'Error uploading file' });
}

return res.status(200).json({ cid });
} catch (error) {
Expand Down
13 changes: 7 additions & 6 deletions pages/api/uploadMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { File } from 'web3.storage';

import { uploadToWeb3Storage } from '@/lib/fileStorage';
import { uploadToPinata } from '@/lib/fileStorage';

type ResponseData = {
cid?: string;
Expand All @@ -17,13 +16,15 @@ export default async function uploadMetadata(
const metadata = req.body;

const fileContents = Buffer.from(metadata);
const file = new File([fileContents], fileName);

const cid = await uploadToWeb3Storage(file);
const cid = await uploadToPinata(fileContents, fileName);
if (!cid) {
return res.status(500).json({ error: 'Error uploading file' });
}

res.status(200).json({ cid });
return res.status(200).json({ cid });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Something went wrong' });
return res.status(500).json({ error: 'Something went wrong' });
}
}
10 changes: 5 additions & 5 deletions pages/api/uploadTraits.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import Jimp from 'jimp';
import type { NextApiRequest, NextApiResponse } from 'next';
import { File } from 'web3.storage';

import {
CharacterInfoFragment,
GetCharacterInfoByIdDocument,
} from '@/graphql/autogen/types';
import { getGraphClient } from '@/graphql/client';
import { uploadToWeb3Storage } from '@/lib/fileStorage';
import { uploadToPinata } from '@/lib/fileStorage';
import {
BaseTraitType,
CharacterTraits,
Expand Down Expand Up @@ -162,10 +161,11 @@ export default async function uploadTraits(
.resize(700, Jimp.AUTO)
.getBufferAsync(Jimp.MIME_JPEG);

const file = new File([fileContents], 'characterAvater.jpg');

const attributes = getAttributesFromTraitsObject(traits);
const cid = await uploadToWeb3Storage(file);
const cid = await uploadToPinata(fileContents, 'characterAvater.jpg');
if (!cid) {
return res.status(500).json({ error: 'Something went wrong' });
}

return res.status(200).json({ attributes, cid });
} catch (error) {
Expand Down
Loading

0 comments on commit e83bfb6

Please sign in to comment.