diff --git a/src/modules/file/file.controller.ts b/src/modules/file/file.controller.ts index d2c7fe15..d14a9162 100644 --- a/src/modules/file/file.controller.ts +++ b/src/modules/file/file.controller.ts @@ -10,9 +10,8 @@ import { Query, Req, Res, - UseGuards, } from '@nestjs/common' -import { Throttle, ThrottlerGuard } from '@nestjs/throttler' +import { Throttle } from '@nestjs/throttler' import { Auth } from '~/common/decorator/auth.decorator' import { HTTPDecorators } from '~/common/decorator/http.decorator' @@ -47,8 +46,7 @@ export class FileController { } @Get('/:type/:name') - @UseGuards(ThrottlerGuard) - @Throttle(10, 60) + @Throttle(60, 60) @HTTPDecorators.Bypass async get(@Param() params: FileQueryDto, @Res() reply: FastifyReply) { const { type, name } = params @@ -75,12 +73,13 @@ export class FileController { const { type = 'file' } = query const ext = path.extname(file.filename) - const filename = md5(file.filename) + ext + const filename = md5(file.filename) + ext.toLowerCase() await this.service.writeFile(type, filename, file.file) return { - path: await this.service.resolveFileUrl(type, filename), + url: await this.service.resolveFileUrl(type, filename), + name: filename, } } diff --git a/src/modules/file/file.service.ts b/src/modules/file/file.service.ts index da992f3e..ed2d7419 100644 --- a/src/modules/file/file.service.ts +++ b/src/modules/file/file.service.ts @@ -38,7 +38,8 @@ export class FileService { return new Promise(async (resolve, reject) => { const filePath = this.resolveFilePath(type, name) if (await this.checkIsExist(filePath)) { - throw new BadRequestException('文件已存在') + reject(new BadRequestException('文件已存在')) + return } await fs.mkdir(path.dirname(filePath), { recursive: true })