feat: crossbell renovation - editor page
This commit is contained in:
+14
-20
@@ -1,30 +1,24 @@
|
||||
import { useCallback } from "react"
|
||||
import { R2_URL } from "~/lib/env"
|
||||
import { trpc } from "~/lib/trpc"
|
||||
import { WEB3_STORAGE_API_TOKEN } from "~/lib/env"
|
||||
import { Web3Storage } from 'web3.storage'
|
||||
|
||||
export const useUploadFile = () => {
|
||||
const { fetchQuery } = trpc.useContext()
|
||||
|
||||
const uploadFile = useCallback<UploadFile>(
|
||||
async (blob, filename) => {
|
||||
const jwt = await fetchQuery(["user.getSignedJwt"], {})
|
||||
|
||||
const form = new FormData()
|
||||
form.append("file", blob, filename)
|
||||
const res = await fetch(R2_URL, {
|
||||
body: form,
|
||||
method: "post",
|
||||
headers: {
|
||||
authorization: `Bearer ${jwt}`,
|
||||
},
|
||||
})
|
||||
if (!res.ok) {
|
||||
throw new Error(await res.text())
|
||||
const file = new File([blob], filename);
|
||||
const web3Storage = new Web3Storage({
|
||||
token: WEB3_STORAGE_API_TOKEN,
|
||||
} as any)
|
||||
const cid = await web3Storage.put([file], {
|
||||
name: file.name,
|
||||
maxRetries: 3,
|
||||
wrapWithDirectory: false,
|
||||
});
|
||||
return {
|
||||
key: `https://gateway.ipfs.io/ipfs/${cid}`
|
||||
}
|
||||
const data = await res.json()
|
||||
return data
|
||||
},
|
||||
[fetchQuery]
|
||||
[]
|
||||
)
|
||||
|
||||
return uploadFile
|
||||
|
||||
Reference in New Issue
Block a user