forked from mito-systems/sol-mem-gen
Part of https://www.notion.so/Upload-generated-image-to-IPFS-190a6b22d47280f1ba81e860b7ca4ae1 Co-authored-by: Shreerang Kale <shreerangkale@gmail.com> Reviewed-on: #17
29 lines
907 B
TypeScript
29 lines
907 B
TypeScript
import { PinataSDK } from 'pinata-web3';
|
|
import 'dotenv/config';
|
|
import assert from 'assert';
|
|
|
|
import { FluxGenerationResult } from '../services/fluxService';
|
|
|
|
assert(process.env.PINATA_JWT, "PINATA_JWT is required");
|
|
assert(process.env.PINATA_GATEWAY, "PINATA_GATEWAY is required");
|
|
|
|
const pinata = new PinataSDK({
|
|
pinataJwt: process.env.PINATA_JWT,
|
|
pinataGateway: process.env.PINATA_GATEWAY,
|
|
});
|
|
|
|
export async function uploadToPinata(imageUrl: string, transactionSignature: string): Promise<FluxGenerationResult> {
|
|
try {
|
|
const upload = await pinata.upload.url(imageUrl, { metadata: { name: transactionSignature }});
|
|
|
|
const publicURL = await pinata.gateways.convert(upload.IpfsHash);
|
|
|
|
return { imageUrl: publicURL };
|
|
} catch (error) {
|
|
console.error('Error uploading to Pinata:', error)
|
|
return {
|
|
error: error instanceof Error ? error.message : 'Upload failed'
|
|
};
|
|
}
|
|
}
|