feat: 支持图片上传接口

This commit is contained in:
richarjiang
2025-09-12 14:23:18 +08:00
parent 97e6a0ff6d
commit 090b91e72d
6 changed files with 207 additions and 42 deletions

View File

@@ -14,11 +14,15 @@ import {
Inject,
Req,
NotFoundException,
UseInterceptors,
UploadedFile,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { Request } from 'express';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger as WinstonLogger } from 'winston';
import { UsersService } from './users.service';
import { UploadImageResponseDto } from './dto/upload-image.dto';
import { CreateUserDto } from './dto/create-user.dto';
import { UserResponseDto } from './dto/user-response.dto';
import { ApiOperation, ApiBody, ApiResponse, ApiTags, ApiQuery } from '@nestjs/swagger';
@@ -193,6 +197,49 @@ export class UsersController {
}
}
// 上传图片到COS
@UseGuards(JwtAuthGuard)
@Post('cos/upload-image')
@HttpCode(HttpStatus.OK)
@UseInterceptors(FileInterceptor('file'))
@ApiOperation({ summary: '上传图片到COS' })
@ApiResponse({
status: 200,
description: '图片上传成功',
type: UploadImageResponseDto,
})
@ApiResponse({
status: 400,
description: '上传失败:文件格式不支持或文件过大',
})
async uploadImageToCos(
@CurrentUser() user: AccessTokenPayload,
@UploadedFile() file: Express.Multer.File,
) {
try {
if (!file) {
return { code: ResponseCode.ERROR, message: '请选择要上传的图片文件' };
}
this.winstonLogger.log(`file: ${file}`, {
context: 'UsersController',
userId: user?.sub,
})
const data = await this.cosService.uploadImage(user.sub, file);
return data;
} catch (error) {
this.winstonLogger.error('上传图片失败', {
context: 'UsersController',
userId: user?.sub,
error: (error as Error).message,
stack: (error as Error).stack,
});
return { code: ResponseCode.ERROR, message: (error as Error).message };
}
}
// App Store 服务器通知接收接口
@Public()
@Post('app-store-notifications')