forked from keep-starknet-strange/joyboy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
395 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export {useClaim} from './useClaim'; | ||
export {useEstimateClaim} from './useEstimateClaim'; | ||
export {useFileUpload} from './useFileUpload'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {ImagePickerAsset} from 'expo-image-picker'; | ||
import {Platform} from 'react-native'; | ||
|
||
import {ApiInstance} from '../../services/api'; | ||
import {dataURLToBlob} from '../../utils/helpers'; | ||
import {useApiMutation} from './useApiMutation'; | ||
|
||
export const useFileUpload = () => { | ||
return useApiMutation({ | ||
mutationKey: ['fileUpload'], | ||
mutationFn: (file: ImagePickerAsset) => { | ||
const formData = new FormData(); | ||
|
||
formData.append( | ||
'file', | ||
Platform.OS === 'web' | ||
? dataURLToBlob(file.uri) | ||
: ({ | ||
uri: file.uri, | ||
name: 'file', | ||
type: file.mimeType, | ||
} as any), | ||
); | ||
|
||
return ApiInstance.post('/file', formData, { | ||
headers: { | ||
'Content-Type': 'multipart/form-data', | ||
}, | ||
}); | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {Readable} from 'node:stream'; | ||
|
||
import {NextRequest, NextResponse} from 'next/server'; | ||
|
||
import {pinata} from '@/services/pinata'; | ||
import {ErrorCode} from '@/utils/errors'; | ||
import {HTTPStatus} from '@/utils/http'; | ||
|
||
export async function POST(request: NextRequest) { | ||
const data = await request.formData(); | ||
const file: File | null = data.get('file') as unknown as File; | ||
|
||
if (!file) { | ||
return NextResponse.json({code: ErrorCode.BAD_REQUEST}, {status: HTTPStatus.BadRequest}); | ||
} | ||
|
||
try { | ||
const stream = Readable.fromWeb(file.stream() as any); | ||
|
||
const {IpfsHash} = await pinata.pinFileToIPFS(stream, { | ||
pinataMetadata: { | ||
name: file.name, | ||
}, | ||
}); | ||
|
||
return NextResponse.json( | ||
{hash: IpfsHash, url: `${process.env.IPFS_GATEWAY}/${IpfsHash}`}, | ||
{status: HTTPStatus.OK}, | ||
); | ||
} catch (error) { | ||
console.log(error); | ||
return NextResponse.json( | ||
{code: ErrorCode.TRANSACTION_ERROR, error}, | ||
{status: HTTPStatus.InternalServerError}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import PinataSDK from '@pinata/sdk'; | ||
|
||
if (!process.env.PINATA_API_KEY) throw new Error('PINATA_API_KEY is not set'); | ||
if (!process.env.PINATA_SECRET_API_KEY) throw new Error('PINATA_SECRET_API_KEY is not set'); | ||
if (!process.env.IPFS_GATEWAY) throw new Error('IPFS_GATEWAY is not set'); | ||
|
||
export const pinata = new PinataSDK({ | ||
pinataApiKey: process.env.PINATA_API_KEY, | ||
pinataSecretApiKey: process.env.PINATA_SECRET_API_KEY, | ||
}); | ||
|
||
pinata.testAuthentication(); |
Oops, something went wrong.