1- import { gql , type GraphQLClient } from "graphql-request" ;
21import { toast } from "react-toastify" ;
32
43import { OPTIONS } from "utils/wrapWithToast" ;
54
6- export async function uploadToIpfs ( client : GraphQLClient , file : File ) : Promise < string | null > {
7- const presignedUrl = await getPreSignedUrl ( client , file . name ) ;
5+ export enum Products {
6+ CourtV2 = "CourtV2" ,
7+ }
8+
9+ export enum Roles {
10+ Photo = "photo" ,
11+ Evidence = "evidence" ,
12+ Policy = "policy" ,
13+ Generic = "generic" ,
14+ }
15+
16+ export type IpfsUploadPayload = {
17+ file : File ;
18+ name : string ;
19+ product : Products ;
20+ role : Roles ;
21+ } ;
22+
23+ type Config = {
24+ baseUrl : string ;
25+ authToken : string ;
26+ } ;
27+
28+ export async function uploadToIpfs ( config : Config , payload : IpfsUploadPayload ) : Promise < string | null > {
29+ const formData = new FormData ( ) ;
30+ formData . append ( "file" , payload . file , payload . name ) ;
31+ formData . append ( "name" , payload . name ) ;
32+ formData . append ( "product" , payload . product ) ;
33+ formData . append ( "role" , payload . role ) ;
834
935 return toast . promise < string | null , Error > (
10- fetch ( presignedUrl , {
11- method : "PUT" ,
12- body : file ,
36+ fetch ( `${ config . baseUrl } /ipfs/file` , {
37+ method : "POST" ,
38+ headers : {
39+ authorization : `Bearer ${ config . authToken } ` ,
40+ } ,
41+ body : formData ,
1342 } ) . then ( async ( response ) => {
14- if ( response . status !== 200 ) {
43+ if ( ! response . ok ) {
1544 const error = await response . json ( ) . catch ( ( ) => ( { message : "Error uploading to IPFS" } ) ) ;
1645 throw new Error ( error . message ) ;
1746 }
18- return response . headers . get ( "x-amz-meta-cid" ) ;
47+
48+ return await response . text ( ) ;
1949 } ) ,
2050 {
2151 pending : `Uploading to IPFS...` ,
@@ -29,42 +59,3 @@ export async function uploadToIpfs(client: GraphQLClient, file: File): Promise<s
2959 OPTIONS
3060 ) ;
3161}
32-
33- const presignedUrlQuery = gql `
34- mutation GetPresignedUrl($filename: String!) {
35- getPresignedUrl(filename: $filename, appname: KlerosCourt)
36- }
37- ` ;
38-
39- type GetPresignedUrlResponse = {
40- getPresignedUrl : string ;
41- } ;
42-
43- const getPreSignedUrl = ( client : GraphQLClient , filename : string ) => {
44- const variables = {
45- filename,
46- } ;
47-
48- return toast . promise < string , Error > (
49- client
50- . request < GetPresignedUrlResponse > ( presignedUrlQuery , variables )
51- . then ( async ( response ) => response . getPresignedUrl )
52- . catch ( ( errors ) => {
53- // eslint-disable-next-line no-console
54- console . log ( "Get Presigned Url error:" , { errors } ) ;
55-
56- const errorMessage = Array . isArray ( errors ?. response ?. errors )
57- ? errors . response . errors [ 0 ] ?. message
58- : "Unknown error" ;
59- throw new Error ( errorMessage ) ;
60- } ) ,
61- {
62- error : {
63- render ( { data : error } ) {
64- return `Getting Presigned Url failed: ${ error ?. message } ` ;
65- } ,
66- } ,
67- } ,
68- OPTIONS
69- ) ;
70- } ;
0 commit comments