Added endpoint for getting user profile image

This commit is contained in:
Alex Tran 2022-05-27 22:24:58 -05:00
parent e33566a04a
commit bdf38e7668
2 changed files with 18 additions and 8 deletions

View File

@ -1,4 +1,4 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, ValidationPipe, Put, Query, UseInterceptors, UploadedFile } from '@nestjs/common';
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, ValidationPipe, Put, Query, UseInterceptors, UploadedFile, Response } 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';
@ -7,6 +7,7 @@ import { AdminRolesGuard } from '../../middlewares/admin-role-guard.middleware';
import { UpdateUserDto } from './dto/update-user.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { profileImageUploadOption } from '../../config/profile-image-upload.config';
import { Response as Res } from 'express';
@Controller('user')
export class UserController {
@ -45,7 +46,9 @@ export class UserController {
}
@Get('/profile-image/:userId')
async getProfileImage(@Param('assetId') assetId: string) {
async getProfileImage(@Param('userId') userId: string,
@Response({ passthrough: true }) res: Res,
) {
return await this.userService.getUserProfileImage(userId, res);
}
}

View File

@ -1,4 +1,4 @@
import { BadRequestException, Injectable, InternalServerErrorException, Logger } from '@nestjs/common';
import { BadRequestException, Injectable, InternalServerErrorException, Logger, StreamableFile } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Not, Repository } from 'typeorm';
import { AuthUserDto } from '../../decorators/auth-user.decorator';
@ -7,6 +7,8 @@ import { UpdateUserDto } from './dto/update-user.dto';
import { UserEntity } from './entities/user.entity';
import * as bcrypt from 'bcrypt';
import sharp from 'sharp';
import { createReadStream } from 'fs';
import { Response as Res } from 'express';
@Injectable()
export class UserService {
@ -129,9 +131,9 @@ export class UserService {
try {
// Convert file to jpeg
let filePath = ''
const fileSave = await sharp(fileInfo.path).webp().resize(512, 512).toFile(fileInfo.path + '.webp')
const convertImageInfo = await sharp(fileInfo.path).webp().resize(512, 512).toFile(fileInfo.path + '.webp')
if (fileSave) {
if (convertImageInfo) {
filePath = fileInfo.path + '.webp';
await this.userRepository.update(authUser.id, {
profileImagePath: filePath
@ -140,11 +142,9 @@ export class UserService {
filePath = fileInfo.path;
await this.userRepository.update(authUser.id, {
profileImagePath: filePath
})
}
return {
userId: authUser.id,
profileImagePath: filePath
@ -153,6 +153,13 @@ export class UserService {
Logger.error(e, 'Create User Profile Image');
throw new InternalServerErrorException('Failed to create new user profile image');
}
}
async getUserProfileImage(userId: string, res: Res) {
const user = await this.userRepository.findOne({ id: userId })
res.set({
'Content-Type': 'image/webp',
});
return new StreamableFile(createReadStream(user.profileImagePath));
}
}