๐Ÿ™†๐Ÿปโ€โ™€๏ธ ๊ฐœ๋ฐœ์ž ์†Œํฌ์”จ์˜ ์ผ์ผ ๐Ÿ™†๐Ÿปโ€โ™€๏ธ

์–ด๋–กํ•˜์ง€...โ—

ํ”„๋ก ํŠธ๋Š” A์„œ๋ฒ„์™€ ํŒจํ‚ค์ง€ํ•˜์—ฌ B์„œ๋ฒ„์˜ ์‚ฌ์ดํŠธ์— ๋ถ™๊ธฐ๋กœ ํ•จ.

์ฃผ์ฐจ์žฅ ์นด๋ฉ”๋ผ ๋ชฉ๋ก ์Šค๋ƒ…์ƒท ๊ตฌํ˜„.
์นด๋ฉ”๋ผ ๋ชฉ๋ก์€ ๊ฐ™์€ A์„œ๋ฒ„์—์„œ ์Šค๋ƒ…์ƒท์€ B์„œ๋ฒ„์—์„œ ์š”์ฒญํ•ด์•ผ ํ–ˆ๋‹ค.

์Šค๋ƒ…์ƒท ์š”์ฒญ์€ url์— A์„œ๋ฒ„๊ฐ€ ์ค€ ์นด๋ฉ”๋ผ ์•„์ด๋””์™€ ์”จ๋ฆฌ์–ผ ๋„˜๋ฒ„๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ–ˆ๊ณ  ํ—ค๋”์— ์ธ์ฆ ํ† ํฐ์„ ๋‹ด์•„์•ผ ํ–ˆ๋‹ค.
์ธ์ฆ ํ† ํฐ ๋„ฃ๋Š” ๊ฒƒ์€ B์„œ๋ฒ„ ์‚ฌ์ดํŠธ์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ํ‚คํด๋ก(keycloak)๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋Š” blobํ˜•ํƒœ๋กœ ์˜ค๋Š”๋ฐ ์ด๊ฒƒ์„ url๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๊ฐ ์นด๋ฉ”๋ผ ๋ฆฌ์ŠคํŠธ img src์— ์ง‘์–ด ๋„ฃ์–ด์•ผ ํ–ˆ๋‹ค.



์ž๊ดด๊ฐ์˜ ์‹œ์ž‘...๐Ÿคฆ๐Ÿปโ€โ™€๏ธ

์ฒ˜์Œ์—๋Š” img src์— ํ•ด๋‹น url์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋„ฃ์—ˆ๋‹ค.

const getCameraSnapshot = async id => {
  const reader = new FileReader();
  const time = Date.now();
  
  await commonGetRequest(`/media/${id}/refresh=${time}`, keycloak.token).then(res => {
   reader.readAsDataURL(res);
    
   reader.onload = e => {
   return e.currentTarget["result"]
   };
  });
}
{cameraList.map(list => {
  const v = list.properties;
  return (
    <div className="img">
      <img src={getCameraSnapshot(`${v.id}/${v.serial_num}`)} alt="์Šค๋ƒ…์ƒท" /> 
    </div>
  )
}

๊ทธ๋Ÿฌ๋‚˜ src์—๋Š” ์›ํ•˜๋Š” ๊ฐ’์ด ์•„๋‹Œ promise ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜...
์ €๋Š” Fulfilled๋œ ๊ฒฐ๊ณผ๊ฐ’์ด ํ•„์š”ํ•˜๋‹ค๊ตฌ์š”...!

๊ทธ๋•Œ๋ถ€ํ„ฐ ๊ตฌ๊ธ€๋ง ์‹œ์ž‘...โ˜†
ํ–ˆ์ง€๋งŒ ์›ํ•˜๋Š” ๋‹ต๋ณ€์„ ์–ป์ง€ ๋ชป ํ–ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‹ค "img src์— ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ๋‹ด์œผ๋ฉด ์•ˆ ๋œ๋‹ค" ๋Š”
๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๋ถ„์˜ ์กฐ์–ธ์„ ์–ป์—ˆ๋‹ค.(๋น› ๊ฐ™์€ ์กด์žฌ)

ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ๋„์ „...!



์–ด๋–กํ•˜์ง€...โœจ

์นด๋ฉ”๋ผ์™€ ์Šค๋ƒ…์ƒท์„ ๋งค์นญํ•˜๊ณ  ๋งˆ์šฐ์Šค ์˜ค๋ฒ„ ์‹œ์— ๊ฐฑ์‹ ๋„ ํ•„์š”ํ–ˆ๊ธฐ์— ์ž๋ฃŒ๊ตฌ์กฐ MAP์„ ํ™œ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.
(key, value ์กฐํ•ฉ, ์ค‘๋ณต ํ—ˆ์šฉ ์•ˆ ํ•จ)

์šฐ์„  ํ•„์š”ํ•œ ๋ณ€์ˆ˜ ์„ ์–ธ...

// ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ ์œ„
const snapshotMap = new Map();

// ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ ์•ˆ
const [snapshotList, setSnapshotList] = useState([]);

id๋ฅผ key๋กœ id, src๋ฅผ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด value๋กœ setํ•œ ๋’ค,
snapshotList์— ๋‹ด๋Š”๋‹ค.

const getCameraSnapshot = async id => {
  const reader = new FileReader();
  const time = Date.now();
    await commonGetTms(`/media/${id}/refresh=${time}`, keycloak.token).then(res => {
    reader.readAsDataURL(res);

    reader.onload = e => {
      const v = e.currentTarget["result"];
          snapshotMap.set(id, { id, src: v });
          setSnapshotList([...snapshotMap.values()]);
    };
  });
};

์ด์ œ ์ฒ˜์Œ ์นด๋ฉ”๋ผ ๋ชฉ๋ก์„ ์š”์ฒญํ•  ๋•Œ, forEach๋ฌธ์„ ๋Œ๋ฉด์„œ ์œ„์˜ ์Šค๋ƒ…์ƒท ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

const getCamereList = async () => {
  await commonGetRequest(`์นด๋ฉ”๋ผ ๋ชฉ๋ก ์š”์ฒญ`).then(res => {
    const result = res.results.features;
    result.forEach(list => {
      const v = list.properties;
      getCameraSnapshot(`${v.id}/${v.serial_num}`);
      });
    });
};

img๋ถ€๋ถ„์— snapshotList๋กœ map()์„ ๋ˆ๋‹ค.
snapshotList์˜ id์™€ ์นด๋ฉ”๋ผ ๋ฆฌ์ŠคํŠธ์˜ id๊ฐ€ ๊ฐ™์œผ๋ฉด ํ•ด๋‹น img src๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
๋งˆ์šฐ์Šค ์˜ค๋ฒ„์‹œ์—๋„ ์Šค๋ƒ…์ƒท ์š”์ฒญ ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

{cameraList.map(list => {
  const v = list.properties;
  return (
    <div className="img">
      {snapshotList.map(
        _list =>
         _list.id === `${v.id}/${v.serial_num}` && (
         <img key={_list.id}
              src={_list.src}
              onMouseOver={() => newSnapshot(_list.id)}
              alt="์Šค๋ƒ…์ƒท"
          />
         ),
       )}
    </div>
  )
}

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ