feat: 实现存储抽象、任务队列与区块链/归属地 Mock 适配
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import {
|
||||
Controller,
|
||||
Post,
|
||||
Query,
|
||||
UploadedFile,
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { ApiBearerAuth, ApiConsumes, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
|
||||
import {
|
||||
ALLOWED_DOC_TYPES,
|
||||
ALLOWED_IMAGE_TYPES,
|
||||
ALLOWED_VIDEO_TYPES,
|
||||
ErrorCode,
|
||||
MAX_FILE_SIZE,
|
||||
MAX_IMAGE_SIZE,
|
||||
MAX_VIDEO_SIZE,
|
||||
} from '@artveris/shared';
|
||||
import { StorageService } from '../storage/storage.service';
|
||||
import { BusinessException } from '../common/exceptions/business.exception';
|
||||
|
||||
type UploadType = 'image' | 'video' | 'file';
|
||||
|
||||
const RULES: Record<UploadType, { types: string[]; max: number; label: string }> = {
|
||||
image: { types: ALLOWED_IMAGE_TYPES, max: MAX_IMAGE_SIZE, label: '图片' },
|
||||
video: { types: ALLOWED_VIDEO_TYPES, max: MAX_VIDEO_SIZE, label: '视频' },
|
||||
file: { types: ALLOWED_DOC_TYPES, max: MAX_FILE_SIZE, label: '文件' },
|
||||
};
|
||||
|
||||
@ApiTags('文件上传')
|
||||
@ApiBearerAuth()
|
||||
@Controller('upload')
|
||||
export class UploadController {
|
||||
constructor(private readonly storage: StorageService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: '上传文件(图片/视频/文档)' })
|
||||
@ApiConsumes('multipart/form-data')
|
||||
@ApiQuery({ name: 'type', enum: ['image', 'video', 'file'], required: false })
|
||||
@UseInterceptors(FileInterceptor('file', { limits: { fileSize: MAX_VIDEO_SIZE } }))
|
||||
async upload(
|
||||
@UploadedFile() file: Express.Multer.File,
|
||||
@Query('type') type: UploadType = 'image',
|
||||
) {
|
||||
if (!file) {
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, '请选择要上传的文件');
|
||||
}
|
||||
const rule = RULES[type] ?? RULES.image;
|
||||
if (!rule.types.includes(file.mimetype)) {
|
||||
throw new BusinessException(
|
||||
ErrorCode.FILE_TYPE_NOT_ALLOWED,
|
||||
`不支持的${rule.label}类型:${file.mimetype}`,
|
||||
);
|
||||
}
|
||||
if (file.size > rule.max) {
|
||||
throw new BusinessException(
|
||||
ErrorCode.FILE_TOO_LARGE,
|
||||
`${rule.label}超出大小限制(${Math.round(rule.max / 1024 / 1024)}MB)`,
|
||||
);
|
||||
}
|
||||
return this.storage.save(file.buffer, file.originalname, file.mimetype);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UploadController } from './upload.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [UploadController],
|
||||
})
|
||||
export class UploadModule {}
|
||||
Reference in New Issue
Block a user