Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:presigned url #81

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/component/admin/EditIntroduction.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useNavigate } from 'react-router-dom';
export default function EditIntroduction({
introduction: initialIntroduction,
instagram: initialInsta,
imgUrl,
imgUrl: initialImgUrl,
leader: initialLeader,
activity: initialActivity,
room: initialRoom,
Expand All @@ -19,6 +19,7 @@ export default function EditIntroduction({
const [cLeader, setcLeader] = useState(initialLeader);
const [cActivity, setcActivity] = useState(initialActivity);
const [cRoom, setcRoom] = useState(initialRoom);
const [img, setimg] = useState(initialImgUrl);

const navigate = useNavigate();
const accessToken = localStorage.getItem('accessToken');
Expand All @@ -30,6 +31,7 @@ export default function EditIntroduction({
setcActivity(initialActivity);
setcRoom(initialRoom);
}, [accessToken]);
console.log('II', initialImgUrl);

const handleIntroductionChange = (e) => setcIntroduction(e.target.value ? e.target.value : initialIntroduction);
const handleInstagramChange = (e) => setInsta(e.target.value ? e.target.value : initialInsta);
Expand All @@ -49,10 +51,11 @@ export default function EditIntroduction({

const patchEditClub = async () => {
try {
console.log('img', img);
const response = await customAxios.patch(
`/v1/admins/change-page`,
{
//imageUrl: imgUrl || "",
imageUrl: img || '',
introduction: cIntroduction ? cIntroduction : initialIntroduction,
instagram: insta ? insta : initialInsta,
activity: cActivity ? cActivity : initialActivity,
Expand Down
109 changes: 77 additions & 32 deletions src/component/admin/EditPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,57 @@ export default function EditPage() {
}, []);
//console.log(imageUrl);

const handleFileChange = (event) => {
const handleFileChange = async (event) => {
const file = event.target.files[0];
setImageFile(file);
setImagePreview(URL.createObjectURL(file));

const extension = file.name.split('.').pop().toUpperCase(); // ν™•μž₯자 μΆ”μΆœ

try {
// presigned URL을 κ°€μ Έμ˜€λŠ” API 호좜
const { data } = await customAxios.post(
'/v1/images/club/logo',

{
imageFileExtension: extension,
},

{
headers: {
Authorization: `Bearer ${accessToken}`,
},
params: {
imageFileExtension: extension,
},
}
);

console.log(data.data);

// 이미지 νŒŒμΌμ„ presigned URL둜 μ—…λ‘œλ“œ

await axios.put(data.data.presignedUrl, file, {
headers: {
'Content-Type': file.type,
},
});
setImageUrl(data.data.imageUrl.split('?')[0]);
alert('이미지 μ—…λ‘œλ“œκ°€ μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.');
} catch (error) {
console.error('이미지 μ—…λ‘œλ“œ μ‹€νŒ¨:', error);
alert('이미지 μ—…λ‘œλ“œμ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.');
}
};

/*

const uploadImage = async () => {
if (!imageFile) return;

try {
// 이전 이미지가 있으면 μ‚­μ œ
/*

if (imageUrl) {
const { data: deleteData } = await customAxios.get('/v1/clubs/images', {
headers: {
Expand All @@ -69,7 +108,7 @@ export default function EditPage() {
console.log('deleteData:', deleteData);
//await customAxios.delete(deleteData.url);
}
*/

console.log(typeof imageFile.name);
const extension = imageFile.name.split('.').pop().toUpperCase(); // ν™•μž₯자 μΆ”μΆœ

Expand Down Expand Up @@ -108,6 +147,7 @@ export default function EditPage() {
}
};

*/
const deleteImage = async () => {
if (!imageUrl) return;

Expand All @@ -134,38 +174,39 @@ export default function EditPage() {
}
};

const handleImageSave = async () => {
try {
const uploadedImageUrl = await uploadImage();
// const handleImageSave = async () => {
// try {
// const uploadedImageUrl = await uploadImage();

if (uploadedImageUrl) {
// 이미지 URL을 ν¬ν•¨ν•œ 동아리 μˆ˜μ • API 호좜
await axios.put(
`/v1/clubs/${clubId}`,
{
imageUrl: uploadedImageUrl,
},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
// if (uploadedImageUrl) {
// // 이미지 URL을 ν¬ν•¨ν•œ 동아리 μˆ˜μ • API 호좜
// await customAxios.put(
// `/v1/clubs/${clubId}`,
// {
// imageUrl: uploadedImageUrl,
// },
// {
// headers: {
// Authorization: `Bearer ${accessToken}`,
// },
// }
// );

alert('동아리 이미지 μˆ˜μ •μ΄ μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.');
}
} catch (error) {
console.error('동아리 이미지 μˆ˜μ • μ‹€νŒ¨:', error);
alert('동아리 이미지 μˆ˜μ •μ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.');
}
};
// alert('동아리 이미지 μˆ˜μ •μ΄ μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.');
// }
// } catch (error) {
// console.error('동아리 이미지 μˆ˜μ • μ‹€νŒ¨:', error);
// alert('동아리 이미지 μˆ˜μ •μ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.');
// }
// };
console.log('url', imageUrl);

return (
<div className={styles.DivMyPage}>
<div className={styles.admin_detail_container}>
<div className={styles.admin_detail_header}>
<br />
{imageUrl && (
{
<>
<div className={styles.logoDiv}>
<img
Expand All @@ -177,16 +218,14 @@ export default function EditPage() {
<button className={styles.logoButton} onClick={deleteImage}>
둜고 μ‚­μ œ
</button>
<button className={styles.logoButton} onClick={uploadImage}>
둜고 μ—…λ‘œλ“œ
</button>

<label className={styles.fileUpload}>
<input type="file" accept=".jpg, .jpeg" onChange={handleFileChange} />
</label>
</div>
</div>
</>
)}
}

<div className={styles.admin_detail_header_container}>
<div className={styles.admin_detail_header_name}>
Expand All @@ -211,7 +250,7 @@ export default function EditPage() {
department={club.department}
division={club.division}
introduction={club.introduction}
imgUrl={club.imageUrl}
imgUrl={imageUrl ? imageUrl : club.imageUrl}
instagram={club.instagram}
activity={clubInfo.activity}
leader={clubInfo.leader}
Expand All @@ -221,3 +260,9 @@ export default function EditPage() {
</div>
);
}

{
/* <button className={styles.logoButton} onClick={uploadImage}>
둜고 μ—…λ‘œλ“œ
</button> */
}
Loading