Commit ccc94c73 authored by nanahira's avatar nanahira

tune

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