deleting folder logic

This commit is contained in:
Ishaan Dey
2024-05-11 17:23:45 -07:00
parent aa97a6771e
commit 9a5a0e13d3
8 changed files with 182 additions and 55 deletions

View File

@ -13,6 +13,7 @@ import {
createFile,
deleteFile,
generateCode,
getFolder,
getProjectSize,
getSandboxFiles,
renameFile,
@ -130,6 +131,11 @@ io.on("connection", async (socket) => {
callback(file.data)
})
socket.on("getFolder", async (folderId: string, callback) => {
const files = await getFolder(folderId)
callback(files)
})
// todo: send diffs + debounce for efficiency
socket.on("saveFile", async (fileId: string, body: string) => {
try {
@ -264,6 +270,33 @@ io.on("connection", async (socket) => {
}
})
socket.on("renameFolder", async (folderId: string, newName: string) => {
// todo
})
socket.on("deleteFolder", async (folderId: string, callback) => {
const files = await getFolder(folderId)
console.log("deleting folder", folderId, files)
await Promise.all(files.map(async (file) => {
fs.unlink(path.join(dirName, file), function (err) {
if (err) throw err
})
sandboxFiles.fileData = sandboxFiles.fileData.filter(
(f) => f.id !== file
)
await deleteFile(file)
}))
const newFiles = await getSandboxFiles(data.sandboxId)
callback(newFiles.files)
})
socket.on("createTerminal", (id: string, callback) => {
console.log("creating terminal", id)
if (terminals[id] || Object.keys(terminals).length >= 4) {
@ -339,6 +372,7 @@ io.on("connection", async (socket) => {
instructions: string,
callback
) => {
// Log code generation credit in DB
const fetchPromise = fetch(`https://database.ishaan1013.workers.dev/api/sandbox/generate`, {
method: "POST",
headers: {
@ -349,6 +383,7 @@ io.on("connection", async (socket) => {
}),
})
// Generate code from cloudflare workers AI
const generateCodePromise = generateCode({
fileName,
code,

View File

@ -1,23 +1,28 @@
import { RateLimiterMemory } from "rate-limiter-flexible"
export const saveFileRL = new RateLimiterMemory({
points: 3,
points: 2,
duration: 1,
})
export const MAX_BODY_SIZE = 5 * 1024 * 1024
export const createFileRL = new RateLimiterMemory({
points: 3,
duration: 1,
points: 1,
duration: 2,
})
export const renameFileRL = new RateLimiterMemory({
points: 3,
duration: 1,
points: 1,
duration: 2,
})
export const deleteFileRL = new RateLimiterMemory({
points: 3,
duration: 1,
points: 1,
duration: 2,
})
export const deleteFolderRL = new RateLimiterMemory({
points: 1,
duration: 2,
})

View File

@ -10,16 +10,25 @@ import {
} from "./types"
export const getSandboxFiles = async (id: string) => {
const sandboxRes = await fetch(
const res = await fetch(
`https://storage.ishaan1013.workers.dev/api?sandboxId=${id}`
)
const sandboxData: R2Files = await sandboxRes.json()
const data: R2Files = await res.json()
const paths = sandboxData.objects.map((obj) => obj.key)
const paths = data.objects.map((obj) => obj.key)
const processedFiles = await processFiles(paths, id)
return processedFiles
}
export const getFolder = async (folderId: string) => {
const res = await fetch(
`https://storage.ishaan1013.workers.dev/api?folderId=${folderId}`
)
const data: R2Files = await res.json()
return data.objects.map((obj) => obj.key)
}
const processFiles = async (paths: string[], id: string) => {
const root: TFolder = { id: "/", type: "folder", name: "/", children: [] }
const fileData: TFileData[] = []

View File

@ -36,11 +36,15 @@ export default {
if (method === 'GET') {
const params = url.searchParams;
const sandboxId = params.get('sandboxId');
const folderId = params.get('folderId');
const fileId = params.get('fileId');
if (sandboxId) {
const res = await env.R2.list({ prefix: `projects/${sandboxId}` });
return new Response(JSON.stringify(res), { status: 200 });
} else if (folderId) {
const res = await env.R2.list({ prefix: folderId });
return new Response(JSON.stringify(res), { status: 200 });
} else if (fileId) {
const obj = await env.R2.get(fileId);
if (obj === null) {