์ค๋ ์๋ฃํ ๋ชฉ๋ก
1. uuid ์๋ฌ ํด๊ฒฐ
2. ํ์ด์ด๋ฒ ์ด์ค ์คํ ๋ฆฌ์ง์ ์ฌ์ง ์ ์ฅ ๋ฐ ๋ถ๋ฌ์ค๊ธฐ
3. ํธ์ ์ญ์ ์ ์คํ ๋ฆฌ์ง์์ ์ญ์ ํ๊ธฐ
๋จผ์ uuid ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ์ด์ ๋,
์คํ ๋ฆฌ์ง๋ ํ์ด์ด์คํ ์ด์ ๋ค๋ฅด๊ฒ ๋ฌธ์์ ์์ด๋๋ฅผ ์๋์ผ๋ก ๋ง๋ค์ด์ฃผ์ง ์๋๋ค.
๊ณ ์ ์๋ณ์๋ฅผ ๋ง๋ค์ด ์คํ ๋ฆฌ์ง์ ์ ์ฅํ ๋ฐ์ดํฐ์ ์ง์ ์ง์ด์ค์ผ ํ๋๋ฐ, ์ฐ๋ฆฌ๊ฐ ๋ง๋ค ๊ณ ์ ์๋ณ์๋ฅผ
๋ง๋ค์ด์ฃผ๋ ๊ฒ์ uuid ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํ๋ ค๊ณ ํ๋ค.
uuid๋ ๋ฒ์ ์ ๋ฐ๋ผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ๋ค๋ฅธ๋ฐ,
Create a UUID (ES6 module syntax)
import { v4 as uuidv4 } from 'uuid';
uuidv4(); // โจ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
(uuid ๊ณต์ ๋ฌธ์ https://github.com/uuidjs/uuid)
๊ณต์ ๋ฌธ์์ ๋ฐ๋ฅด๋ฉด ์ ๋ฐ ์์ผ๋ก ์ฌ์ฉํด์ผํ๋ค๊ณ ๋์ด์์๋ค.
๊ทธ๋์ ๋๊ฐ์ด import๋ฅผ ํด์คฌ์ง๋ง, ์๋ฌ๊ฐ ๋ฐ์ํ์๊ณ ์ฒ์์๋ uuid ์ค์น๊ฐ ์๋ชป๋๋ค๊ณ ํ๋จํ์ฌ
์ด๊ฒ ์ ๊ฒ ๊ฑด๋ค๋ค๊ฐ ์์ ๋
ธ๋๋ชจ๋ ์ ์ฒด๊ฐ ์์ผ๋ฒ๋ ธ๋ค..ใ
ใ
๊ฒฐ๋ก ์ ์ผ๋ก ๋ด๊ฐ ๋ฐ๊ฒฌํ ๋ฐฉ๋ฒ์ ์์ ํ ๋ฌ๋๋ค..
import {v4} from 'uuid';
v4();
์์ง๊น์ง ์ด๊ฒ ์ ์ด๋ ๊ฒ ์๋๋์ง๋ ์ ํํ ๋ชจ๋ฅด๊ฒ ์ง๋ง, ๊ณต์ ๋ฌธ์๋ฅผ ๋๋ฌ๋ด๋ ๋ต์ด ์ ๋์์
ํผ์ ์ด๊ฒ์ ๊ฒ ๊ฑด๋ค์ด๋ค ๋ณด๋๊น ์๋์ด ๋๋ค...
๋ญ.. ์ผ๋จ ๋์ผ๋๊น ๋ค์์ ์ฌ์ ๊ฐ ์์ ๋ uuid์ ๋ํด ๋ ์์ธํ๊ฒ ๊ณต๋ถํด๋ด์ผ๊ฒ ๋ค.
์คํ ๋ฆฌ์ง๋ ์ฝ๊ฒ ์ค๋ช
ํ๋ฉด ์ปดํจํฐ ํ๋๋์คํฌ๋ ๋น์ทํ๋ค.
ํ๋๋์คํฌ์ ํด๋๋ฅผ ๋ง๋ค๊ณ ํ์ผ์ ์ ์ฅํ๊ณ ์ฌ์ฉํ๋ ๋ฉ์ปค๋์ฆ์ด๋ค.
๋ง์ฐฌ๊ฐ์ง๋ก ์คํ ๋ฆฌ์ง์ ์ฌ์ฉ์์์ด๋(ํด๋)๋ฅผ ๋ง๋ค๊ณ uuid(ํ์ผ ์ด๋ฆ)์ ์ ํด ์ ์ฅํ๋ ๊ฒ์ด๋ค.
๋จผ์ ์คํ ๋ฆฌ์ง๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ํ์ด์ด๋ฒ ์ด์ค ํ์ผ์์ ์คํ ๋ฆฌ์ง๋ฅผ import ํด์ผํ๋ค.
import {getStorage} from "firebase/storage";
export const storageService = getStorage();
์คํ ๋ฆฌ์ง๋ฅผ import ํด์ฃผ๊ณ ๋ค๋ฅธ ํ์ผ์์ ์ฌ์ฉํ ์ ์๊ฒ export ์ฒ๋ฆฌํด์คฌ๋ค.
const onSubmit = async (e) => {
try{
e.preventDefault();
const docRef = await addDoc(collection(dbService, "nweets"),
{
nweet,
createdAt: Date.now(),
creatorId : userObj.uid, // db์ ์ ์ ์์ด๋ ์ถ๊ฐ
}); // ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฑ
console.log("Document Written with Id:", docRef.id);
}catch(error){
console.log("Error adding document", error)
}
setNweet("");
};
// ์๋ต
import { storageService } from 'fbase';
const onSubmit = async (e) => {
e.preventDefault();
let attachmentUrl =""
if(attachment !== ""){
const attachmentRef = ref(storageService, `${userObj.uid}/${v4()}`);
const response = await uploadString(attachmentRef, attachment, "data_url");
console.log(getDownloadURL(response.ref));
attachmentUrl = await getDownloadURL(response.ref);
}
const nweetObj = {
nweet,
createdAt: Date.now(),
creatorId : userObj.uid, // db์ ์ ์ ์์ด๋ ์ถ๊ฐ
attachmentUrl
}
await addDoc(collection(dbService, "nweets"),(nweetObj));
setNweet("");
setAttachment("");
};
์คํ ๋ฆฌ์ง์์ ์ฌ์ง์ ์ ์ฅํ๊ณ ๋ถ๋ฌ์ค๊ธฐ ์ํด์๋ ๊ธฐ์กด์ ์ฝ๋์์ ์์ ์ด ํ์ํ๋ค.
const attachmentRef = ref(storageService, `${userObj.uid}/${v4()}`);
์์ ์๋ ์ฝ๋๋ ์คํ ๋ฆฌ์ง, ๋ ํผ๋ฐ์ค๋ฅผ ํธ์ถํ๊ณ ๋์ ์ฌ์ฉ์์ ์์ด๋๋ฅผ ํด๋์ด๋ฆ์ผ๋ก ์ค์ ํ ํ ํ์ผ ์ด๋ฆ์ v4(uuid)๋ก ์ฒ๋ฆฌํ๋ค.
์ด์ ํ์ผ ํด๋๋ฅผ ๋ง๋ค์ด์คฌ์ผ๋ ์ ์ฅ๊ธฐ๋ฅ์ ๊ตฌํํด์ผํ๋ค.
const response = await uploadString(attachmentRef, attachment, "data_url");
์ ์ฅ๊ธฐ๋ฅ์ ํ๊ธฐ ์ํด์๋ uploadString ํจ์๋ฅผ ์ด์ฉํ๋ค.
์ด์ ๋ฒ์ ์์๋ putString ํจ์๋ฅผ ์ฌ์ฉํ์ง๋ง ๋ฒ์ ์ด ์
๊ทธ๋ ์ด๋ ๋๋ฉด์ ์ฌ์ฉํ๋ ํจ์๊ฐ ๋ฐ๋์๋ค.
(์ฌ์ฉ๋ฌธ์ https://modularfirebase.web.app/common-use-cases/storage/#upload-a-string)
์ฌ๊ธฐ์ attachmentRef๋ ์๊น ๋ง๋ ํ์ผํด๋์ด๋ค. ์ฌ์งํ์ผ์ URL์ attachment์ ์ ์ฅ๋์ด์๊ธฐ ๋๋ฌธ์, ํ์ผํด๋์ ํ์ผURL์ ๋ฃ์ด์ค ํ์ผ์ ์ฌ์ง์ ์ ์ฅํ๋ค.
๋ค์์ ์ฌ์ง์ ๋ถ๋ฌ์ค๊ธฐ๋ค
์ฌ์ง์ ๋ถ๋ฌ์ค๊ธฐ ์ํด์ response.ref.getDownloadURL()ํจ์๋ฅผ ์ฌ์ฉํด์ผํ๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์๊น ์ฌ์ง ์ ์ฅ ์ฝ๋๋ฅผ response ๋ณ์์ ๋ฃ์ด์ฃผ๊ณ
attachmentUrl = await getDownloadURL(response.ref);
์ด๋ ๊ฒ ์ฌ์ฉํ๋ค. ๊ทธ๋ฆฌ๊ณ attachmentUrl์ ์ ์ฅํด์คฌ๋ค.
(์ฌ์ฉ๋ฒ)
const nweetObj = {
nweet,
createdAt: Date.now(),
creatorId : userObj.uid, // db์ ์ ์ ์์ด๋ ์ถ๊ฐ
attachmentUrl
}
๊ทธ๋ฆฌ๊ณ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํ๊ธฐ์ํด attachmentUrl์ ์ถ๊ฐํด์คฌ๋ค.
์ด์ ๋ถ๋ฌ์จ ์ฌ์งํ์ผ์ ํ๋ฉด์ ๋ํ๋ด์ฃผ๊ธฐ ์ํด img์๋ฆฌ๋จผํธ ์์ฑ์ nweetObj.attachmentUrl๋ฅผ ์ถ๊ฐํด์คฌ๋ค.
<>
<h4>{nweetObj.nweet}</h4>
{nweetObj.attachmentUrl &&(
<img src ={nweetObj.attachmentUrl} alt='~' width="50px" height="50px" />
)}
{isOwner && (
<>
<button onClick={onDeleteClick}>Delete tweet</button>
<button onClick={toggleEditing}>Edit Nweet</button>
</>
)}
</>
์ด์ ์ ์ฒด์ ์ธ ์ฝ๋๋ฅผ ๋ค๋ฌ์ด์ค์ผํ๋ค.
๋จผ์
if(attachment !== ""){
const attachmentRef = ref(storageService, `${userObj.uid}/${v4()}`);
const response = await uploadString(attachmentRef, attachment, "data_url");
console.log(getDownloadURL(response.ref));
const attachmentUrl = await getDownloadURL(response.ref);
}
const nweetObj = {
nweet,
createdAt: Date.now(),
creatorId : userObj.uid, // db์ ์ ์ ์์ด๋ ์ถ๊ฐ
attachmentUrl
}
๋ค์๊ณผ ๊ฐ์ ์ํฉ์์ attachmentUrl์ if๋ฌธ ์์์ ์ ์ํ๊ฒ ๋๋ฉด if๋ฌธ์์์๋ง ์ฐธ์กฐ๋๋ ๋ณ์๊ฐ ๋๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ let์ ์ด์ฉํ์ฌ if๋ฌธ ๋ฐ์์ ๋ฐ๋ก ์ ์ํด์คฌ๋ค.
์์ ํ
e.preventDefault();
let attachmentUrl =""
if(attachment !== ""){
const attachmentRef = ref(storageService, `${userObj.uid}/${v4()}`);
const response = await uploadString(attachmentRef, attachment, "data_url");
console.log(getDownloadURL(response.ref));
attachmentUrl = await getDownloadURL(response.ref);
}
if๋ฌธ ์ ์ ์ ์๋ฅผ ํด์ค ์ ์ฒด์ ์ผ๋ก ์ฌ์ฉํ ์ ์๊ฒ ์ฝ๋๋ฅผ ๋ฐ๊ฟ์คฌ๋ค.
ํ์ฌ ํธ์ ๊ธ์ ํ์ด์ด์คํ ์ด์ ์๊ธฐ ๋๋ฌธ์ ์ญ์ ํด๋ ์คํ ๋ฆฌ์ง์ ์ ์ฅ๋ ํ์ผ์ ๊ทธ๋๋ก ์๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์ฐ๋ฆฌ๋ ํ์ด์ด์คํ ์ด์์ ์ฌ์ฉํ๋ deleteObject ํจ์๋ฅผ ์ด์ฉํ๋ค.
const NweetTextRef = doc(dbService,"nweets", `${nweetObj.id}`);
const NweetImageRef = ref(storageService, nweetObj.attachmentUrl);
// ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ํธ์์ ์์ด๋๋ฅผ ์ฐพ๊ธฐ ์ํ ์ฝ๋.
const onDeleteClick = async () => { // ํธ์ ์ญ์ ํจ์
const ok = window.confirm("Are you sure you want to delete this tweet?");
if(ok){
await deleteDoc(NweetTextRef); // ํธ์ ๊ธ ์ญ์ ๊ธฐ๋ฅ
await deleteObject(NweetImageRef); // ํธ์ ์ด๋ฏธ์ง ์ญ์
};
๋จผ์ ์ด๋ฏธ์ง์ url์ ์ฐพ์์ผํ๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์ฐ๋ฆฌ๊ฐ ์๊น ์ด๋ฏธ์ง url์ ์ ์ฅํด๋จ๋ attachmentUrl์ ์ด์ฉํ๋ค.
ํ์ด์ด๋ฒ ์ด์ค์ ref() ํจ์๋ฅผ ์ด์ฉํ์ฌ ์ด๋ฏธ์งํ์ผ์ ๊ฒฝ๋ก๋ฅผ ์ฝ์ด NweetImageRef์ ์ ์ฅํด์คฌ๊ณ ,
deleteObject ํจ์์ ๋ฃ์ด์คฌ๋ค.
์๊ฐ๋ณด๋ค ์ค๋ ํ ๊ธฐ๋ฅ๋ค์ด ์ค๋ ๊ฑธ๋ ธ๋ค.. ์ด๋ฒ ๋ฌ ์์ ๋ง๋๋๊ฒ ๋ชฉํ์์ง๋ง ํ๊ต ๊ณผ์ ๋ ์์
๋ฑ ์ด๊ฒ์ ๊ฒ์ผ๋ก ๋ง์ด ๋ฐ๋ฆฐ ๊ฒ ๊ฐ๋ค!
๋นจ๋ฆฌ ๋ถ์ง๋ฐํ ํด์ ๋๋ด์ผ๊ฒ ๋ค.