Skip to content

Commit

Permalink
Merge pull request #13 from fga-eps-mds/buffer-image
Browse files Browse the repository at this point in the history
adiciona conversao da foto do usuario
  • Loading branch information
HenriqueAmorim20 authored Oct 27, 2023
2 parents 877f13a + 630cb4c commit 33b307d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sonar.python.version=3

sonar.sources=./src
sonar.inclusions=**
sonar.exclusions=**/main.ts,**/ormconfig.ts,**/*.module.ts,src/migration/*.ts,src/migrations.ts
sonar.exclusions=**/main.ts,**/ormconfig.ts,**/*.module.ts,src/migration/*.ts,src/migrations.ts,**/*.spec.ts
sonar.javascript.lcov.reportPaths=./coverage/unit/lcov.info
sonar.dynamicAnalysis=reuseReports
sonar.core.codeCoveragePlugin=cobertura
14 changes: 13 additions & 1 deletion src/shared/helpers/buffer-to-image.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { bufferToBase64, isBase64, isBase64Image } from './buffer-to-image';
import {
bufferToBase64,
getImageUri,
isBase64,
isBase64Image,
} from './buffer-to-image';

describe('Buffer to image', () => {
describe('bufferToBase64', () => {
Expand All @@ -10,6 +15,13 @@ describe('Buffer to image', () => {
expect(image).toEqual(str);
});

it('should getImageUri', async () => {
const str = '/9j/4AAQSkZJRgABAQAAAQABAAD';
const buff = Buffer.from(str, 'utf-8');
const image = getImageUri(buff);
expect(image).toEqual('data:image/png;base64,' + str);
});

it('should be bufferToBase64 a empty if null', async () => {
const str = 'null';
const buff = Buffer.from(str, 'utf-8');
Expand Down
4 changes: 4 additions & 0 deletions src/shared/helpers/buffer-to-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export const bufferToBase64 = (buffer: Buffer): string => {
return isBase64(base64) ? base64 : '';
};

export const getImageUri = (value: Buffer | string): string => {
return `data:image/png;base64,${Buffer.from(value).toString()}`;
};

export function isBase64(value: unknown): boolean {
return typeof value === 'string' && isBase64Image(value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/usuario/usuario.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class UsuarioController {

@Get(':id')
async findOne(@Param() param: IdValidator): Promise<Usuario> {
return this._service.findOne(param.id);
return this._service.findOne(param.id, true);
}

@Patch(':id')
Expand Down
10 changes: 10 additions & 0 deletions src/usuario/usuario.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ describe('UsuarioService', () => {
expect(found.id).toEqual(1);
});

it('should find Usuario with foto', async () => {
jest.spyOn(repository, 'findOneOrFail').mockReturnValue({
id: 1,
foto: Buffer.from('/9j/4AAQSkZJRgABAQAAAQABAAD', 'utf-8'),
} as any);

const found = await service.findOne(1, true);
expect(found.id).toEqual(1);
});

it('should remove Usuario', async () => {
jest.spyOn(repository, 'findOneOrFail').mockReturnValue({ id: 1 } as any);
jest.spyOn(repository, 'remove').mockReturnValue({ id: 1 } as any);
Expand Down
10 changes: 7 additions & 3 deletions src/usuario/usuario.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import bcrypt from 'bcrypt';
import { Repository } from 'typeorm';
import { Ordering } from '../shared/decorators/ordenate.decorator';
import { Pagination } from '../shared/decorators/paginate.decorator';
import { getImageUri } from '../shared/helpers/buffer-to-image';
import {
getWhereClauseNumber,
getWhereClauseString,
Expand Down Expand Up @@ -53,14 +54,17 @@ export class UsuarioService {
.getOne();
}

async findOne(id: number) {
return this._repository.findOneOrFail({ where: { id } });
async findOne(id: number, transformImage = false) {
const user = await this._repository.findOneOrFail({ where: { id } });
if (transformImage && user.foto) {
user.foto = getImageUri(user.foto) as unknown as Buffer;
}
return user;
}

async update(id: number, body: UpdateUsuarioDto): Promise<Usuario> {
const found = await this.findOne(id);
const merged = Object.assign(found, body);
// TODO caso a senha seja editada, também criptografar
return this._repository.save(merged);
}

Expand Down

0 comments on commit 33b307d

Please sign in to comment.