maintenant seul les jpg et png sont cceptés pour l'avatar
This commit is contained in:
@@ -24,5 +24,13 @@ export const storageForAvatar = {
|
|||||||
const extension : string = MIME_TYPES[file.mimetype];
|
const extension : string = MIME_TYPES[file.mimetype];
|
||||||
cb(null, `${filename}${extension}`);
|
cb(null, `${filename}${extension}`);
|
||||||
}
|
}
|
||||||
})
|
}),
|
||||||
|
fileFilter: (req, file, cb) => {
|
||||||
|
if (file.mimetype === 'image/png' || file.mimetype === 'image/jpg' || file.mimetype === 'image/jpeg') {
|
||||||
|
cb(null, true);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cb(null, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
Body, Controller, Delete, Get, NotFoundException, Param, Patch, Post, Query, Redirect, Req, Res, UploadedFile, UseGuards, UseInterceptors
|
Body, Controller, Delete, Get, NotFoundException,HttpStatus, Param, Patch, Post, Query, Redirect, Req, Res, UploadedFile, UseGuards, UseInterceptors
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { FileInterceptor } from '@nestjs/platform-express';
|
import { FileInterceptor } from '@nestjs/platform-express';
|
||||||
import { Response } from 'express';
|
import { Response } from 'express';
|
||||||
@@ -31,19 +31,6 @@ export class UsersController {
|
|||||||
///////////////////////// RUD //////////////////////////
|
///////////////////////// RUD //////////////////////////
|
||||||
////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/**
|
|
||||||
* On ne fait de création via une route
|
|
||||||
* car un utilisateur est crée à la première connexion avec l'Oauth de 42.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// @UseGuards(AuthenticateGuard)
|
|
||||||
// @UseGuards(TwoFactorGuard)
|
|
||||||
// @Get()
|
|
||||||
// findOne(@Req() req) {
|
|
||||||
// console.log("Backend Getting current user");
|
|
||||||
// return this.usersService.findOne(req.user.id);
|
|
||||||
// }
|
|
||||||
|
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@UseGuards(TwoFactorGuard)
|
@UseGuards(TwoFactorGuard)
|
||||||
@Get()
|
@Get()
|
||||||
@@ -100,9 +87,14 @@ export class UsersController {
|
|||||||
@UseGuards(TwoFactorGuard)
|
@UseGuards(TwoFactorGuard)
|
||||||
@Post('avatar')
|
@Post('avatar')
|
||||||
@UseInterceptors(FileInterceptor('file', storageForAvatar))
|
@UseInterceptors(FileInterceptor('file', storageForAvatar))
|
||||||
uploadAvatar(@UploadedFile() file, @Req() request){
|
uploadAvatar(@UploadedFile() file, @Req() request, @Res() res){
|
||||||
const user : User = request.user;
|
const user : User = request.user;
|
||||||
this.usersService.updateAvatar(user.id, file.filename);
|
if (file)
|
||||||
|
{
|
||||||
|
this.usersService.updateAvatar(user.id, file.filename);
|
||||||
|
return res.status(HttpStatus.OK).json({message : "Avatar updated"});
|
||||||
|
}
|
||||||
|
return res.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE).json({message : "Unsupported media type. Please use a valid image file."});
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
|
|||||||
@@ -101,14 +101,20 @@
|
|||||||
// tmp
|
// tmp
|
||||||
console.log(data);
|
console.log(data);
|
||||||
|
|
||||||
await fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user/avatar`,
|
const responseWhenChangeAvatar = fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user/avatar`,
|
||||||
{
|
{
|
||||||
method : 'POST',
|
method : 'POST',
|
||||||
body : data,
|
body : data,
|
||||||
})
|
})
|
||||||
.then(() => uploadAvatarSuccess = true ) // for some reason it needs to be a function, i think a TS thing, not a promis otherwise
|
const responseFromServer = await responseWhenChangeAvatar;
|
||||||
.then(() => success.avatar = 'Your changes have been saved')
|
if (responseFromServer.ok === true) {
|
||||||
.catch(() => errors.avatar = 'Sorry failed to upload your new Avatar' );
|
uploadAvatarSuccess = true;
|
||||||
|
success.avatar = 'Your avatar has been updated';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
errors.avatar = responseFromServer.statusText;
|
||||||
|
}
|
||||||
|
|
||||||
await fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user/avatar`, {method: "GET"})
|
await fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user/avatar`, {method: "GET"})
|
||||||
.then(response => {return response.blob()})
|
.then(response => {return response.blob()})
|
||||||
.then(data => {
|
.then(data => {
|
||||||
|
|||||||
Reference in New Issue
Block a user