feat: 优化上传逻辑

master
luoer 2023-10-18 11:41:56 +08:00
parent e639798918
commit 36872823f7
10 changed files with 56 additions and 63 deletions

View File

@ -1,7 +0,0 @@
dist
node_modules
scripts
.git
.gitea
.gitignore
README.md

2
.env
View File

@ -37,6 +37,8 @@ DB_MYSQL_DATABASE = test1
# ========================================================================================
# 上传文件目录
UPLOAD_DIR = ./content/upload
# 上传文件前缀
UPLOAD_PREFIX = /upload
# 静态文件目录
STATIC_DIR = ./content/html

View File

@ -1,4 +1,8 @@
import { PartialType } from '@nestjs/swagger';
import { IsNumber } from 'class-validator';
import { Create{{upcaseName name}}Dto } from './create-{{fileName name}}.dto';
export class Update{{upcaseName name}}Dto extends PartialType(Create{{upcaseName name}}Dto) {}
export class Update{{upcaseName name}}Dto extends PartialType(Create{{upcaseName name}}Dto) {
@IsNumber()
id: number;
}

View File

@ -15,45 +15,35 @@ export class {{upcaseName name}}Controller extends BaseController {
super();
}
/**
* 新增{{cnName}}
*/
@Post()
@ApiOperation({ description: '新增{{cnName}}', operationId: 'add{{upcaseName name}}' })
add{{upcaseName name}}(@Body() create{{upcaseName name}}Dto: Create{{upcaseName name}}Dto) {
return this.{{lowcaseName name}}Service.create(create{{upcaseName name}}Dto);
}
/**
* 根据分页/过滤参数查询{{cnName}}
*/
@Get()
@Respond(RespondType.PAGINATION)
@ApiOkResponse({ isArray: true, type: {{upcaseName name}} })
@ApiOperation({ description: '查询{{cnName}}', operationId: 'get{{upcaseName name}}s' })
get{{upcaseName name}}s(@Query() query: Find{{upcaseName name}}Dto) {
return this.{{lowcaseName name}}Service.findMany(query);
}
/**
* 根据ID查询{{cnName}}
*/
@Get(':id')
get{{upcaseName name}}(@Param('id', ParseIntPipe) id: number): Promise<{{upcaseName name}}> {
@ApiOperation({ description: '获取{{cnName}}', operationId: 'get{{upcaseName name}}' })
get{{upcaseName name}}(id: number): Promise<{{upcaseName name}}> {
return this.{{lowcaseName name}}Service.findOne(id);
}
/**
* 根据ID更新{{cnName}}
*/
@Patch(':id')
update{{upcaseName name}}(@Param('id', ParseIntPipe) id: number, @Body() update{{upcaseName name}}Dto: Update{{upcaseName name}}Dto) {
@ApiOperation({ description: '更新{{cnName}}', operationId: 'set{{upcaseName name}}' })
update{{upcaseName name}}(id: number, @Body() update{{upcaseName name}}Dto: Update{{upcaseName name}}Dto) {
return this.{{lowcaseName name}}Service.update(+id, update{{upcaseName name}}Dto);
}
/**
* 根据ID删除{{cnName}}
*/
@Delete(':id')
del{{upcaseName name}}(@Param('id', ParseIntPipe) id: number) {
@ApiOperation({ description: '删除{{cnName}}', operationId: 'del{{upcaseName name}}' })
del{{upcaseName name}}(id: number) {
return this.{{lowcaseName name}}Service.remove(+id);
}
}

View File

@ -111,7 +111,7 @@ export class ConfigService {
* @default '/uploads'
*/
get uploadPrefix(): string {
return this.config.get('UPLOAD_URL', '/uploads');
return this.config.get('UPLOAD_URL', '/upload');
}
/**

View File

@ -1,4 +1,8 @@
import { PartialType } from '@nestjs/swagger';
import { CreateUserDto } from './create-user.dto';
import { IsNumber } from 'class-validator';
export class UpdateUserDto extends PartialType(CreateUserDto) {}
export class UpdateUserDto extends PartialType(CreateUserDto) {
@IsNumber()
id: number;
}

View File

@ -10,38 +10,22 @@ export class OperationLog extends BaseEntity {
@Column()
nickname: string;
/**
*
*/
@Column()
method: string;
/**
*
*/
@Column()
path: string;
/**
*
* @example 1
*/
@Column()
description: string;
/**
* IP
* @example '127.0.0.1'
*/
@Column()
ip: string;
/**
*
* @example '广东省深圳市'
*/
@Column()
addr: string;
/**
*
* @example 'chrome'
*/
@Column()
browser: string;
/**
*
* @example 'windows 10'
*/
@Column()
os: string;
}

View File

@ -1,7 +1,7 @@
import { BaseEntity } from '@/database';
import { Column, Entity } from 'typeorm';
@Entity()
@Entity({ orderBy: { id: 'DESC' } })
export class Upload extends BaseEntity {
/**
*
@ -33,7 +33,7 @@ export class Upload extends BaseEntity {
/**
*
* @example "xxx"
* @example "2afb1f8b83ef0cc564f227d75d0b6914"
*/
@Column({ comment: '文件哈希' })
hash: string;

View File

@ -6,8 +6,8 @@ import { CreateUploadDto } from './dto/create-upload.dto';
import { UploadService } from './upload.service';
import { Request } from 'express';
@ApiTags('upload')
@Controller('upload')
@ApiTags('file')
@Controller('file')
export class UploadController {
constructor(private readonly uploadService: UploadService) {}
@ -22,7 +22,7 @@ export class UploadController {
@Get()
@Respond(RespondType.PAGINATION)
@ApiOperation({ description: '批量查询', operationId: 'getUploads' })
@ApiOperation({ description: '批量查询', operationId: 'getFiles' })
findAll() {
return this.uploadService.findAll();
}
@ -33,8 +33,14 @@ export class UploadController {
return this.uploadService.findOne(+id);
}
@Get('hash/:hash')
@ApiOperation({ description: '查询文件是否已存在', operationId: 'getFileByHash' })
isHashExists(@Param('hash') hash: string) {
return this.uploadService.isHashExists(hash);
}
@Patch(':id')
@ApiOperation({ description: '更新', operationId: 'updateFile' })
@ApiOperation({ description: '更新', operationId: 'setFile' })
update() {
return this.uploadService.update();
}

View File

@ -17,7 +17,7 @@ export class UploadService extends BaseService {
* @returns
*/
async create(file: Express.Multer.File) {
const path = relative(this.config.uploadDir, file.path).split(sep).join(posix.sep);
const path = relative(this.config.uploadDir, file.path).split(sep).join('/');
const uploadPrefix = this.config.uploadPrefix;
const uploadUrl = `${uploadPrefix}/${path}`;
const upload = this.uploadRepository.create({
@ -44,6 +44,16 @@ export class UploadService extends BaseService {
return `This action updates a #${1} upload`;
}
/**
*
* @param hash
* @returns
*/
async isHashExists(hash: string) {
const count = await this.uploadRepository.count({ where: { hash } });
return count > 0;
}
remove(id: number) {
return this.uploadRepository.softDelete(id);
}