From e33327e6d7121aba971daf252a5b269dec05e364 Mon Sep 17 00:00:00 2001 From: Andrea Antonutti <56296563+1M4nt0@users.noreply.github.com> Date: Wed, 8 Nov 2023 10:27:17 +0100 Subject: [PATCH] Fix/fix ipfs uploading error (#39) * fix: fix ipfs image uploading error * refactor: Refactor ipfs connector upload input structure --- apps/web/pages/api/v0/course/credential.ts | 20 ++++++++++---------- apps/web/pages/api/v0/course/metadata.ts | 21 +++++++++++---------- packages/ipfs/src/connectors/infura.ts | 14 +++++++++----- packages/ipfs/src/index.ts | 14 +++++++++----- 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/apps/web/pages/api/v0/course/credential.ts b/apps/web/pages/api/v0/course/credential.ts index 1ef94c4a..5f725a17 100644 --- a/apps/web/pages/api/v0/course/credential.ts +++ b/apps/web/pages/api/v0/course/credential.ts @@ -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: { diff --git a/apps/web/pages/api/v0/course/metadata.ts b/apps/web/pages/api/v0/course/metadata.ts index 87448cfb..61cd824c 100644 --- a/apps/web/pages/api/v0/course/metadata.ts +++ b/apps/web/pages/api/v0/course/metadata.ts @@ -32,16 +32,17 @@ 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, @@ -49,9 +50,9 @@ const handlePostRequest = async ( 'snapshot-ens': snapshotEns, 'media-channel': mediaChannel, }, - '', - 'data/json', - ) + fileName: '', + mimeType: 'data/json', + }) res.status(200).json({ status: ApiResponseStatus.success, diff --git a/packages/ipfs/src/connectors/infura.ts b/packages/ipfs/src/connectors/infura.ts index 5160a0c8..b60f3dee 100644 --- a/packages/ipfs/src/connectors/infura.ts +++ b/packages/ipfs/src/connectors/infura.ts @@ -15,11 +15,15 @@ export class InfuraIpfsConnector implements IpfsConnector { this.ipfsGateway = ipfsGateway } - public async upload( - fileContent: Buffer | Record, - fileName: string, - mimeType: string, - ): Promise { + public async upload({ + fileContent, + fileName, + mimeType, + }: { + fileContent: Buffer | Record + fileName: string + mimeType: string + }): Promise { const formData = new FormData() if (fileContent instanceof Buffer) { diff --git a/packages/ipfs/src/index.ts b/packages/ipfs/src/index.ts index bac77ea2..3a038168 100644 --- a/packages/ipfs/src/index.ts +++ b/packages/ipfs/src/index.ts @@ -9,9 +9,13 @@ export type IpfsUploadResult = { } export interface IpfsConnector { - upload( - fileContent: Buffer | Record, - mimeType?: string, - fileName?: string, - ): Promise + upload({ + fileContent, + mimeType, + fileName, + }: { + fileContent: Buffer | Record + mimeType?: string + fileName?: string + }): Promise }