์๋ํ๋ ์ค ๊ธ์ ์ด๋ฏธ์ง ์ถ๊ฐํ๊ธฐ (์ปค์คํ ํ๋ ํ์ฉ)
๐ ์๋ํ๋ ์ค REST API๋ฅผ ์ด์ฉํด์ ๋๋ง์ ์ฌ์ดํธ ๋ง๋ค๊ธฐ - 5ํธ
์ด๋ฏธ์ง๋ฅผ ์ ๋ก๋ํ๊ณ ๊ฒ์๋ฌผ์ ์ฐ๊ฒฐํ๊ธฐ (์ปค์คํ ํ๋ ํ์ฉ)
์ด์ ๋ค์ ๊ธ์ฐ๊ธฐ๋ก ๋์์ ์ฌ์ฉ์๋ค์ด ๊ธ์ ์์ฑํ ๋ ์ด๋ฏธ์ง๋ฅผ ํจ๊ป ์
๋ก๋ํ ์ ์๋๋ก ๊ตฌํํ ๊ฒ์
๋๋ค.
์งง์ ํ
์คํธ ์ค์ฌ์ ํธ์ํฐ ์คํ์ผ ์น ์ฑ์ด์ง๋ง, ์ด๋ฏธ์ง๊ฐ ์ถ๊ฐ๋๋ฉด ์ฝํ
์ธ ์ ์ ๋ฌ๋ ฅ๊ณผ ๋ชฐ์
๋๊ฐ ํ์คํ ๋ฌ๋ผ์ง๋๋ค. ๊ทธ๋์ ๊ฐ๋จํ๋ฉด์๋ ์ง๊ด์ ์ธ ์ด๋ฏธ์ง ์
๋ก๋ ๊ธฐ๋ฅ์ ๊ตฌํํด๋ดค์ต๋๋ค.
๋ชฉํ
- ์ด๋ฏธ์ง ์ ํ ๋ฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
- ์ต๋ 8์ฅ๊น์ง ์ ๋ก๋ ์ ํ
- ์๋ํ๋ ์ค Media API๋ฅผ ํตํ ์ด๋ฏธ์ง ๋ฑ๋ก
- image_urls๋ผ๋ ์ปค์คํ ๋ฉํ ํ๋์ ์ด๋ฏธ์ง URL ์ ์ฅ
์ ์ปค์คํ ๋ฉํ ํ๋๊ฐ ํ์ํ๊ฐ?
์๋ํ๋ ์ค์์ ๋ํ ์ด๋ฏธ์ง(featured_media)๋ ํ๋๋ง ์ค์ ํ ์ ์๊ณ , ๋ณธ๋ฌธ(content)์ ์ด๋ฏธ์ง๋ฅผ ์ฝ์ ํ๋ฉด ํธ์ง์ด ๋ถํธํ๋ฉฐ ๊ตฌ์กฐ์ ์ผ๋ก ๊น๋ํ๊ฒ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๊ธฐ ์ด๋ ต์ต๋๋ค.
์ ๋ ์ด๋ฏธ์ง URL์ ๋ณ๋๋ก ์ ์ฅํ๊ณ , ํด๋ผ์ด์ธํธ์์ ํด๋น ํ๋๋ฅผ ์ด์ฉํด ์ด๋ฏธ์ง๋ฅผ ๊น๋ํ๊ฒ ๋ ๋๋งํ๋ ๊ตฌ์กฐ๋ฅผ ์ํ์ต๋๋ค.
์๋ฅผ ๋ค์ด, ๋ค์๊ณผ ๊ฐ์ ํํ๋ก ์ด๋ฏธ์ง๋ฅผ ์ ์ฅํ๊ณ ์ฌ์ฉํ๋ ๊ฒ์ด์ฃ :
"meta": {
"image_urls": [
"https://myDomain.com/uploads/image1.jpg",
"https://myDomain.com/uploads/image2.jpg",
"https://myDomain.com/uploads/image3.jpg"
]
}
์ด๋ฏธ์ง URL ํ๋๋ฅผ ์ํ ์ปค์คํ ํ๋ฌ๊ทธ์ธ
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ ๋ image_urls๋ผ๋ ์ปค์คํ ๋ฉํ ํ๋๋ฅผ ๋ฑ๋กํด์ฃผ๋ ์ ์ฉ ํ๋ฌ๊ทธ์ธ์ ๋ฐ๋ก ๋ง๋ค์ด ์ฌ์ฉํฉ๋๋ค.
์ด ํ๋ฌ๊ทธ์ธ์ ์ญํ ์ ๋จ์ํ image_urls๋ผ๋ ๋ฉํ ํ๋๋ฅผ REST API์ ๋ ธ์ถ์ํค๋ ๊ฒ์ ๋๋ค.
๐ ์ด ํ๋ฌ๊ทธ์ธ์ ์๋ํ๋ ์ค ๊ด๋ฆฌ์ > ํ๋ฌ๊ทธ์ธ > ์๋ก ์ถ๊ฐ > ์ ๋ก๋ ๋ฅผ ํตํด ์ค์นํ ๋ค ํ์ฑํํ๋ฉด ๋ฐ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค.
๐ HTML ๊ตฌ์กฐ
<textarea id="postContent" placeholder="๋ฌด์จ ์ผ์ด ์ผ์ด๋๊ณ ์๋์?"></textarea>
<input type="file" id="imageUpload" multiple style="display: none" />
<button id="customUploadBtn" type="button">์ด๋ฏธ์ง ์ ํ</button>
<div id="preview"></div>
<p id="fileStatus">0์ฅ์ ์ด๋ฏธ์ง ์ ํ๋จ</p>
<button id="postButton" onclick="submitPost()">๊ฒ์ํ๊ธฐ</button>
<p id="loading" class="loading" style="display: none">
์ด๋ฏธ์ง ์
๋ก๋ ์ค...<span id="progress">0/0</span>
</p>
1. input[type="file"]์ ์จ๊ฒจ๋๊ณ , ๋ฒํผ์ผ๋ก ํด๋ฆญ ์ ๋
2. ๋ฏธ๋ฆฌ๋ณด๊ธฐ(preview) ์์ญ์ ์ด๋ฏธ์ง์ ์ญ์ ๋ฒํผ ํ์
3. ์ ๋ก๋ ์งํ ์ํ๋ progress๋ก ํ์
๐ง ์ด๋ฏธ์ง ์ ํ & ๋ฏธ๋ฆฌ๋ณด๊ธฐ ํจ์:
์ด๋ฏธ์ง๋ฅผ ์ ํํ๋ฉด ๋ฏธ๋ฆฌ๋ณด๊ธฐ๋ก ๋ณด์ฌ์ฃผ๊ณ , ๊ฐ๋ณ ์ญ์ ๋ ๊ฐ๋ฅํ๊ฒ ์ฒ๋ฆฌํฉ๋๋ค.
let selectedImages = [];
function renderPreview() {
const preview = document.getElementById('preview');
preview.innerHTML = '';
selectedImages.forEach((file, index) => {
const reader = new FileReader();
reader.onload = function (e) {
const img = document.createElement('img');
img.src = e.target.result;
img.style.width = '80px';
img.style.height = '80px';
img.style.objectFit = 'cover';
const removeBtn = document.createElement('button');
removeBtn.innerText = 'โ';
removeBtn.onclick = () => {
selectedImages.splice(index, 1);
renderPreview();
};
const container = document.createElement('div');
container.style.display = 'inline-block';
container.style.position = 'relative';
container.appendChild(img);
container.appendChild(removeBtn);
preview.appendChild(container);
};
reader.readAsDataURL(file);
});
fileStatus.innerText = `${selectedImages.length}์ฅ์ ์ด๋ฏธ์ง ์ ํ๋จ`;
}
๐ค ์ด๋ฏธ์ง ์ ๋ก๋: ์๋ํ๋ ์ค Media API ํ์ฉ
fetch๋ก ์๋ํ๋ ์ค์ Media API์ ์ง์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํฉ๋๋ค.
์ด ๊ณผ์ ์์ JWT ํ ํฐ์ ์ฌ์ฉํด ์ธ์ฆ์ ์ฒ๋ฆฌํ๊ณ , ์
๋ก๋๋ ์ด๋ฏธ์ง์ source_url์ ๋ฐํ๋ฐ์ต๋๋ค.
async function uploadImage(file, index, total) {
const formData = new FormData();
formData.append('file', file);
formData.append('title', file.name);
formData.append('status', 'publish');
document.getElementById('progress').innerText = `${index}/${total}`;
const response = await fetch(mediaApiUrl, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`
},
body: formData
});
if (response.ok) {
const data = await response.json();
return data.source_url;
} else {
alert(`์ด๋ฏธ์ง ${index} ์
๋ก๋ ์คํจ!`);
return null;
}
}
๐ ๊ฒ์๋ฌผ ์์ฑ ์ ์ด๋ฏธ์ง URL ์ ์ฅ:
์ด๋ฏธ์ง ์ ๋ก๋๊ฐ ๋๋๋ฉด ํด๋น URL๋ค์ ๋ฐฐ์ด๋ก ๋ชจ์์ ๊ฒ์๋ฌผ์ ์ ์ฅํ๊ณ REST API์ ์ปค์คํ ๋ฉํ ํ๋๋ก ์ ๋ฌ์ ํฉ๋๋ค.
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
title: `${userSlug}-${getCurrentTimestamp()}`,
content,
status: 'publish',
meta: {
image_urls: JSON.stringify(imageUrls),
},
}),
});
์ค์ ๋ก ๊ตฌํ๋ ๋ถ๋ถ ์ํ