start ws server file fetching logic

This commit is contained in:
Ishaan Dey
2024-04-26 02:10:37 -04:00
parent a49de2294d
commit 4e7d6d1a97
6 changed files with 164 additions and 39 deletions

57
backend/server/dist/getSandboxFiles.js vendored Normal file
View File

@ -0,0 +1,57 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
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 processFiles = (paths, id) => {
const root = { id: "/", type: "folder", name: "/", children: [] };
paths.forEach((path) => {
const allParts = path.split("/");
if (allParts[1] !== id) {
console.log("invalid path!!!!");
return;
}
const parts = allParts.slice(2);
let current = root;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
const isFile = i === parts.length - 1 && part.includes(".");
const existing = current.children.find((child) => child.name === part);
if (existing) {
if (!isFile) {
current = existing;
}
}
else {
if (isFile) {
const file = { id: path, type: "file", name: part };
current.children.push(file);
}
else {
const folder = {
id: path,
type: "folder",
name: part,
children: [],
};
current.children.push(folder);
current = folder;
}
}
}
});
return root.children;
};
exports.default = getSandboxFiles;

View File

@ -17,6 +17,7 @@ const dotenv_1 = __importDefault(require("dotenv"));
const http_1 = require("http");
const socket_io_1 = require("socket.io");
const zod_1 = require("zod");
const getSandboxFiles_1 = __importDefault(require("./getSandboxFiles"));
dotenv_1.default.config();
const app = (0, express_1.default)();
const port = process.env.PORT || 4000;
@ -43,8 +44,8 @@ io.use((socket, next) => __awaiter(void 0, void 0, void 0, function* () {
next(new Error("Invalid request."));
return;
}
const query = parseQuery.data;
const dbUser = yield fetch(`http://localhost:8787/api/user?id=${query.userId}`);
const { sandboxId, userId, type } = parseQuery.data;
const dbUser = yield fetch(`http://localhost:8787/api/user?id=${userId}`);
const dbUserJSON = (yield dbUser.json());
console.log("dbUserJSON:", dbUserJSON);
if (!dbUserJSON) {
@ -52,29 +53,23 @@ io.use((socket, next) => __awaiter(void 0, void 0, void 0, function* () {
next(new Error("DB error."));
return;
}
const sandbox = dbUserJSON.sandbox.find((s) => s.id === query.sandboxId);
const sandbox = dbUserJSON.sandbox.find((s) => s.id === sandboxId);
if (!sandbox) {
console.log("Invalid credentials.");
next(new Error("Invalid credentials."));
return;
}
const data = {
userId: query.userId,
sandboxId: query.sandboxId,
type: query.type,
init: sandbox.init,
socket.data = {
id: sandboxId,
type,
userId,
};
socket.data = data;
next();
}));
io.on("connection", (socket) => __awaiter(void 0, void 0, void 0, function* () {
const data = socket.data;
console.log("init:", data.init);
if (!data.init) {
// const dbUser = await fetch(
// `http://localhost:8787/sandbox/${data.sandboxId}/init`
// )
}
const sandboxFiles = yield (0, getSandboxFiles_1.default)(data.id);
// fetch all file data TODO
// socket.emit("loaded", {
// rootContent: await fetchDir("/workspace", "")
// });