Skip to content

Commit

Permalink
Merge 025db19 into bc59f32
Browse files Browse the repository at this point in the history
  • Loading branch information
guilherme1guy committed Sep 22, 2021
2 parents bc59f32 + 025db19 commit 9f1a98e
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 3 deletions.
19 changes: 18 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@nestjs/microservices": "^8.0.6",
"@nestjs/platform-express": "^8.0.0",
"@nestjs/swagger": "^5.0.9",
"@types/multer": "^1.4.7",
"amqp-connection-manager": "^3.6.0",
"amqplib": "^0.8.0",
"class-transformer": "^0.4.0",
Expand Down
13 changes: 13 additions & 0 deletions src/midia/dto/uploadFile.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class UploadFileDto {
constructor(file: Express.Multer.File) {
this.file = file;
this.bufferB64 = file.buffer.toString('base64');

// clean the file bufffer, because we are sending it as base64
// the default implementation messes up the data
this.file.buffer = null;
}

file: Express.Multer.File;
bufferB64: string;
}
55 changes: 53 additions & 2 deletions src/midia/midia.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import { Controller, Inject } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Inject,
Post,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { firstValueFrom, timeout } from 'rxjs';
import { FileInterceptor } from '@nestjs/platform-express';
import { ApiTags } from '@nestjs/swagger';
import { UploadFileDto } from './dto/uploadFile.dto';

@Controller('midias')
const TEN_SECONDS = 10000;

@ApiTags('midia')
@Controller('midia')
export class MidiaController {
constructor(
@Inject('MIDIA_SERVICE') private readonly midiaServiceClient: ClientProxy,
Expand All @@ -10,4 +25,40 @@ export class MidiaController {
async onApplicationBootstrap() {
await this.midiaServiceClient.connect();
}

@Post('getUrl')
public async getMidiaUrl(@Body('id') id: string): Promise<string> {
const midiaUrl: string = await firstValueFrom(
this.midiaServiceClient.send('getUrl', id).pipe(timeout(TEN_SECONDS)),
);

return midiaUrl;
}

@Post('uploadMidia')
@UseInterceptors(FileInterceptor('file'))
public async uploadMidia(
@UploadedFile() file: Express.Multer.File,
): Promise<string> {
const fileTransferable = new UploadFileDto(file);

const responseId: string = await firstValueFrom(
this.midiaServiceClient
.send('uploadMidia', fileTransferable)
.pipe(timeout(TEN_SECONDS)),
);

return responseId;
}

@Delete('removeMidia')
public async removeMidia(@Body() id: string): Promise<string> {
const response: string = await firstValueFrom(
this.midiaServiceClient
.send('removeMidia', id)
.pipe(timeout(TEN_SECONDS)),
);

return response;
}
}
75 changes: 75 additions & 0 deletions test/midia/midia.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { Test, TestingModule } from '@nestjs/testing';
import { Observable } from 'rxjs';
import { MidiaController } from '../../src/midia/midia.controller';

describe('MidiaController', () => {
let controller: MidiaController;

const customModule = async (fn: any) => {
return await Test.createTestingModule({
providers: [
{
provide: 'MIDIA_SERVICE',
useValue: {
send: fn,
},
},
],
controllers: [MidiaController],
}).compile();
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
Expand All @@ -23,4 +38,64 @@ describe('MidiaController', () => {
it('should be defined', () => {
expect(controller).toBeDefined();
});

it('should upload midia ', async () => {
const file = {
fieldname: 'file',
originalname: 'test.png',
encoding: '7bit',
mimetype: 'image/png',
buffer: Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
size: 8,
stream: null,
destination: null,
filename: null,
path: null,
};

const result = {
asset_id: 'fc92a7220a24f3aca8dccb6a0d59fc56',
public_id: 'bfuv1vwi2hmowrjwyaxh',
version: 1632330093,
version_id: 'd3fd59825199ae990a728e90741c222c',
signature: 'fc02a27eb48cbb1f8ba2f859da19b653958d3a41',
width: 384,
height: 332,
format: 'png',
resource_type: 'image',
created_at: '2021-09-22T17:01:33Z',
tags: [],
bytes: 1995,
type: 'upload',
etag: 'cf9b4ee84dbcc43bfaf59b8971aa0d25',
placeholder: false,
url: 'http://res.cloudinary.com/nova-cartografia-social/image/upload/v1632330093/bfuv1vwi2hmowrjwyaxh.png',
secure_url:
'https://res.cloudinary.com/nova-cartografia-social/image/upload/v1632330093/bfuv1vwi2hmowrjwyaxh.png',
original_filename: 'file',
api_key: '156751358469945',
};

const module = await await customModule(
jest.fn(() => new Observable((sub) => sub.next(result))),
);

controller = module.get<MidiaController>(MidiaController);

expect(await controller.uploadMidia(file)).toStrictEqual(result);
});

it('should get url ', async () => {
const id = '123';

const url = 'aspdaw';

const module = await await customModule(
jest.fn(() => new Observable((sub) => sub.next(url))),
);

controller = module.get<MidiaController>(MidiaController);

expect(await controller.getMidiaUrl(id)).toStrictEqual(url);
});
});

0 comments on commit 9f1a98e

Please sign in to comment.