delete file ui + logic

This commit is contained in:
Ishaan Dey
2024-04-30 01:56:43 -04:00
parent 268dcb61f2
commit 894de9990a
13 changed files with 421 additions and 44 deletions

View File

@ -129,6 +129,19 @@ io.on("connection", (socket) => __awaiter(void 0, void 0, void 0, function* () {
});
yield (0, utils_1.renameFile)(fileId, newFileId, file.data);
}));
socket.on("deleteFile", (fileId, callback) => __awaiter(void 0, void 0, void 0, function* () {
const file = sandboxFiles.fileData.find((f) => f.id === fileId);
if (!file)
return;
fs_1.default.unlink(path_1.default.join(dirName, fileId), function (err) {
if (err)
throw err;
});
sandboxFiles.fileData = sandboxFiles.fileData.filter((f) => f.id !== fileId);
yield (0, utils_1.deleteFile)(fileId);
const newFiles = yield (0, utils_1.getSandboxFiles)(data.id);
callback(newFiles.files);
}));
socket.on("createTerminal", ({ id }) => {
console.log("creating terminal, id=" + id);
const pty = (0, node_pty_1.spawn)(os_1.default.platform() === "win32" ? "cmd.exe" : "bash", [], {

View File

@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveFile = exports.renameFile = exports.createFile = exports.getSandboxFiles = void 0;
exports.deleteFile = exports.saveFile = exports.renameFile = exports.createFile = 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();
@ -110,3 +110,14 @@ const saveFile = (fileId, data) => __awaiter(void 0, void 0, void 0, function* (
return res.ok;
});
exports.saveFile = saveFile;
const deleteFile = (fileId) => __awaiter(void 0, void 0, void 0, function* () {
const res = yield fetch(`https://storage.ishaan1013.workers.dev/api`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ fileId }),
});
return res.ok;
});
exports.deleteFile = deleteFile;

View File

@ -8,7 +8,13 @@ import { Server } from "socket.io"
import { z } from "zod"
import { User } from "./types"
import { createFile, getSandboxFiles, renameFile, saveFile } from "./utils"
import {
createFile,
deleteFile,
getSandboxFiles,
renameFile,
saveFile,
} from "./utils"
import { IDisposable, IPty, spawn } from "node-pty"
dotenv.config()
@ -148,6 +154,21 @@ io.on("connection", async (socket) => {
await renameFile(fileId, newFileId, file.data)
})
socket.on("deleteFile", async (fileId: string, callback) => {
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
if (!file) return
fs.unlink(path.join(dirName, fileId), function (err) {
if (err) throw err
})
sandboxFiles.fileData = sandboxFiles.fileData.filter((f) => f.id !== fileId)
await deleteFile(fileId)
const newFiles = await getSandboxFiles(data.id)
callback(newFiles.files)
})
socket.on("createTerminal", ({ id }: { id: string }) => {
console.log("creating terminal, id=" + id)
const pty = spawn(os.platform() === "win32" ? "cmd.exe" : "bash", [], {

View File

@ -123,3 +123,14 @@ export const saveFile = async (fileId: string, data: string) => {
})
return res.ok
}
export const deleteFile = async (fileId: string) => {
const res = await fetch(`https://storage.ishaan1013.workers.dev/api`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ fileId }),
})
return res.ok
}