|
| 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 | +} |
0 commit comments