瀏覽代碼

Added endpoint for getting user profile image

Alex Tran 3 年之前
父節點
當前提交
bdf38e7668
共有 2 個文件被更改,包括 18 次插入8 次删除
  1. 6 3
      server/src/api-v1/user/user.controller.ts
  2. 12 5
      server/src/api-v1/user/user.service.ts

+ 6 - 3
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);
   }
 }

+ 12 - 5
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));
   }
 }