Skip to content

Commit 28f6302

Browse files
committed
🔀 Merge!
2 parents f432cef + 4f192d3 commit 28f6302

File tree

5 files changed

+58
-19
lines changed

5 files changed

+58
-19
lines changed

src/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ const infoRouter = require('./routes/UserInfo')
7373
app.use('/userInfo', infoRouter)
7474

7575
const fittingRouter = require('./routes/FittingImage')
76+
const clothUploadRouter = require('./routes/UploadClothImage')
7677
app.use('/api', fittingRouter)
78+
app.use('/api', clothUploadRouter)
7779

7880
const clothRouter = require('./routes/Clothes')
7981
app.use('/cloth', clothRouter)

src/routes/ImageUploader.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const ImageUploader = multer({
2626
s3: s3,
2727
bucket: 'bigprogect-bucket',
2828
key: (req, file, callback) => {
29-
const userId = req.query.userId
29+
const userId = req.body.userId || req.query.userId
3030
const uploadDirectory = req.query.directory ?? userId
3131
const extension = path.extname(file.originalname)
3232
if (!allowedExtensions.includes(extension)) {
@@ -39,10 +39,7 @@ const ImageUploader = multer({
3939
if (imageType === 'body') {
4040
imageFilename = `userimage${extension}`
4141
} else if (imageType === 'clothing') {
42-
const fileName = file.originalname
43-
.substr(-10)
44-
.replace(fileExtension, '')
45-
imageFilename = `${fileName}${extension}`
42+
imageFilename = file.originalname
4643
} else {
4744
return callback(new Error('Unknown image type'))
4845
}

src/routes/UploadClothImage.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const ImageUploader = require('./ImageUploader')
44
const Closet = require('../models/Closet')
55

66
router.post(
7-
'/api/cloth-upload',
7+
'/cloth-upload',
88
(req, res, next) => {
99
req.query.type = 'clothing'
1010
ImageUploader.single('clothingImage')(req, res, (error) => {
@@ -16,14 +16,14 @@ router.post(
1616
},
1717
async (req, res) => {
1818
// 유효성 검사: 요청 userId 확인
19-
if (!req.query.userId) {
19+
if (!req.body.userId) {
2020
return res.status(400).json({
2121
success: false,
2222
error: 'Missing userId in request.',
2323
})
2424
}
2525

26-
const userId = req.query.userId
26+
const userId = req.body.userId
2727
// 이미지 업로드 확인
2828
if (!req.file || !req.file.location) {
2929
return res.status(500).json({
@@ -34,11 +34,14 @@ router.post(
3434

3535
// 이미지 URL을 생성
3636
const clothingImageUrl = req.file.location
37+
console.log(userId)
38+
console.log(clothingImageUrl)
39+
console.log(req.body.clothesUrl)
3740
// 새 Closet 생성 및 저장
3841
try {
3942
const newCloset = new Closet({
4043
userId: userId,
41-
clothesUrl: req.query.clothesUrl,
44+
clothesUrl: req.body.clothesUrl,
4245
clothesImageLink: clothingImageUrl,
4346
})
4447

style/src/api/authenticatedAxios.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ authenticatedAxios.interceptors.request.use(
88
if (token) {
99
config.headers['Authorization'] = `Bearer ${token}`
1010
}
11+
12+
if (config.data instanceof FormData) {
13+
config.headers['Content-Type'] = 'multipart/form-data'
14+
}
15+
1116
return config
1217
},
1318
(error) => {

style/src/components/FittingPage/ButtonBar.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import helpImage from '../../images/help.png'
1414
import { API_URL } from '../../api/apiConfig'
1515
import ClassMerger from '../common/ClassNameGenerater'
1616

17-
function ButtonBar({ setErrorCode, showAlert }) {
17+
function ButtonBar({ setErrorCode, showAlert, setIsDefaultPage }) {
1818
// URL 입력 저장을 위한 state 생성
1919
const [inputUrl, setInputUrl] = useState('')
2020
const [isModalOpen, setIsModalOpen] = useState(false)
@@ -33,7 +33,46 @@ function ButtonBar({ setErrorCode, showAlert }) {
3333

3434
// URL 업로드 버튼 클릭 이벤트 핸들러
3535
const handleImgUploadClick = async () => {
36-
// URL 크롤링 하는 함수 작성
36+
if (selected < 0 || selected >= clothes.length) {
37+
// 이미지 선택이 없거나 범위를 벗어난 경우
38+
return
39+
}
40+
41+
const file = await fetch(clothes[selected])
42+
.then((response) => response.blob())
43+
.then(
44+
(blob) =>
45+
new File([blob], `clothes-${Date.now()}.png`, { type: 'image/png' })
46+
)
47+
48+
const user = JSON.parse(localStorage.getItem('user'))
49+
if (!user || !user._id) {
50+
console.error('User ID not found')
51+
return
52+
}
53+
const userId = user._id
54+
const urlWithoutQueryString = inputUrl.split('?')[0]
55+
56+
const formData = new FormData()
57+
formData.append('userId', userId)
58+
formData.append('clothingImage', file)
59+
formData.append('clothesUrl', urlWithoutQueryString)
60+
for (let value of formData.values()) {
61+
console.log(value)
62+
}
63+
try {
64+
const response = await authenticatedAxios.post(
65+
`${API_URL}/api/cloth-upload`,
66+
formData
67+
)
68+
if (response.status === 200) {
69+
console.log('업로드에 성공했습니다!', response.data)
70+
} else {
71+
console.error('이미지 업로드에 실패했습니다.', response)
72+
}
73+
} catch (error) {
74+
console.error('이미지 업로드 중 오류가 발생했습니다.', error)
75+
}
3776
}
3877

3978
// url 업로드 버튼 : url에서 이미지 목록 가져옴
@@ -107,7 +146,7 @@ function ButtonBar({ setErrorCode, showAlert }) {
107146
src={resetImage}
108147
alt="reset"
109148
text="되돌리기"
110-
onClick={getUserImage}
149+
onClick={() => setIsDefaultPage(true)}
111150
/>
112151
<CardButton src={helpImage} alt="help" text="도움말" onClick={() => {}} />
113152
{isModalOpen && (
@@ -163,13 +202,6 @@ function ButtonBar({ setErrorCode, showAlert }) {
163202
)}
164203
</div>
165204
)
166-
167-
async function getUserImage() {
168-
// 데이터베이스에서 유저의 이미지를 불러오는 함수를 호출
169-
//const userImage = await fetchUserImageFromDB();
170-
// 상태를 업데이트하여 이미지가 표시되는 곳에 사용자 이미지를 표시합니다.
171-
//setImageUrl(userImage);
172-
}
173205
}
174206

175207
function ClothesImageElement({ src, index, selected, onClick }) {

0 commit comments

Comments
 (0)