web端打通oss资源上传

This commit is contained in:
2026-07-01 21:46:36 +08:00
parent ef6cc18fb2
commit 139e37651b
22 changed files with 1186 additions and 40 deletions
@@ -14,6 +14,16 @@ export class UploadTokenDto {
fileName: string;
}
export class UploadFileDto {
@IsString()
@IsNotEmpty()
bizType: string;
@IsString()
@IsIn(['IMAGE', 'VIDEO', 'FILE'])
mediaType: string;
}
export class RegisterResourceDto {
@IsString()
@IsNotEmpty()
@@ -1,8 +1,23 @@
import { Body, Controller, Delete, Get, Param, Post, Put, Query, UseGuards } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Put,
Query,
UploadedFile,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { ResourceService } from './resource.service';
import { ResourceListQueryDto } from './dto/common-query.dto';
import { RegisterResourceDto, UpdateResourceDto, UploadTokenDto } from './dto/common-mutate.dto';
import { RegisterResourceDto, UpdateResourceDto, UploadFileDto, UploadTokenDto } from './dto/common-mutate.dto';
const DEFAULT_MAX_BYTES = 10 * 1024 * 1024;
@Controller('common/resources')
@UseGuards(JwtAuthGuard)
@@ -14,6 +29,16 @@ export class ResourceController {
return this.service.getUploadToken(dto);
}
@Post('upload')
@UseInterceptors(
FileInterceptor('file', {
limits: { fileSize: Number(process.env.OSS_MAX_UPLOAD_BYTES ?? DEFAULT_MAX_BYTES) },
}),
)
upload(@UploadedFile() file: Express.Multer.File, @Body() dto: UploadFileDto) {
return this.service.uploadFile(file, dto);
}
@Post()
register(@Body() dto: RegisterResourceDto) {
return this.service.register(dto);
@@ -1,4 +1,4 @@
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
import { BadRequestException, Inject, Injectable, NotFoundException } from '@nestjs/common';
import type { ResourceBizType, ResourceMediaType, ResourceOwnerType } from '@prisma/client';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../../common/prisma/prisma.module';
@@ -6,7 +6,9 @@ import { serializeBigInt } from '../../common/decorators/current-user.decorator'
import { OSS_PROVIDER } from '../../integrations/integrations.constants';
import type { IOssProvider } from '../../integrations/oss/oss.interface';
import type { ResourceListQueryDto } from './dto/common-query.dto';
import type { RegisterResourceDto, UpdateResourceDto, UploadTokenDto } from './dto/common-mutate.dto';
import type { RegisterResourceDto, UpdateResourceDto, UploadFileDto, UploadTokenDto } from './dto/common-mutate.dto';
const DEFAULT_MAX_BYTES = 10 * 1024 * 1024;
@Injectable()
export class ResourceService {
@@ -19,6 +21,23 @@ export class ResourceService {
return this.oss.getUploadToken(dto);
}
async uploadFile(file: Express.Multer.File | undefined, dto: UploadFileDto) {
if (!file) {
throw new BadRequestException('请选择要上传的文件');
}
const maxUploadBytes = Number(process.env.OSS_MAX_UPLOAD_BYTES ?? DEFAULT_MAX_BYTES);
if (file.size > maxUploadBytes) {
throw new BadRequestException(`文件不能超过 ${Math.floor(maxUploadBytes / 1024 / 1024)}MB`);
}
return this.oss.putObject({
bizType: dto.bizType,
mediaType: dto.mediaType,
fileName: file.originalname || 'upload.bin',
buffer: file.buffer,
mimeType: file.mimetype,
});
}
async register(dto: RegisterResourceDto) {
const resource = await this.prisma.commonResource.create({
data: {