Skip to content

Commit 4804fa2

Browse files
committed
웹소켓2
1 parent 8aeaf5b commit 4804fa2

3 files changed

Lines changed: 434 additions & 143 deletions

File tree

src/components/codecast/api/files.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// No changes to apply
12
import config from "../../../config";
23

34
const API_BASE = process.env.REACT_APP_API_BASE_URL || config.API_BASE_URL;
@@ -83,3 +84,46 @@ export function inferLanguageFromFilename(filename = "") {
8384
const ext = filename.slice(lastDot + 1).toLowerCase();
8485
return EXTENSION_TO_LANGUAGE[ext] || "plaintext";
8586
}
87+
88+
export async function saveFile({ token, filename, content = "", fileUUID }) {
89+
if (!token) {
90+
throw new Error("인증 토큰이 필요합니다. 다시 로그인해 주세요.");
91+
}
92+
if (!filename) {
93+
throw new Error("파일 이름이 필요합니다.");
94+
}
95+
96+
const fileBlob = new Blob([content], { type: "text/plain" });
97+
const formData = new FormData();
98+
formData.append("file", new File([fileBlob], filename));
99+
100+
let url = `${API_BASE}/api/file/upload`;
101+
if (fileUUID) {
102+
url += `?fileUUID=${encodeURIComponent(fileUUID)}`;
103+
}
104+
105+
const res = await fetch(url, {
106+
method: "POST",
107+
headers: {
108+
Authorization: `Bearer ${token}`,
109+
},
110+
credentials: "include",
111+
body: formData,
112+
});
113+
114+
const text = await res.text().catch(() => "");
115+
if (!res.ok) {
116+
throw new Error(text || `HTTP ${res.status}`);
117+
}
118+
119+
if (!text) {
120+
throw new Error("파일 저장 응답을 해석할 수 없습니다.");
121+
}
122+
123+
const data = JSON.parse(text);
124+
if (!data?.fileUUID) {
125+
throw new Error("파일 저장 결과에 fileUUID가 없습니다.");
126+
}
127+
128+
return data;
129+
}

src/components/codecast/api/sessions.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,24 @@ export async function createSession({ token, roomId, sessionName }) {
2222
}
2323
return text ? JSON.parse(text) : {};
2424
}
25+
26+
export async function updateSessionStatus({ token, sessionId, status }) {
27+
const url = `${config.API_BASE_URL}/api/collab/sessions/${sessionId}/status`;
28+
29+
const res = await fetch(url, {
30+
method: "PATCH",
31+
headers: {
32+
"Content-Type": "application/json",
33+
Authorization: `Bearer ${token}`,
34+
},
35+
credentials: "include",
36+
body: JSON.stringify({ status }),
37+
});
38+
39+
if (!res.ok) {
40+
const text = await res.text().catch(() => "");
41+
const err = new Error(`HTTP ${res.status}${text ? ` - ${text}` : ""}`);
42+
err.status = res.status;
43+
throw err;
44+
}
45+
}

0 commit comments

Comments
 (0)