diff --git a/backend/src/ipfs/services/ipfs.service.ts b/backend/src/ipfs/services/ipfs.service.ts index 30054ab2..3bf86bea 100644 --- a/backend/src/ipfs/services/ipfs.service.ts +++ b/backend/src/ipfs/services/ipfs.service.ts @@ -52,7 +52,8 @@ export class IpfsService { const formData = new FormData(); formData.append('file', blob, file.originalname); - const apiLink = this.configService.getOrThrow('IPFS_SERVICE_URL') + '/ipfs'; + const apiLink = + this.configService.getOrThrow('IPFS_SERVICE_URL') + '/ipfs/file'; const requestConfig = { headers: { 'Content-Type': 'multipart/form-data', diff --git a/ipfs-service/src/app.controller.ts b/ipfs-service/src/app.controller.ts index 666c95c9..18471ad5 100644 --- a/ipfs-service/src/app.controller.ts +++ b/ipfs-service/src/app.controller.ts @@ -22,9 +22,9 @@ export class AppController { } @UseInterceptors(FileInterceptor('file')) - @Post() - async addDoc(@UploadedFile() file: Express.Multer.File): Promise { - return await this.appService.addDoc(file); + @Post('file') + async addFile(@UploadedFile() file: Express.Multer.File): Promise { + return await this.appService.addFile(file); } @Post('json') @@ -34,7 +34,7 @@ export class AppController { @Get(':cid') async getDoc(@Param('cid') cid: string): Promise { - const doc = await this.appService.getDoc(cid); + const doc = await this.appService.getDocByCid(cid); if (!doc) { throw new NotFoundException(`Document with cid: ${cid} not found`); } diff --git a/ipfs-service/src/app.service.ts b/ipfs-service/src/app.service.ts index 9c2bec45..d566e24b 100644 --- a/ipfs-service/src/app.service.ts +++ b/ipfs-service/src/app.service.ts @@ -163,7 +163,7 @@ export class AppService implements OnModuleInit { this.logger.log(`IPNS PeerID: ${this.ipnsPeerId}`); } - async addDoc(file: Express.Multer.File): Promise { + async addFile(file: Express.Multer.File): Promise { try { this.fs = unixfs(this.helia); const fileBuffer = Buffer.from(file.buffer); @@ -197,19 +197,6 @@ export class AppService implements OnModuleInit { } } - async getDoc(cidString: string): Promise { - this.fs = unixfs(this.helia); - const decoder = new TextDecoder(); - const cid = CID.parse(cidString); - let text = ''; - for await (const chunk of this.fs.cat(cid)) { - text += decoder.decode(chunk, { - stream: true, - }); - } - return IpfsMapper.ipfsToIpfsDto(cidString, text); - } - async addJson(json: string): Promise { try { this.fs = unixfs(this.helia); @@ -233,6 +220,25 @@ export class AppService implements OnModuleInit { } } + async getDocByCid(cidString: string): Promise { + try { + this.fs = unixfs(this.helia); + const decoder = new TextDecoder(); + const cid = CID.parse(cidString); + + let text = ''; + for await (const chunk of this.fs.cat(cid)) { + text += decoder.decode(chunk, { + stream: true, + }); + } + return IpfsMapper.ipfsToIpfsDto(cidString, text); + } catch (error) { + this.logger.error(`Failed to get doc by CID - ${cidString} error: ${error}`); + return null; + } +} + async provideCidtoDHTViaQueue(cid: CID) { await this.helia.libp2p.contentRouting.provide(cid); this.logger.log(`Announced CID to the DHT: ${cid.toString()}`);