working file renaming

This commit is contained in:
Ishaan Dey
2024-04-27 14:23:09 -04:00
parent 76b2fc7e0f
commit 676f88a7ce
10 changed files with 252 additions and 22 deletions

View File

@ -5,7 +5,7 @@ import { Server } from "socket.io"
import { z } from "zod"
import { User } from "./types"
import getSandboxFiles from "./getSandboxFiles"
import { getSandboxFiles, renameFile } from "./utils"
dotenv.config()
@ -75,9 +75,16 @@ io.on("connection", async (socket) => {
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
if (!file) return
// console.log("file " + file.id + ": ", file.data)
console.log("file " + file.id + ": ", file.data)
callback(file.data)
})
socket.on("renameFile", async (fileId: string, newName: string) => {
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
if (!file) return
await renameFile(fileId, newName, file.data)
file.id = newName
})
})
httpServer.listen(port, () => {

View File

@ -8,7 +8,7 @@ import {
User,
} from "./types"
const getSandboxFiles = async (id: string) => {
export const getSandboxFiles = async (id: string) => {
const sandboxRes = await fetch(
`https://storage.ishaan1013.workers.dev/api?sandboxId=${id}`
)
@ -50,7 +50,7 @@ const processFiles = async (paths: string[], id: string) => {
fileData.push({ id: path, data: "" })
} else {
const folder: TFolder = {
id: path,
id: path, // issue todo: for example, folder "src" ID is: projects/a7vgttfqbgy403ratp7du3ln/src/App.css
type: "folder",
name: part,
children: [],
@ -87,4 +87,20 @@ const fetchFileContent = async (fileId: string): Promise<string> => {
}
}
export default getSandboxFiles
export const renameFile = async (
fileId: string,
newName: string,
data: string
) => {
const parts = fileId.split("/")
const newFileId = parts.slice(0, parts.length - 1).join("/") + "/" + newName
const res = await fetch(`https://storage.ishaan1013.workers.dev/api/rename`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ fileId, newFileId, data }),
})
return res.ok
}