From bdf38e7668697e1fe3bed907c99617aea021475b Mon Sep 17 00:00:00 2001 From: Alex Tran Date: Fri, 27 May 2022 22:24:58 -0500 Subject: [PATCH] Added endpoint for getting user profile image --- server/src/api-v1/user/user.controller.ts | 9 ++++++--- server/src/api-v1/user/user.service.ts | 17 ++++++++++++----- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/server/src/api-v1/user/user.controller.ts b/server/src/api-v1/user/user.controller.ts index ab32e548f1..58c30a9e2a 100644 --- a/server/src/api-v1/user/user.controller.ts +++ b/server/src/api-v1/user/user.controller.ts @@ -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); } } diff --git a/server/src/api-v1/user/user.service.ts b/server/src/api-v1/user/user.service.ts index 0d54ef6cd7..2cf0fac725 100644 --- a/server/src/api-v1/user/user.service.ts +++ b/server/src/api-v1/user/user.service.ts @@ -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)); } }