Commit 7231a554 authored by nanahira's avatar nanahira

cache only succeeded things

parent 49f17d59
Pipeline #20243 passed with stages
in 6 minutes
......@@ -4,12 +4,14 @@ import {
Header,
Param,
ParseIntPipe,
Res,
StreamableFile,
} from '@nestjs/common';
import { AppService } from './app.service';
import { ApiOperation, ApiParam, ApiProduces } from '@nestjs/swagger';
import { BlankReturnMessageDto } from './dto/ReturnMessage.dto';
import { ConfigService } from '@nestjs/config';
import { Response } from 'express';
@Controller()
export class AppController {
......@@ -21,7 +23,6 @@ export class AppController {
) {}
@Get('avatar/:username/:size/:filename.png')
@Header('Cache-Control', 'public, max-age=600')
@ApiOperation({ summary: '获取裁剪后的用户头像' })
@ApiParam({ name: 'username', description: '用户名' })
@ApiParam({ name: 'size', description: '头像尺寸', type: 'number' })
......@@ -30,11 +31,13 @@ export class AppController {
async getImage(
@Param('username') username: string,
@Param('size', ParseIntPipe) size: number,
@Res({ passthrough: true }) res: Response,
) {
if (size < 8 || size > 1000) {
throw new BlankReturnMessageDto(400, 'Invalid size').toException();
}
const buffer = await this.appService.getImage(username, size);
res.header('Cache-Control', 'public, max-age=600');
return new StreamableFile(buffer, {
type: 'image/png',
disposition: 'inline',
......@@ -51,6 +54,7 @@ export class AppController {
async getAnyImage(
@Param('url') url: string,
@Param('size', ParseIntPipe) size: number,
@Res({ passthrough: true }) res: Response,
) {
if (size < 8 || size > 1000) {
throw new BlankReturnMessageDto(400, 'Invalid size').toException();
......@@ -62,6 +66,7 @@ export class AppController {
`${this.urlGetPrefix}${url}`,
size,
);
res.header('Cache-Control', 'public, max-age=31536000');
return new StreamableFile(buffer, {
type: 'image/png',
disposition: 'inline',
......
......@@ -18,11 +18,7 @@ export class AppService extends ConsoleLogger {
super('app');
}
private async saveAvatar(
url: string,
size: number,
buffer: Buffer,
) {
private async saveAvatar(url: string, size: number, buffer: Buffer) {
const avatar = new Avatar();
avatar.url = url;
avatar.size = size;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment