Skip to content

Commit

Permalink
refactor: ipfs service path and minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
BEdev24 authored Sep 12, 2024
1 parent fcd49aa commit d1f5065
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
3 changes: 2 additions & 1 deletion backend/src/ipfs/services/ipfs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 4 additions & 4 deletions ipfs-service/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export class AppController {
}

@UseInterceptors(FileInterceptor('file'))
@Post()
async addDoc(@UploadedFile() file: Express.Multer.File): Promise<IpfsDto> {
return await this.appService.addDoc(file);
@Post('file')
async addFile(@UploadedFile() file: Express.Multer.File): Promise<IpfsDto> {
return await this.appService.addFile(file);
}

@Post('json')
Expand All @@ -34,7 +34,7 @@ export class AppController {

@Get(':cid')
async getDoc(@Param('cid') cid: string): Promise<IpfsDto> {
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`);
}
Expand Down
34 changes: 20 additions & 14 deletions ipfs-service/src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class AppService implements OnModuleInit {
this.logger.log(`IPNS PeerID: ${this.ipnsPeerId}`);
}

async addDoc(file: Express.Multer.File): Promise<IpfsDto> {
async addFile(file: Express.Multer.File): Promise<IpfsDto> {
try {
this.fs = unixfs(this.helia);
const fileBuffer = Buffer.from(file.buffer);
Expand Down Expand Up @@ -197,19 +197,6 @@ export class AppService implements OnModuleInit {
}
}

async getDoc(cidString: string): Promise<IpfsDto> {
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<IpfsDto> {
try {
this.fs = unixfs(this.helia);
Expand All @@ -233,6 +220,25 @@ export class AppService implements OnModuleInit {
}
}

async getDocByCid(cidString: string): Promise<IpfsDto> {
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()}`);
Expand Down

0 comments on commit d1f5065

Please sign in to comment.