add file fetching data to the ws server
This commit is contained in:
29
backend/server/dist/getSandboxFiles.js
vendored
29
backend/server/dist/getSandboxFiles.js
vendored
@ -13,10 +13,13 @@ const getSandboxFiles = (id) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
const sandboxRes = yield fetch(`https://storage.ishaan1013.workers.dev/api?sandboxId=${id}`);
|
||||
const sandboxData = yield sandboxRes.json();
|
||||
const paths = sandboxData.objects.map((obj) => obj.key);
|
||||
return processFiles(paths, id);
|
||||
const processedFiles = yield processFiles(paths, id);
|
||||
// console.log("processedFiles.fileData:", processedFiles.fileData)
|
||||
return processedFiles;
|
||||
});
|
||||
const processFiles = (paths, id) => {
|
||||
const processFiles = (paths, id) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
const root = { id: "/", type: "folder", name: "/", children: [] };
|
||||
const fileData = [];
|
||||
paths.forEach((path) => {
|
||||
const allParts = path.split("/");
|
||||
if (allParts[1] !== id) {
|
||||
@ -38,6 +41,7 @@ const processFiles = (paths, id) => {
|
||||
if (isFile) {
|
||||
const file = { id: path, type: "file", name: part };
|
||||
current.children.push(file);
|
||||
fileData.push({ id: path, data: "" });
|
||||
}
|
||||
else {
|
||||
const folder = {
|
||||
@ -52,6 +56,23 @@ const processFiles = (paths, id) => {
|
||||
}
|
||||
}
|
||||
});
|
||||
return root.children;
|
||||
};
|
||||
yield Promise.all(fileData.map((file) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
const data = yield fetchFileContent(file.id);
|
||||
file.data = data;
|
||||
})));
|
||||
return {
|
||||
files: root.children,
|
||||
fileData,
|
||||
};
|
||||
});
|
||||
const fetchFileContent = (fileId) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
try {
|
||||
const fileRes = yield fetch(`https://storage.ishaan1013.workers.dev/api?fileId=${fileId}`);
|
||||
return yield fileRes.text();
|
||||
}
|
||||
catch (error) {
|
||||
console.error("ERROR fetching file:", error);
|
||||
return "";
|
||||
}
|
||||
});
|
||||
exports.default = getSandboxFiles;
|
||||
|
10
backend/server/dist/index.js
vendored
10
backend/server/dist/index.js
vendored
@ -31,7 +31,6 @@ const io = new socket_io_1.Server(httpServer, {
|
||||
const handshakeSchema = zod_1.z.object({
|
||||
userId: zod_1.z.string(),
|
||||
sandboxId: zod_1.z.string(),
|
||||
type: zod_1.z.enum(["node", "react"]),
|
||||
EIO: zod_1.z.string(),
|
||||
transport: zod_1.z.string(),
|
||||
});
|
||||
@ -44,7 +43,7 @@ io.use((socket, next) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
next(new Error("Invalid request."));
|
||||
return;
|
||||
}
|
||||
const { sandboxId, userId, type } = parseQuery.data;
|
||||
const { sandboxId, userId } = parseQuery.data;
|
||||
const dbUser = yield fetch(`http://localhost:8787/api/user?id=${userId}`);
|
||||
const dbUserJSON = (yield dbUser.json());
|
||||
console.log("dbUserJSON:", dbUserJSON);
|
||||
@ -61,7 +60,6 @@ io.use((socket, next) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
}
|
||||
socket.data = {
|
||||
id: sandboxId,
|
||||
type,
|
||||
userId,
|
||||
};
|
||||
next();
|
||||
@ -69,11 +67,7 @@ io.use((socket, next) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
io.on("connection", (socket) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
const data = socket.data;
|
||||
const sandboxFiles = yield (0, getSandboxFiles_1.default)(data.id);
|
||||
// fetch all file data TODO
|
||||
// socket.emit("loaded", {
|
||||
// rootContent: await fetchDir("/workspace", "")
|
||||
// });
|
||||
// initHandlers(socket, replId);
|
||||
socket.emit("loaded", sandboxFiles.files);
|
||||
}));
|
||||
httpServer.listen(port, () => {
|
||||
console.log(`Server running on port ${port}`);
|
||||
|
Reference in New Issue
Block a user