-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserInfo.js
252 lines (223 loc) · 6.83 KB
/
UserInfo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// 사이즈: backend -> frontend
const express = require('express')
const router = express.Router()
const mongoose = require('mongoose')
const { Schema } = mongoose
const axios = require('axios')
const User = require('../models/User')
const SizeProfile = require('../models/SizeProfile')
const sizeAPI = process.env.AI_SIZE_API_URL
const { ImageUploader } = require('./ImageUploader')
// 사이즈 : backend -> frontend
router.get('/api/size', async (req, res) => {
console.log('get /userInfo/api/size')
const { userId } = req.query
try {
const sizeProfile = await SizeProfile.findOne({ userId: userId })
res.status(201).json({ success: true, message: sizeProfile })
} catch (error) {
console.error('Error finding size :', error)
res.status(500).json({ success: false, error: 'Failed to fetch size.' })
}
})
router.get('/api/info', async (req, res) => {
console.log('get /userInfo/api/info')
const { userId } = req.query
try {
const user = await User.findById({ _id: userId })
if (!user) {
return res
.status(404)
.json({ success: false, message: 'The user was not found.' })
}
res.status(201).json({ success: true, user })
} catch (error) {
console.error('User lookup errors :', error)
res.status(500).json({
success: false,
message: 'An error occurred during user lookup.',
})
}
})
// 사용자 정보 업데이트
router.put('/api/privacy', async (req, res) => {
console.log('put /userInfo/api/privacy')
const user = req.body
const userId = user.userId
const update = {
$set: user,
}
try {
const result = await User.findByIdAndUpdate({ _id: userId }, update)
res.status(201).json({ success: true, code: 'UPDATE_DONE', errno: 0 })
} catch (error) {
res.status(500).json({
success: false,
code: error.codeName,
errno: error.code,
/* message: 'An error occurred while updating the document.', */
})
}
})
// 사이즈 정보 업데이트
router.put('/api/size', async (req, res) => {
console.log('put /userInfo/api/size')
const reqSize = req.body
const userId = reqSize.userId
let isShoulderWidthNull = reqSize.shoulderWidth ? false : true
let isChestWidthNull = reqSize.chestWidth ? false : true
let isLengthNull = reqSize.length ? false : true
let sizeData = {
shoulderWidth: reqSize.shoulderWidth,
chestWidth: reqSize.chestWidth,
length: reqSize.length,
}
let userData = { height: reqSize.height, weight: reqSize.weight }
let sizeResponse
// 하나라도 null인 경우 size api 사용해서 값 가져옴
if (isShoulderWidthNull || isChestWidthNull || isLengthNull) {
sizeResponse = await axios.get(sizeAPI, {
params: userData,
})
// null인 값 sizeRes로 채워줌
if (isShoulderWidthNull)
sizeData.shoulderWidth = sizeResponse.data.size.shoulderWidth
if (isChestWidthNull)
sizeData.chestWidth = sizeResponse.data.size.chestWidth
if (isLengthNull) sizeData.length = sizeResponse.data.size.length
if (sizeResponse.data.error) {
console.error('Error from AI API:', sizeResponse.data.error)
res
.status(500)
.json({ success: false, code: 'SIZE_API_FAILED', errno: -1 })
return
}
}
try {
const userUpdateRes = await User.findByIdAndUpdate(
{ _id: userId },
{
$set: userData,
},
{ new: true }
)
const sizeUpdateRes = await SizeProfile.findOneAndUpdate(
{ userId: userId },
{
$set: sizeData,
},
{ new: true }
)
res.status(201).json({ success: true, code: 'UPDATE_DONE', errno: 0 })
} catch (error) {
res.status(500).json({
success: false,
code: error.codeName,
errno: error.code,
})
}
})
// 사용자 신체 이미지 경로 전송
router.get('/api/userimage', async (req, res) => {
const { userId } = req.query
try {
const user = await User.findById(userId)
if (!user) {
return res
.status(404)
.json({ success: false, message: 'The user was not found.' })
}
res.status(200).json({ success: true, image: user.file })
} catch (error) {
console.error('User lookup errors :', error)
res.status(500).json({
success: false,
message: 'An error occurred during user lookup.',
})
}
})
// kyi : 이거 뭐죠? 제가 만든 건가요? 흠..
router.post(
'/api/userimage/change',
ImageUploader.single('image'),
async (req, res) => {
try {
if (req.file) {
res.status(200).json({ imageUrl: req.file.location })
} else {
res.status(400).send({ error: 'Image upload failed' })
}
} catch (error) {
console.error('Error processing image upload:', error)
res.status(500).send({ error: 'Error processing image upload' })
}
}
)
// router.post(
router.put(
'/api/userimage',
(req, res, next) => {
console.log(req)
req.query.type = 'body'
next()
},
ImageUploader.single('file'),
async (req, res) => {
console.log('Step 2: Handling the response')
// const file = req.body.file
const file = req.file ? req.file.location : undefined
// 파라미터에서 이미지 ID 추출하고 없다면, 저장한 파일 경로에서 userID 추출
const userId =
(req.body.userId || req.query.userId) ?? req.file.key.split('/')[0]
console.log(file)
try {
// 업데이트할 정보
const update = {
$set: { file: file },
}
// 업데이트로 수정
const result = await User.findByIdAndUpdate({ _id: userId }, update)
const port = process.env.PORT
let webAPI
if (process.env.NODE_ENV === 'development') {
webAPI = `http://localhost:${port}`
} else {
webAPI = process.env.WEB_API
}
// 사용자이미지 전처리 human parse
const aiApiParseResponse = await axios.post(
process.env.AI_PARSE_API,
null,
{
params: { ID: userId, image_url: file },
}
)
if (aiApiParseResponse.data.error) {
console.error('Error from AI API:', aiApiParseResponse.data.error)
if (
aiApiParseResponse.data.message ===
'human parse 실행 중 오류가 발생했습니다: cannot write mode RGBA as JPEG'
) {
res
.status(500)
.json({ success: false, code: 'CAN_NOT_WRITE', errno: -1 })
return
}
res
.status(500)
.json({ success: false, code: 'AI_PARSE_ERROR', errno: -1 })
return
}
console.log('Parse done!')
res
.status(200)
.json({ success: true, code: 'IMAGE_CHANGE_DONE', errno: 0, url: file })
} catch (error) {
console.error('Profile Image CHange Error:', error)
res
.status(500)
.json({ success: false, code: 'IMAGE_CHANGE_FAIL', errno: -1 })
}
}
)
module.exports = router