From 36872823f71cdbe29ef89735898242c43896862b Mon Sep 17 00:00:00 2001 From: luoer <952222@163.com> Date: Wed, 18 Oct 2023 11:41:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 7 ---- .env | 2 + .../dto/update-{{fileName name}}.dto.ts.hbs | 6 ++- .../{{fileName name}}.controller.ts.hbs | 26 ++++-------- src/config/config.service.ts | 2 +- src/modules/user/dto/update-user.dto.ts | 6 ++- .../log/entities/operationLog.entity.ts | 40 ++++++------------- src/storage/upload/entities/upload.entity.ts | 4 +- src/storage/upload/upload.controller.ts | 14 +++++-- src/storage/upload/upload.service.ts | 12 +++++- 10 files changed, 56 insertions(+), 63 deletions(-) delete mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 2963048..0000000 --- a/.dockerignore +++ /dev/null @@ -1,7 +0,0 @@ -dist -node_modules -scripts -.git -.gitea -.gitignore -README.md \ No newline at end of file diff --git a/.env b/.env index d1748d2..bb13c7b 100644 --- a/.env +++ b/.env @@ -37,6 +37,8 @@ DB_MYSQL_DATABASE = test1 # ======================================================================================== # 上传文件目录 UPLOAD_DIR = ./content/upload +# 上传文件前缀 +UPLOAD_PREFIX = /upload # 静态文件目录 STATIC_DIR = ./content/html diff --git a/scripts/template/module/dto/update-{{fileName name}}.dto.ts.hbs b/scripts/template/module/dto/update-{{fileName name}}.dto.ts.hbs index cb1c76d..6230b60 100644 --- a/scripts/template/module/dto/update-{{fileName name}}.dto.ts.hbs +++ b/scripts/template/module/dto/update-{{fileName name}}.dto.ts.hbs @@ -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; +} diff --git a/scripts/template/module/{{fileName name}}.controller.ts.hbs b/scripts/template/module/{{fileName name}}.controller.ts.hbs index 605f3a6..d37bd8d 100644 --- a/scripts/template/module/{{fileName name}}.controller.ts.hbs +++ b/scripts/template/module/{{fileName name}}.controller.ts.hbs @@ -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); } } diff --git a/src/config/config.service.ts b/src/config/config.service.ts index 13191cf..a212a20 100644 --- a/src/config/config.service.ts +++ b/src/config/config.service.ts @@ -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'); } /** diff --git a/src/modules/user/dto/update-user.dto.ts b/src/modules/user/dto/update-user.dto.ts index 78ab602..d21d4e4 100644 --- a/src/modules/user/dto/update-user.dto.ts +++ b/src/modules/user/dto/update-user.dto.ts @@ -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; +} diff --git a/src/monitor/log/entities/operationLog.entity.ts b/src/monitor/log/entities/operationLog.entity.ts index f199ae7..1b91455 100644 --- a/src/monitor/log/entities/operationLog.entity.ts +++ b/src/monitor/log/entities/operationLog.entity.ts @@ -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; } diff --git a/src/storage/upload/entities/upload.entity.ts b/src/storage/upload/entities/upload.entity.ts index ec6a291..4c87464 100644 --- a/src/storage/upload/entities/upload.entity.ts +++ b/src/storage/upload/entities/upload.entity.ts @@ -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; diff --git a/src/storage/upload/upload.controller.ts b/src/storage/upload/upload.controller.ts index 95cc64b..5baed66 100644 --- a/src/storage/upload/upload.controller.ts +++ b/src/storage/upload/upload.controller.ts @@ -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(); } diff --git a/src/storage/upload/upload.service.ts b/src/storage/upload/upload.service.ts index a9851f5..72fb5de 100644 --- a/src/storage/upload/upload.service.ts +++ b/src/storage/upload/upload.service.ts @@ -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); }