Skip to content

Commit

Permalink
Merge pull request #424 from sparcs-kaist/#173-profile-image-caching
Browse files Browse the repository at this point in the history
#173 프로필 이미지 업데이트 profileImageUpdatedAt 속성으로 관리
  • Loading branch information
kmc7468 authored Jan 2, 2024
2 parents 04568d5 + 2233d94 commit 299052a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 19 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"test": "npm run sample && cross-env TZ='Asia/Seoul' npm run mocha",
"mocha": "cross-env TZ='Asia/Seoul' NODE_ENV=test mocha --recursive --reporter spec --exit",
"serve": "cross-env TZ='Asia/Seoul' NODE_ENV=production node app.js",
"runscript": "cross-env TZ='Asia/Seoul' NODE_ENV=production node",
"lint": "npx eslint --fix .",
"sample": "cd sampleGenerator && npm start && cd .."
},
Expand Down Expand Up @@ -59,6 +60,7 @@
"eslint": "^8.22.0",
"eslint-plugin-mocha": "^10.1.0",
"mocha": "^10.2.0",
"mongodb": "^6.2.0",
"nodemon": "^3.0.1",
"supertest": "^6.2.4"
}
Expand Down
57 changes: 41 additions & 16 deletions pnpm-lock.yaml

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

36 changes: 36 additions & 0 deletions scripts/profileImageUrlUpdater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Issue #173을 해결하기 위한 DB 마이그레이션 스크립트입니다.
// https://github.com/sparcs-kaist/taxi-back/issues/173

const { MongoClient } = require("mongodb");
const { mongo: mongoUrl, aws: awsEnv } = require("../loadenv");

const time = Date.now();

const client = new MongoClient(mongoUrl);
const db = client.db("taxi");
const users = db.collection("users");

async function run() {
try {
for await (const doc of users.find()) {
// 이미 변환이 완료된 경우에는 Pass합니다.
if (doc.profileImageUrl.startsWith(awsEnv.s3Url)) continue;

await users.findOneAndUpdate(
{ _id: doc._id },
{
$set: {
profileImageUrl: `${awsEnv.s3Url}/profile-img/${doc.profileImageUrl}?token=${time}`,
},
}
);
}
} catch (err) {
console.log(err);
} finally {
await client.close();
}
}
run().then(() => {
console.log("Done!");
});
3 changes: 2 additions & 1 deletion src/modules/modifyProfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const crypto = require("crypto");
const aws = require("./stores/aws");

const nouns = [
"재료역학",
Expand Down Expand Up @@ -81,7 +82,7 @@ const generateNickname = (id) => {
// 기존 프로필 사진의 URI 중 하나를 무작위로 선택해 반환합니다.
const generateProfileImageUrl = () => {
const ridx = crypto.randomInt(defaultProfile.length);
return `default/${defaultProfile[ridx]}`;
return aws.getS3Url(`/profile-img/default/${defaultProfile[ridx]}`);
};

// 사용자의 이름과 성을 받아, 한글인지 영어인지에 따라 전체 이름을 반환합니다.
Expand Down
4 changes: 2 additions & 2 deletions src/services/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const editProfileImgDoneHandler = async (req, res) => {
}
const userAfter = await userModel.findOneAndUpdate(
{ id: req.userId },
{ profileImageUrl: user._id },
{ profileImageUrl: aws.getS3Url(`/${key}?token=${req.timestamp}`) },
{ new: true }
);
if (!userAfter) {
Expand All @@ -153,7 +153,7 @@ const editProfileImgDoneHandler = async (req, res) => {
}
res.json({
result: true,
profileImageUrl: userAfter._id,
profileImageUrl: userAfter.profileImageUrl,
});
});
} catch (e) {
Expand Down

0 comments on commit 299052a

Please sign in to comment.