Skip to content

Commit

Permalink
addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Nov 30, 2022
1 parent 4a10300 commit 0350fbe
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "node-ts",
"version": "0.4.4",
"version": "0.5.0",
"version": "0.5.1",
"description": "Nevermined Node",
"main": "main.ts",
"scripts": {
Expand Down
39 changes: 17 additions & 22 deletions src/access/access.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@ import { UploadResult } from './dto/upload-result';
import { AgreementData } from '@nevermined-io/nevermined-sdk-js/dist/node/keeper/contracts/managers';
import { utils } from '@nevermined-io/nevermined-sdk-js';

export enum UploadBackends {
IPFS = 'ipfs',
Filecoin = 'filecoin',
AmazonS3 = 's3',
}

@ApiTags('Access')
@Controller()
export class AccessController {

constructor(private nvmService: NeverminedService) {}

@Get('access/:agreement_id/:index')
Expand Down Expand Up @@ -148,7 +155,7 @@ export class AccessController {
})
async doUpload(
@Body() uploadData: UploadDto,
@Param('backend') backend: string,
@Param('backend') backend: UploadBackends,
@UploadedFile() file: Express.Multer.File
): Promise<UploadResult> {
let data: Buffer
Expand All @@ -162,35 +169,23 @@ export class AccessController {
} else {
throw new BadRequestException('No file or message in request');
}
if (backend !== 's3' && backend !== 'filecoin' && backend !== 'ipfs')
console.log(`Backend ${backend}`)
if (!Object.values(UploadBackends).includes(backend))
throw new BadRequestException(`Backend ${backend} not supported`);
try {
let url: string
if (uploadData.encrypt) {
// generate password
Logger.debug(`Uploading with password, filename ${fileName}`);
const password = crypto.randomBytes(32).toString('base64url');
data = Buffer.from(aes_encryption_256(data, password), 'binary');
if (backend === 's3') {
const url = await this.nvmService.uploadS3(data, fileName);
return { url, password };
} else if (backend === 'filecoin') {
const url = await this.nvmService.uploadFilecoin(data, fileName);
return { url, password };
} else if (backend === 'ipfs') {
const url = await this.nvmService.uploadIPFS(data, fileName);
return { url, password };
}
}
if (backend === 's3') {
const url = await this.nvmService.uploadS3(data, fileName);
return { url };
} else if (backend === 'filecoin') {
const url = await this.nvmService.uploadFilecoin(data, fileName);
return { url };
} else if (backend === 'ipfs') {
const url = await this.nvmService.uploadIPFS(data, fileName);
return { url };
url = await this.nvmService.uploadToBackend(backend, data, fileName);
return { url, password };
}

url = await this.nvmService.uploadToBackend(backend, data, fileName);
return { url };

} catch (error) {
Logger.error(`Error processing upload: ${error.message}`);
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
Expand Down
1 change: 1 addition & 0 deletions src/access/access.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ describe('Info', () => {
message: 'hi there'
});
expect(response.statusCode).toBe(201);
console.log(JSON.stringify(response.body))
expect(response.body.url).toContain('cid://');
});
});
2 changes: 1 addition & 1 deletion src/access/dto/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class UploadDto {
example: 'false',
required: false,
})
encrypt: string;
encrypt: string;
@ApiProperty({
type: string,
example: 'Hello!',
Expand Down
11 changes: 11 additions & 0 deletions src/shared/nevermined/nvm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { HttpModuleOptions, HttpService } from '@nestjs/axios';
import { firstValueFrom } from 'rxjs';
import { AxiosError } from 'axios';
import IpfsHttpClientLite from 'ipfs-http-client-lite';
import { UploadBackends } from 'src/access/access.controller';

@Injectable()
export class NeverminedService {
Expand Down Expand Up @@ -256,6 +257,16 @@ export class NeverminedService {
}
}

async uploadToBackend(backend: UploadBackends, data: Buffer, fileName: string): Promise<string> {
if (backend === 's3') {
return await this.uploadS3(data, fileName);
} else if (backend === 'filecoin') {
return await this.uploadFilecoin(data, fileName);
} else if (backend === 'ipfs') {
return await this.uploadIPFS(data, fileName);
}
}

private getIPFSAuthToken(): string | undefined {

if (!this.config.get('IPFS_PROJECT_ID') || !this.config.get('IPFS_PROJECT_SECRET')) {
Expand Down

0 comments on commit 0350fbe

Please sign in to comment.