delete file ui + logic
This commit is contained in:
@ -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", [], {
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user