Skip to content

Commit

Permalink
changed avatar loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Dec 7, 2024
1 parent f46661a commit b643ae5
Showing 1 changed file with 2 additions and 17 deletions.
19 changes: 2 additions & 17 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,8 @@ export class UserService {

if (hash === 'none') return null;

const avatar_response = await axios.get(`${process.env.DISCORD_AVATAR}/${user_id}/${hash}?size=512`, { responseType: 'arraybuffer' });
let avatar_buffer = Buffer.from(avatar_response.data);

if (avatar_buffer[0] === 0x47 && avatar_buffer[1] === 0x49 && avatar_buffer[2] === 0x46) {
const sharp_obj = sharp(avatar_buffer);
const meta = await sharp_obj.metadata();
avatar_buffer = await sharp_obj
.extract({
left: 0,
top: 0,
width: Math.min(meta.width as number, 512),
height: Math.min(meta.height as number, 512)
})
.toBuffer();
}

const avatarB64 = avatar_buffer.toString('base64');
const avatar_response = await axios.get(`${process.env.DISCORD_AVATAR}/${user_id}/${hash}.png?size=512`, { responseType: 'arraybuffer' });
const avatarB64 = Buffer.from(avatar_response.data).toString('base64');
await this.cacheManager.set(`avatar:${user_id}`, avatarB64, 1000 * 60 * 60 * 24);
return avatarB64;
}
Expand Down

0 comments on commit b643ae5

Please sign in to comment.