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 { ...@@ -4,12 +4,14 @@ import {
Header, Header,
Param, Param,
ParseIntPipe, ParseIntPipe,
Res,
StreamableFile, StreamableFile,
} from '@nestjs/common'; } from '@nestjs/common';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { ApiOperation, ApiParam, ApiProduces } from '@nestjs/swagger'; import { ApiOperation, ApiParam, ApiProduces } from '@nestjs/swagger';
import { BlankReturnMessageDto } from './dto/ReturnMessage.dto'; import { BlankReturnMessageDto } from './dto/ReturnMessage.dto';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { Response } from 'express';
@Controller() @Controller()
export class AppController { export class AppController {
...@@ -21,7 +23,6 @@ export class AppController { ...@@ -21,7 +23,6 @@ export class AppController {
) {} ) {}
@Get('avatar/:username/:size/:filename.png') @Get('avatar/:username/:size/:filename.png')
@Header('Cache-Control', 'public, max-age=600')
@ApiOperation({ summary: '获取裁剪后的用户头像' }) @ApiOperation({ summary: '获取裁剪后的用户头像' })
@ApiParam({ name: 'username', description: '用户名' }) @ApiParam({ name: 'username', description: '用户名' })
@ApiParam({ name: 'size', description: '头像尺寸', type: 'number' }) @ApiParam({ name: 'size', description: '头像尺寸', type: 'number' })
...@@ -30,11 +31,13 @@ export class AppController { ...@@ -30,11 +31,13 @@ export class AppController {
async getImage( async getImage(
@Param('username') username: string, @Param('username') username: string,
@Param('size', ParseIntPipe) size: number, @Param('size', ParseIntPipe) size: number,
@Res({ passthrough: true }) res: Response,
) { ) {
if (size < 8 || size > 1000) { if (size < 8 || size > 1000) {
throw new BlankReturnMessageDto(400, 'Invalid size').toException(); throw new BlankReturnMessageDto(400, 'Invalid size').toException();
} }
const buffer = await this.appService.getImage(username, size); const buffer = await this.appService.getImage(username, size);
res.header('Cache-Control', 'public, max-age=600');
return new StreamableFile(buffer, { return new StreamableFile(buffer, {
type: 'image/png', type: 'image/png',
disposition: 'inline', disposition: 'inline',
...@@ -51,6 +54,7 @@ export class AppController { ...@@ -51,6 +54,7 @@ export class AppController {
async getAnyImage( async getAnyImage(
@Param('url') url: string, @Param('url') url: string,
@Param('size', ParseIntPipe) size: number, @Param('size', ParseIntPipe) size: number,
@Res({ passthrough: true }) res: Response,
) { ) {
if (size < 8 || size > 1000) { if (size < 8 || size > 1000) {
throw new BlankReturnMessageDto(400, 'Invalid size').toException(); throw new BlankReturnMessageDto(400, 'Invalid size').toException();
...@@ -62,6 +66,7 @@ export class AppController { ...@@ -62,6 +66,7 @@ export class AppController {
`${this.urlGetPrefix}${url}`, `${this.urlGetPrefix}${url}`,
size, size,
); );
res.header('Cache-Control', 'public, max-age=31536000');
return new StreamableFile(buffer, { return new StreamableFile(buffer, {
type: 'image/png', type: 'image/png',
disposition: 'inline', disposition: 'inline',
......
...@@ -18,11 +18,7 @@ export class AppService extends ConsoleLogger { ...@@ -18,11 +18,7 @@ export class AppService extends ConsoleLogger {
super('app'); super('app');
} }
private async saveAvatar( private async saveAvatar(url: string, size: number, buffer: Buffer) {
url: string,
size: number,
buffer: Buffer,
) {
const avatar = new Avatar(); const avatar = new Avatar();
avatar.url = url; avatar.url = url;
avatar.size = size; 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