mirror of
https://github.com/immich-app/immich
synced 2025-11-14 17:36:12 +00:00
Upload profile picture and convert into webp
This commit is contained in:
parent
c28251b8b4
commit
e33566a04a
6 changed files with 94 additions and 10 deletions
|
|
@ -1,11 +1,12 @@
|
|||
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, ValidationPipe, Put, Query } from '@nestjs/common';
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, ValidationPipe, Put, Query, UseInterceptors, UploadedFile } from '@nestjs/common';
|
||||
import { UserService } from './user.service';
|
||||
import { JwtAuthGuard } from '../../modules/immich-jwt/guards/jwt-auth.guard';
|
||||
import { AuthUserDto, GetAuthUser } from '../../decorators/auth-user.decorator';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { AdminRolesGuard } from '../../middlewares/admin-role-guard.middleware';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { boolean } from 'joi';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { profileImageUploadOption } from '../../config/profile-image-upload.config';
|
||||
|
||||
@Controller('user')
|
||||
export class UserController {
|
||||
|
|
@ -35,4 +36,16 @@ export class UserController {
|
|||
async updateUser(@Body(ValidationPipe) updateUserDto: UpdateUserDto) {
|
||||
return await this.userService.updateUser(updateUserDto)
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@UseInterceptors(FileInterceptor('file', profileImageUploadOption))
|
||||
@Post('/profile-image')
|
||||
async createProfileImage(@GetAuthUser() authUser: AuthUserDto, @UploadedFile() fileInfo: Express.Multer.File) {
|
||||
return await this.userService.createProfileImage(authUser, fileInfo);
|
||||
}
|
||||
|
||||
@Get('/profile-image/:userId')
|
||||
async getProfileImage(@Param('assetId') assetId: string) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import { CreateUserDto } from './dto/create-user.dto';
|
|||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { UserEntity } from './entities/user.entity';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
|
||||
import sharp from 'sharp';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
|
|
@ -124,4 +124,35 @@ export class UserService {
|
|||
throw new InternalServerErrorException('Failed to register new user');
|
||||
}
|
||||
}
|
||||
|
||||
async createProfileImage(authUser: AuthUserDto, fileInfo: Express.Multer.File) {
|
||||
try {
|
||||
// Convert file to jpeg
|
||||
let filePath = ''
|
||||
const fileSave = await sharp(fileInfo.path).webp().resize(512, 512).toFile(fileInfo.path + '.webp')
|
||||
|
||||
if (fileSave) {
|
||||
filePath = fileInfo.path + '.webp';
|
||||
await this.userRepository.update(authUser.id, {
|
||||
profileImagePath: filePath
|
||||
})
|
||||
} else {
|
||||
filePath = fileInfo.path;
|
||||
await this.userRepository.update(authUser.id, {
|
||||
profileImagePath: filePath
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
userId: authUser.id,
|
||||
profileImagePath: filePath
|
||||
};
|
||||
} catch (e) {
|
||||
Logger.error(e, 'Create User Profile Image');
|
||||
throw new InternalServerErrorException('Failed to create new user profile image');
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue