Skip to content

Commit 6cfc17d

Browse files
authored
Merge pull request #105 from DMU-DebugVisual/inseong2
Inseong2
2 parents 4344d8b + 4804fa2 commit 6cfc17d

5 files changed

Lines changed: 769 additions & 258 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// No changes to apply
2+
import config from "../../../config";
3+
4+
const API_BASE = process.env.REACT_APP_API_BASE_URL || config.API_BASE_URL;
5+
6+
async function request(path, { token, method = "GET", headers = {}, responseType = "json" } = {}) {
7+
if (!token) {
8+
throw new Error("인증 토큰이 필요합니다. 다시 로그인해 주세요.");
9+
}
10+
11+
const res = await fetch(`${API_BASE}${path}`, {
12+
method,
13+
headers: {
14+
...(responseType === "json" && method !== "GET" ? { "Content-Type": "application/json" } : {}),
15+
Authorization: `Bearer ${token}`,
16+
...headers,
17+
},
18+
credentials: "include",
19+
});
20+
21+
if (!res.ok) {
22+
const text = await res.text().catch(() => "");
23+
throw new Error(text || `HTTP ${res.status}`);
24+
}
25+
26+
if (responseType === "text") {
27+
return res.text();
28+
}
29+
30+
try {
31+
return await res.json();
32+
} catch (error) {
33+
throw new Error("서버 응답을 해석할 수 없습니다.");
34+
}
35+
}
36+
37+
export async function fetchMyFiles({ token }) {
38+
const data = await request("/api/file/my", { token });
39+
40+
if (!Array.isArray(data)) {
41+
throw new Error("파일 목록 응답이 올바르지 않습니다.");
42+
}
43+
44+
return data;
45+
}
46+
47+
export async function fetchFileContent({ token, fileUUID }) {
48+
if (!fileUUID) {
49+
throw new Error("파일 식별자가 필요합니다.");
50+
}
51+
52+
const content = await request(`/api/file/${encodeURIComponent(fileUUID)}/content`, {
53+
token,
54+
responseType: "text",
55+
});
56+
57+
return content;
58+
}
59+
60+
const EXTENSION_TO_LANGUAGE = {
61+
py: "python",
62+
js: "javascript",
63+
jsx: "javascript",
64+
ts: "typescript",
65+
tsx: "typescript",
66+
java: "java",
67+
c: "c",
68+
cpp: "c++",
69+
cc: "c++",
70+
cs: "csharp",
71+
go: "go",
72+
rb: "ruby",
73+
php: "php",
74+
swift: "swift",
75+
kt: "kotlin",
76+
};
77+
78+
export function inferLanguageFromFilename(filename = "") {
79+
const lastDot = filename.lastIndexOf(".");
80+
if (lastDot === -1 || lastDot === filename.length - 1) {
81+
return "plaintext";
82+
}
83+
84+
const ext = filename.slice(lastDot + 1).toLowerCase();
85+
return EXTENSION_TO_LANGUAGE[ext] || "plaintext";
86+
}
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+
}
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// sessions.js
22
import config from "../../../config";
33

4-
export async function createSession({ token, roomId, fileName, language }) {
4+
export async function createSession({ token, roomId, sessionName }) {
55
const url = `${config.API_BASE_URL}/api/collab/rooms/${roomId}/sessions`;
66

77
const res = await fetch(url, {
@@ -10,7 +10,8 @@ export async function createSession({ token, roomId, fileName, language }) {
1010
"Content-Type": "application/json",
1111
Authorization: `Bearer ${token}`,
1212
},
13-
body: JSON.stringify({ fileName, language }),
13+
credentials: "include",
14+
body: JSON.stringify({ sessionName }),
1415
});
1516

1617
const text = await res.text().catch(() => "");
@@ -20,6 +21,25 @@ export async function createSession({ token, roomId, fileName, language }) {
2021
throw err;
2122
}
2223
return text ? JSON.parse(text) : {};
24+
}
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+
});
2338

24-
// return res.json(); // { sessionId, ... }
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+
}
2545
}

0 commit comments

Comments
 (0)