fix: file response

This commit is contained in:
Innei
2022-05-22 17:33:45 +08:00
parent 59c50a4670
commit 62ffd6f28b
2 changed files with 7 additions and 7 deletions

View File

@@ -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,
}
}

View File

@@ -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 })