correction de l'upload d'image, reste à faire en sorte de n'accepter que du png ou jpg

This commit is contained in:
batche
2022-11-17 18:01:52 +01:00
parent f98e1fdb4e
commit 686067375a
7 changed files with 52 additions and 26 deletions

View File

@@ -10,5 +10,3 @@
!api_back/*.lock
!api_back/.env
!api_back/src/uploads/avatars/default.png
!api_back/*.jpg
!api_back/*.jpeg

View File

@@ -2,10 +2,11 @@ FROM node:alpine AS development
WORKDIR /usr/app
COPY api_back/* .
COPY api_back/.env .env
COPY ./api_back ./
COPY ./api_back/.env ./.env
COPY ./api_back/src/uploads/avatars/default.png ./uploads/avatars/default.png
RUN npm ci
CMD [ "npm", "run", "start:dev" ]

View File

@@ -4,6 +4,7 @@ import { User } from 'src/users/entities/user.entity';
import { UsersService } from 'src/users/users.service';
import { toFileStream } from 'qrcode';
import { authenticator } from 'otplib';
import { randomUUID } from 'crypto';
@Injectable()
export class AuthenticationService {
@@ -16,7 +17,19 @@ export class AuthenticationService {
const user = await this.userService.findOneByFourtyTwoId(createUsersDto.fortyTwoId);
if (user)
return user;
return this.userService.create(createUsersDto);
let check_name : boolean = false;
if (!user)
check_name = await this.userService.isUsernameExists(createUsersDto.username);
if (!check_name)
return await this.userService.create(createUsersDto);
let createUsersDtoWithUsername : CreateUsersDto = createUsersDto;
while (check_name === true)
{
createUsersDtoWithUsername = { ...createUsersDto, username: createUsersDto.username + randomUUID() };
check_name = await this.userService.isUsernameExists(createUsersDtoWithUsername.username);
}
return this.userService.create(createUsersDtoWithUsername);
}
async findUser(fourtytwo_id : string): Promise<User | undefined> {

View File

@@ -1,14 +1,21 @@
import { randomUUID } from "crypto";
import { diskStorage } from "multer";
import path from "path";
const MIME_TYPES = {
'image/jpg': 'jpg',
'image/jpeg': 'jpg',
'image/png': 'png'
};
export const storageForAvatar = {
storage: diskStorage({
destination: './uploads/avatars',
filename: (req, file, cb) => {
console.log(file);
const filename : string = file.originalname.split(' ').join('_') + randomUUID();
const extension : string = path.extname(file.originalname);
console.log("RES : " )
const extension : string = MIME_TYPES[file.mimetype];
cb(null, `${filename}${extension}`);
}
})

View File

@@ -1,5 +1,5 @@
import {
Body, Controller, Delete, Get, HttpException, NotFoundException, Patch, Post, Query, Req, Res, UploadedFile, UseGuards, UseInterceptors
Body, Controller, Delete, Get, NotFoundException, Patch, Post, Query, Req, Res, UploadedFile, UseGuards, UseInterceptors
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
@@ -8,22 +8,9 @@ import { ValidationPipe } from 'src/common/validation/validation.pipe';
import { UpdateUsersDto } from './dto/update-users.dto';
import { UsersService } from './users.service';
import { User } from './entities/user.entity';
import { diskStorage } from 'multer';
import path from 'path';
import { randomUUID } from 'crypto';
import { of } from 'rxjs';
import { storageForAvatar } from 'src/common/constants/constants';
export const storageForAvatar = {
storage: diskStorage({
destination: './uploads/avatars',
filename: (req, file, cb) => {
const filename : string = file.originalname.split(' ').join('_') + randomUUID();
console.log("RES : " )
const extension : string = path.extname(file.originalname);
cb(null, `${filename}${extension}`);
}
})
}
@Controller('user')
export class UsersController {

View File

@@ -40,9 +40,28 @@ export class UsersService {
throw new NotFoundException(`The requested user not found.`);
const partialUser : Partial<User> = {
username: user.username,
email: user.email,
image_url: user.image_url,
isEnabledTwoFactorAuth: user.isEnabledTwoFactorAuth,
status: user.status,
};
return partialUser;
}
async isUsernameExists(usernameToSearch: string): Promise<boolean> {
const user = await this.userRepository.findOneBy({username : usernameToSearch});
if (!user)
return false;
return true;
}
async findOneByUsername(username: string) {
const user = await this.userRepository.findOneBy({username : username});
if (!user)
throw new HttpException(`The user could not be found.`,HttpStatus.NOT_FOUND);
const partialUser : Partial<User> = {
username: user.username,
image_url: user.image_url,
status: user.status,
};
return partialUser;
}

View File

@@ -2,8 +2,9 @@ FROM node:alpine AS development
WORKDIR /usr/app
COPY ./api_front/* .
COPY ./api_front ./
RUN npm ci
CMD [ "npm", "run", "dev" ]