From bfc218a91b12798957fd31d9393128b396e1e90e Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 17 Dec 2024 11:17:51 -0500 Subject: [PATCH] chore: Refacted next app to use signed urls for uploads --- examples/next-starter/app/api/key/route.ts | 26 ---- examples/next-starter/app/api/url/route.ts | 19 +++ examples/next-starter/app/page.tsx | 138 ++++++++++----------- 3 files changed, 88 insertions(+), 95 deletions(-) delete mode 100644 examples/next-starter/app/api/key/route.ts create mode 100644 examples/next-starter/app/api/url/route.ts diff --git a/examples/next-starter/app/api/key/route.ts b/examples/next-starter/app/api/key/route.ts deleted file mode 100644 index 0da0bb1..0000000 --- a/examples/next-starter/app/api/key/route.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { type NextRequest, NextResponse } from "next/server"; -import { pinata } from "@/utils/config"; -import { KeyResponse } from "../../../../../dist"; - -export const dynamic = "force-dynamic"; - -export async function GET(req: NextRequest, res: NextResponse) { - try { - const uuid = crypto.randomUUID(); - const keyData: KeyResponse = await pinata.keys.create({ - keyName: uuid.toString(), - permissions: { - endpoints: { - pinning: { - pinFileToIPFS: true, - }, - }, - }, - maxUses: 1, - }); - return NextResponse.json(keyData, { status: 200 }); - } catch (error) { - console.log(error); - return NextResponse.json({ text: "Error creating API Key:" }, { status: 500 }); - } -} diff --git a/examples/next-starter/app/api/url/route.ts b/examples/next-starter/app/api/url/route.ts new file mode 100644 index 0000000..169acc7 --- /dev/null +++ b/examples/next-starter/app/api/url/route.ts @@ -0,0 +1,19 @@ +import { type NextRequest, NextResponse } from "next/server"; +import { pinata } from "@/utils/config"; + +export const dynamic = "force-dynamic"; + +export async function GET(req: NextRequest, res: NextResponse) { + try { + const url = await pinata.upload.createSignedURL({ + expires: 30, + }); + return NextResponse.json({ url: url }, { status: 200 }); + } catch (error) { + console.log(error); + return NextResponse.json( + { text: "Error creating API Key:" }, + { status: 500 }, + ); + } +} diff --git a/examples/next-starter/app/page.tsx b/examples/next-starter/app/page.tsx index caf3a92..93f72b6 100644 --- a/examples/next-starter/app/page.tsx +++ b/examples/next-starter/app/page.tsx @@ -4,78 +4,78 @@ import { useState } from "react"; import { pinata } from "@/utils/config"; export default function Home() { - const [file, setFile] = useState(); - const [url, setUrl] = useState(""); - const [uploading, setUploading] = useState(false); + const [file, setFile] = useState(); + const [url, setUrl] = useState(""); + const [uploading, setUploading] = useState(false); - // Client side upload - // - const uploadFile = async () => { - try { - if (!file) { - alert("No file selected"); - return; - } - setUploading(true); - const keyRequest = await fetch("/api/key", { - method: "GET", - }); - const keyData = await keyRequest.json(); - const upload = await pinata.upload.file(file).key(keyData.JWT); - const urlReuest = await fetch("/api/sign", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ cid: upload.cid }), - }); - const url = await urlReuest.json(); - setUrl(url); - setUploading(false); - } catch (e) { - console.log(e); - setUploading(false); - alert("Trouble uploading file"); - } - }; + // Client side upload + // + const uploadFile = async () => { + try { + if (!file) { + alert("No file selected"); + return; + } + setUploading(true); + const signedUrlReq = await fetch("/api/url", { + method: "GET", + }); + const signedUrlRes = await signedUrlReq.json(); + const upload = await pinata.upload.file(file).url(signedUrlRes.url); + const urlReuest = await fetch("/api/sign", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ cid: upload.cid }), + }); + const url = await urlReuest.json(); + setUrl(url); + setUploading(false); + } catch (e) { + console.log(e); + setUploading(false); + alert("Trouble uploading file"); + } + }; - // Server side Upload + // Server side Upload - // const uploadFile = async () => { - // try { - // if (!file) { - // alert("No file selected"); - // return; - // } - // setUploading(true); - // const data = new FormData(); - // data.set("file", file); - // const uploadRequest = await fetch("/api/files", { - // method: "POST", - // body: data, - // }); - // const signedUrl = await uploadRequest.json(); - // console.log(signedUrl); - // setUrl(signedUrl); - // setUploading(false); - // } catch (e) { - // console.log(e); - // setUploading(false); - // alert("Trouble uploading file"); - // } - // }; + // const uploadFile = async () => { + // try { + // if (!file) { + // alert("No file selected"); + // return; + // } + // setUploading(true); + // const data = new FormData(); + // data.set("file", file); + // const uploadRequest = await fetch("/api/files", { + // method: "POST", + // body: data, + // }); + // const signedUrl = await uploadRequest.json(); + // console.log(signedUrl); + // setUrl(signedUrl); + // setUploading(false); + // } catch (e) { + // console.log(e); + // setUploading(false); + // alert("Trouble uploading file"); + // } + // }; - const handleChange = (e: React.ChangeEvent) => { - setFile(e.target?.files?.[0]); - }; + const handleChange = (e: React.ChangeEvent) => { + setFile(e.target?.files?.[0]); + }; - return ( -
- - - {url && Image from Pinata} -
- ); + return ( +
+ + + {url && Image from Pinata} +
+ ); }