feat: 处理文件上传参数
This commit is contained in:
7
server/src/modules/system/file/dto/file.dto.ts
Normal file
7
server/src/modules/system/file/dto/file.dto.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { IsOptional, IsString } from 'class-validator';
|
||||
|
||||
export class FileDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
dir?: string;
|
||||
}
|
||||
21
server/src/modules/system/file/file.controller.ts
Normal file
21
server/src/modules/system/file/file.controller.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import {
|
||||
Controller,
|
||||
Post,
|
||||
UseInterceptors,
|
||||
UploadedFile,
|
||||
Body,
|
||||
} from '@nestjs/common';
|
||||
import { FileService } from './file.service';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { FileDto } from '@/modules/system/file/dto/file.dto';
|
||||
|
||||
@Controller()
|
||||
export class FileController {
|
||||
constructor(private readonly filesService: FileService) {}
|
||||
|
||||
@Post()
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
upload(@Body() dto: FileDto, @UploadedFile() file: Express.Multer.File) {
|
||||
return this.filesService.upload(dto, file);
|
||||
}
|
||||
}
|
||||
10
server/src/modules/system/file/file.module.ts
Normal file
10
server/src/modules/system/file/file.module.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { FileService } from './file.service';
|
||||
import { FileController } from './file.controller';
|
||||
import { MinioService } from '@/minio/minio.service';
|
||||
|
||||
@Module({
|
||||
controllers: [FileController],
|
||||
providers: [FileService, MinioService],
|
||||
})
|
||||
export class FileModule {}
|
||||
59
server/src/modules/system/file/file.service.ts
Normal file
59
server/src/modules/system/file/file.service.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '@/prisma/prisma.service';
|
||||
import path from 'path';
|
||||
import { Cache, CACHE_MANAGER } from '@nestjs/cache-manager';
|
||||
import { MinioService } from '@/minio/minio.service';
|
||||
import { nanoid } from 'nanoid';
|
||||
import sharp from 'sharp';
|
||||
import { FileDto } from '@/modules/system/file/dto/file.dto';
|
||||
|
||||
@Injectable()
|
||||
export class FileService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
@Inject(CACHE_MANAGER) private cacheService: Cache,
|
||||
private readonly minioService: MinioService,
|
||||
) {}
|
||||
|
||||
async getImageInfo(
|
||||
file: Express.Multer.File,
|
||||
): Promise<Partial<sharp.Metadata>> {
|
||||
try {
|
||||
return await sharp(file.buffer).metadata();
|
||||
} catch (error) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
async upload(dto: FileDto, file: Express.Multer.File) {
|
||||
// 处理nodejs中上传文件名UTF-8 字符问题
|
||||
file.originalname = Buffer.from(file.originalname, 'latin1').toString(
|
||||
'utf8',
|
||||
);
|
||||
|
||||
const metadata = await this.getImageInfo(file);
|
||||
|
||||
const fileName = path.join(
|
||||
dto.dir || '',
|
||||
`${nanoid()}${path.extname(file.originalname)}`,
|
||||
);
|
||||
|
||||
const filePath = await this.minioService.uploadFile(file, fileName);
|
||||
|
||||
const res = await this.prisma.file.create({
|
||||
data: {
|
||||
name: fileName,
|
||||
path: filePath,
|
||||
size: file.size,
|
||||
width: metadata.width,
|
||||
height: metadata.height,
|
||||
originName: file.originalname,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
id: res.id,
|
||||
path: res.path,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
import {
|
||||
Controller,
|
||||
Post,
|
||||
UseInterceptors,
|
||||
UploadedFile,
|
||||
ParseFilePipe,
|
||||
} from '@nestjs/common';
|
||||
import { FilesService } from './files.service';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
|
||||
@Controller()
|
||||
export class FilesController {
|
||||
constructor(private readonly filesService: FilesService) {}
|
||||
|
||||
@Post()
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
upload(
|
||||
@UploadedFile(new ParseFilePipe({ validators: [] }))
|
||||
file: Express.Multer.File,
|
||||
) {
|
||||
return this.filesService.upload(file);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { FilesService } from './files.service';
|
||||
import { FilesController } from './files.controller';
|
||||
import { MinioService } from '@/minio/minio.service';
|
||||
|
||||
@Module({
|
||||
controllers: [FilesController],
|
||||
providers: [FilesService, MinioService],
|
||||
})
|
||||
export class FilesModule {}
|
||||
@@ -1,31 +0,0 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '@/prisma/prisma.service';
|
||||
|
||||
import path from 'path';
|
||||
import { Cache, CACHE_MANAGER } from '@nestjs/cache-manager';
|
||||
import { MinioService } from '@/minio/minio.service';
|
||||
import { nanoid } from 'nanoid';
|
||||
|
||||
@Injectable()
|
||||
export class FilesService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
@Inject(CACHE_MANAGER) private cacheService: Cache,
|
||||
private readonly minioService: MinioService,
|
||||
) {}
|
||||
|
||||
async upload(file: Express.Multer.File) {
|
||||
const fileName = `${nanoid()}${path.extname(file.originalname)}`;
|
||||
|
||||
const filePath = await this.minioService.uploadFile(file, fileName);
|
||||
|
||||
const res = await this.prisma.file.create({
|
||||
data: { name: fileName, path: filePath },
|
||||
});
|
||||
|
||||
return {
|
||||
id: res.id,
|
||||
path: res.path,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import { RouterModule } from '@nestjs/core';
|
||||
import { UserModule } from './user/user.module';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import { RoleModule } from './role/role.module';
|
||||
import { FilesModule } from './files/files.module';
|
||||
import { FileModule } from '@/modules/system/file/file.module';
|
||||
import { MenuModule } from './menu/menu.module';
|
||||
import { ConfigModule } from './config/config.module';
|
||||
import { PermissionModule } from './permission/permission.module';
|
||||
@@ -28,8 +28,8 @@ import { PermissionModule } from './permission/permission.module';
|
||||
module: RoleModule,
|
||||
},
|
||||
{
|
||||
path: 'files',
|
||||
module: FilesModule,
|
||||
path: 'file',
|
||||
module: FileModule,
|
||||
},
|
||||
{
|
||||
path: 'menu',
|
||||
@@ -49,7 +49,7 @@ import { PermissionModule } from './permission/permission.module';
|
||||
UserModule,
|
||||
AuthModule,
|
||||
RoleModule,
|
||||
FilesModule,
|
||||
FileModule,
|
||||
MenuModule,
|
||||
ConfigModule,
|
||||
PermissionModule,
|
||||
|
||||
Reference in New Issue
Block a user