Commit dd882e3f authored by nanahira's avatar nanahira

add any url get

parent ebbbadba
Pipeline #15069 passed with stages
in 5 minutes and 48 seconds
......@@ -9,14 +9,19 @@ import {
import { AppService } from './app.service';
import { ApiOperation, ApiParam, ApiProduces } from '@nestjs/swagger';
import { BlankReturnMessageDto } from './dto/ReturnMessage.dto';
import { ConfigService } from '@nestjs/config';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
private readonly urlGetPrefix =
this.config.get<string>('URL_GET_PREFIX') || '';
constructor(
private readonly appService: AppService,
private config: ConfigService,
) {}
@Get('avatar/:username/:size/:filename.png')
@Header('Cache-Control', 'public, max-age=600')
// @Header('Content-Type', 'image/png')
@ApiOperation({ summary: '获取裁剪后的用户头像' })
@ApiParam({ name: 'username', description: '用户名' })
@ApiParam({ name: 'size', description: '头像尺寸', type: 'number' })
......@@ -35,4 +40,31 @@ export class AppController {
disposition: 'inline',
});
}
@Get('url/:url/:size/:filename.png')
@Header('Cache-Control', 'public, max-age=31536000')
@ApiOperation({ summary: '获取裁剪后的用户头像' })
@ApiParam({ name: 'url', description: '地址,没有前缀' })
@ApiParam({ name: 'size', description: '头像尺寸', type: 'number' })
@ApiParam({ name: 'filename', description: '图片文件名' })
@ApiProduces('image/png')
async getAnyImage(
@Param('url') url: string,
@Param('size', ParseIntPipe) size: number,
) {
if (size < 8 || size > 1000) {
throw new BlankReturnMessageDto(400, 'Invalid size').toException();
}
if (url.length > 247) {
throw new BlankReturnMessageDto(400, 'Invalid url').toException();
}
const buffer = await this.appService.getResizedImage(
`${this.urlGetPrefix}${url}`,
size,
);
return new StreamableFile(buffer, {
type: 'image/png',
disposition: 'inline',
});
}
}
......@@ -23,6 +23,7 @@ import { RedisModule } from '@nestjs-modules/ioredis';
type: 'postgres',
entities: [Avatar], // entities here
synchronize: !config.get('DB_NO_INIT'),
dropSchema: !!config.get('DB_DROP'),
host: config.get('DB_HOST'),
port: parseInt(config.get('DB_PORT')) || 5432,
username: config.get('DB_USER'),
......
......@@ -18,7 +18,11 @@ 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;
......@@ -26,8 +30,7 @@ export class AppService extends ConsoleLogger {
return this.db.getRepository(Avatar).save(avatar);
}
async getImage(username: string, size: number) {
const url = await this.avatarApi.getURLFromUsername(username);
async getResizedImage(url: string, size: number) {
const existingAvatar = await this.db.getRepository(Avatar).findOne({
where: {
url,
......@@ -74,4 +77,9 @@ export class AppService extends ConsoleLogger {
},
);
}
async getImage(username: string, size: number) {
const url = await this.avatarApi.getURLFromUsername(username);
return this.getResizedImage(url, 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