working file renaming
This commit is contained in:
@ -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, () => {
|
||||
|
@ -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
|
||||
}
|
Reference in New Issue
Block a user