Commit ccc94c73 authored by nanahira's avatar nanahira

tune

parent 867eae47
......@@ -16,6 +16,14 @@ export class AppService extends ConsoleLogger {
super('app');
}
private async saveAvatar(url: string, size: number, buffer: Buffer) {
const avatar = new Avatar();
avatar.url = url;
avatar.size = size;
avatar.fromBuffer(buffer);
return this.db.getRepository(Avatar).save(avatar);
}
async getImage(username: string, size: number) {
const url = await this.avatarApi.getURLFromUsername(username);
const existingAvatar = await this.db.getRepository(Avatar).findOne({
......@@ -27,6 +35,7 @@ export class AppService extends ConsoleLogger {
if (existingAvatar) {
return existingAvatar.toBuffer();
}
this.log(`Resizing ${url} to ${size}`);
let originalAvatarBuffer: Buffer;
const originalAvatar = await this.db.getRepository(Avatar).findOne({
where: {
......@@ -39,22 +48,14 @@ export class AppService extends ConsoleLogger {
} else {
originalAvatarBuffer = await this.avatarApi.downloadAvatar(url);
try {
await this.db.getRepository(Avatar).save({
url,
size: 0,
buffer: originalAvatarBuffer,
});
await this.saveAvatar(url, 0, originalAvatarBuffer);
} catch (e) {
this.error(`Fail to save original avatar: ${e.message}`);
}
}
const avatarBuffer = await this.image.resize(originalAvatarBuffer, size);
try {
const avatar = new Avatar();
avatar.url = url;
avatar.size = size;
avatar.fromBuffer(avatarBuffer);
await this.db.getRepository(Avatar).save(avatar);
await this.saveAvatar(url, size, avatarBuffer);
} catch (e) {
this.error(`Fail to save avatar: ${e.message}`);
}
......
import { ApiProperty } from '@nestjs/swagger';
import { HttpException } from '@nestjs/common';
import { User } from '../entities/User.entity';
import { PageSettingsWise } from './PageSettings.dto';
export interface BlankReturnMessage {
......
......@@ -16,7 +16,7 @@ export class ImageService extends ConsoleLogger {
height: size,
});
} catch (e) {
console.error(`Error resizing image to ${size}: ${e.toString()}`);
this.error(`Error resizing image to ${size}: ${e.toString()}`);
throw new BlankReturnMessageDto(500, 'Error resizing image');
}
}
......
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