file saving

This commit is contained in:
Ishaan Dey
2024-04-27 19:12:25 -04:00
parent d6769f3407
commit c4e1a894c3
6 changed files with 62 additions and 28 deletions

View File

@ -70,21 +70,23 @@ io.on("connection", (socket) => __awaiter(void 0, void 0, void 0, function* () {
const file = sandboxFiles.fileData.find((f) => f.id === fileId);
if (!file)
return;
console.log("file " + file.id + ": ", file.data);
callback(file.data);
});
socket.on("saveFile", (activeId, body, callback) => {
// const file = sandboxFiles.fileData.find((f) => f.id === fileId)
// if (!file) return
// console.log("file " + file.id + ": ", file.data)
// callback(file.data)
});
// todo: send diffs + debounce for efficiency
socket.on("saveFile", (fileId, body) => __awaiter(void 0, void 0, void 0, function* () {
const file = sandboxFiles.fileData.find((f) => f.id === fileId);
if (!file)
return;
file.data = body;
console.log("save file " + file.id + ": ", file.data);
yield (0, utils_1.saveFile)(fileId, body);
}));
socket.on("renameFile", (fileId, newName) => __awaiter(void 0, void 0, void 0, function* () {
const file = sandboxFiles.fileData.find((f) => f.id === fileId);
if (!file)
return;
yield (0, utils_1.renameFile)(fileId, newName, file.data);
file.id = newName;
yield (0, utils_1.renameFile)(fileId, newName, file.data);
}));
}));
httpServer.listen(port, () => {

View File

@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.renameFile = exports.getSandboxFiles = void 0;
exports.saveFile = exports.renameFile = exports.getSandboxFiles = void 0;
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();
@ -47,7 +47,7 @@ const processFiles = (paths, id) => __awaiter(void 0, void 0, void 0, function*
}
else {
const folder = {
id: path, // issue todo: for example, folder "src" ID is: projects/a7vgttfqbgy403ratp7du3ln/src/App.css
id: path, // todo: wrong id. for example, folder "src" ID is: projects/a7vgttfqbgy403ratp7du3ln/src/App.css
type: "folder",
name: part,
children: [],
@ -90,3 +90,14 @@ const renameFile = (fileId, newName, data) => __awaiter(void 0, void 0, void 0,
return res.ok;
});
exports.renameFile = renameFile;
const saveFile = (fileId, data) => __awaiter(void 0, void 0, void 0, function* () {
const res = yield fetch(`https://storage.ishaan1013.workers.dev/api/save`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ fileId, data }),
});
return res.ok;
});
exports.saveFile = saveFile;