Skip to content

Commit

Permalink
Fix/fix ipfs uploading error (#39)
Browse files Browse the repository at this point in the history
* fix: fix ipfs image uploading error

* refactor: Refactor ipfs connector upload input structure
  • Loading branch information
1M4nt0 authored Nov 8, 2023
1 parent 79d295b commit e33327e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
20 changes: 10 additions & 10 deletions apps/web/pages/api/v0/course/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,21 @@ const handlePostRequest = async (

const credentialImageBuffer = fs.readFileSync(filepath)

const ipfsCredentialImageData = await IpfsConnector.upload(
credentialImageBuffer,
mimetype ?? '',
originalFilename ?? '',
)
const ipfsCredentialImageData = await IpfsConnector.upload({
fileContent: credentialImageBuffer,
fileName: originalFilename ?? '',
mimeType: mimetype ?? '',
})

const ipfsCredentialMetadata = await IpfsConnector.upload(
{
const ipfsCredentialMetadata = await IpfsConnector.upload({
fileContent: {
name: name,
description: description,
image: ipfsCredentialImageData.url,
},
'',
'data/json',
)
fileName: '',
mimeType: 'data/json',
})

const credential = await prisma.credential.create({
data: {
Expand Down
21 changes: 11 additions & 10 deletions apps/web/pages/api/v0/course/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,27 @@ const handlePostRequest = async (
}

const { mimetype, filepath, originalFilename } = imageFile
console.log(originalFilename)
const courseImageBuffer = fs.readFileSync(filepath)

const ipfsCourseImageData = await IpfsConnector.upload(
courseImageBuffer,
mimetype ?? '',
originalFilename ?? '',
)
const ipfsCourseImageData = await IpfsConnector.upload({
fileContent: courseImageBuffer,
fileName: originalFilename ?? '',
mimeType: mimetype ?? '',
})

const ipfsCourseMetadata = await IpfsConnector.upload(
{
const ipfsCourseMetadata = await IpfsConnector.upload({
fileContent: {
image: ipfsCourseImageData.url,
name: name,
description: description,
website: website,
'snapshot-ens': snapshotEns,
'media-channel': mediaChannel,
},
'',
'data/json',
)
fileName: '',
mimeType: 'data/json',
})

res.status(200).json({
status: ApiResponseStatus.success,
Expand Down
14 changes: 9 additions & 5 deletions packages/ipfs/src/connectors/infura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ export class InfuraIpfsConnector implements IpfsConnector {
this.ipfsGateway = ipfsGateway
}

public async upload(
fileContent: Buffer | Record<string, any>,
fileName: string,
mimeType: string,
): Promise<IpfsUploadResult> {
public async upload({
fileContent,
fileName,
mimeType,
}: {
fileContent: Buffer | Record<string, any>
fileName: string
mimeType: string
}): Promise<IpfsUploadResult> {
const formData = new FormData()

if (fileContent instanceof Buffer) {
Expand Down
14 changes: 9 additions & 5 deletions packages/ipfs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ export type IpfsUploadResult = {
}

export interface IpfsConnector {
upload(
fileContent: Buffer | Record<string, any>,
mimeType?: string,
fileName?: string,
): Promise<IpfsUploadResult>
upload({
fileContent,
mimeType,
fileName,
}: {
fileContent: Buffer | Record<string, any>
mimeType?: string
fileName?: string
}): Promise<IpfsUploadResult>
}

0 comments on commit e33327e

Please sign in to comment.