Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
[Feat] SNS로그인시 프로필 이미지를 불러올 수 있도록 하라 (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
runasy-koonta authored Feb 14, 2024
2 parents 2d64d02 + 380103b commit d59a657
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 5 deletions.
39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"class-validator": "^0.14.1",
"date-fns": "^3.3.1",
"jsonwebtoken": "^9.0.2",
"md5": "^2.3.0",
"multer": "^1.4.5-lts.1",
"multer-s3": "^3.0.1",
"mysql2": "^3.9.0",
Expand All @@ -53,6 +54,7 @@
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/jsonwebtoken": "^9.0.5",
"@types/md5": "^2.3.5",
"@types/multer-s3": "^3.0.3",
"@types/node": "^20.3.1",
"@types/node-fetch": "^2.6.11",
Expand Down
4 changes: 3 additions & 1 deletion src/user/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export class UserEntity extends BaseEntity {
@Column()
oauthToken: string;

@OneToOne(() => UserProfileImageEntity, (profileImage) => profileImage.user)
@OneToOne(() => UserProfileImageEntity, (profileImage) => profileImage.user, {
cascade: true,
})
profileImage: UserProfileImageEntity;

@OneToMany(() => UserFollowingEntity, (following) => following.user)
Expand Down
5 changes: 3 additions & 2 deletions src/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { S3UtilService } from '../utils/S3.service';
import { S3Module } from '../utils/S3.module';

@Module({
imports: [S3Module],
controllers: [UserController],
providers: [UserService, S3UtilService],
providers: [UserService],
})
export class UserModule {}
64 changes: 62 additions & 2 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ResponseCode } from '../response/response-code.enum';
import { UserFollowingEntity } from './user.following.entity';
import { LessThan } from 'typeorm';
import { RuleInvitationEntity } from '../rule/domain/rule.invitation.entity';
import * as md5 from 'md5';
import { DiaryEntity } from '../diary/models/diary.entity';
import { S3UtilService } from '../utils/S3.service';

Expand Down Expand Up @@ -89,6 +90,12 @@ export class UserService {
return await response.json();
}

private async downloadImage(url: string) {
const response = await fetch(url);

return await response.buffer();
}

async Login(email: string, password: string) {
console.log(email, password);
const user = await UserEntity.findOne({
Expand Down Expand Up @@ -161,7 +168,35 @@ export class UserService {
userEntity.password = '';
userEntity.nickname = userProfile?.nickname;

// Todo: 프로필 이미지 저장하기
if (userProfile?.profile_image_url) {
const urlHash = md5(userProfile.profile_image_url);
const extension = userProfile.profile_image_url.split('.').pop();
const profileFilename = `profile/kakao_${urlHash}.${extension}`;

if (
!userEntity.profileImage ||
userEntity.profileImage.imageKey !== profileFilename
) {
const profileImageEntity = new UserProfileImageEntity();
profileImageEntity.imageKey = urlHash;

const profileImageFile = await this.downloadImage(
userProfile.profile_image_url,
);
await this.s3Service.putObject(profileFilename, profileImageFile);

profileImageEntity.imageKey = profileFilename;
if (userEntity.profileImage) {
userEntity.profileImage = null;
await userEntity.save();
}

await profileImageEntity.save();

userEntity.profileImage = profileImageEntity;
}
}

await userEntity.save();

return {
Expand Down Expand Up @@ -208,7 +243,32 @@ export class UserService {
userEntity.password = '';
userEntity.nickname = googleInfo.name;

// Todo: 프로필 이미지 저장하기
if (googleInfo.picture) {
const urlHash = md5(googleInfo.picture);
const profileFilename = `profile/google_${urlHash}`;

if (
!userEntity.profileImage ||
userEntity.profileImage.imageKey !== profileFilename
) {
const profileImageEntity = new UserProfileImageEntity();
profileImageEntity.imageKey = urlHash;

const profileImageFile = await this.downloadImage(googleInfo.picture);
await this.s3Service.putObject(profileFilename, profileImageFile);

profileImageEntity.imageKey = profileFilename;
if (userEntity.profileImage) {
userEntity.profileImage = null;
await userEntity.save();
}

await profileImageEntity.save();

userEntity.profileImage = profileImageEntity;
}
}

await userEntity.save();

return {
Expand Down

0 comments on commit d59a657

Please sign in to comment.