2024-04-26 21:57:30 -04:00
|
|
|
import {
|
|
|
|
R2FileBody,
|
|
|
|
R2Files,
|
|
|
|
Sandbox,
|
|
|
|
TFile,
|
|
|
|
TFileData,
|
|
|
|
TFolder,
|
|
|
|
User,
|
|
|
|
} from "./types"
|
2024-04-26 02:10:37 -04:00
|
|
|
|
2024-04-27 14:23:09 -04:00
|
|
|
export const getSandboxFiles = async (id: string) => {
|
2024-04-26 02:10:37 -04:00
|
|
|
const sandboxRes = await fetch(
|
|
|
|
`https://storage.ishaan1013.workers.dev/api?sandboxId=${id}`
|
|
|
|
)
|
|
|
|
const sandboxData: R2Files = await sandboxRes.json()
|
|
|
|
|
|
|
|
const paths = sandboxData.objects.map((obj) => obj.key)
|
2024-04-26 21:57:30 -04:00
|
|
|
const processedFiles = await processFiles(paths, id)
|
|
|
|
// console.log("processedFiles.fileData:", processedFiles.fileData)
|
|
|
|
return processedFiles
|
2024-04-26 02:10:37 -04:00
|
|
|
}
|
|
|
|
|
2024-04-26 21:57:30 -04:00
|
|
|
const processFiles = async (paths: string[], id: string) => {
|
2024-04-26 02:10:37 -04:00
|
|
|
const root: TFolder = { id: "/", type: "folder", name: "/", children: [] }
|
2024-04-26 21:57:30 -04:00
|
|
|
const fileData: TFileData[] = []
|
2024-04-26 02:10:37 -04:00
|
|
|
|
|
|
|
paths.forEach((path) => {
|
|
|
|
const allParts = path.split("/")
|
|
|
|
if (allParts[1] !== id) {
|
|
|
|
console.log("invalid path!!!!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const parts = allParts.slice(2)
|
|
|
|
let current: TFolder = root
|
|
|
|
|
|
|
|
for (let i = 0; i < parts.length; i++) {
|
|
|
|
const part = parts[i]
|
|
|
|
const isFile = i === parts.length - 1 && part.includes(".")
|
|
|
|
const existing = current.children.find((child) => child.name === part)
|
|
|
|
|
|
|
|
if (existing) {
|
|
|
|
if (!isFile) {
|
|
|
|
current = existing as TFolder
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isFile) {
|
|
|
|
const file: TFile = { id: path, type: "file", name: part }
|
|
|
|
current.children.push(file)
|
2024-04-26 21:57:30 -04:00
|
|
|
fileData.push({ id: path, data: "" })
|
2024-04-26 02:10:37 -04:00
|
|
|
} else {
|
|
|
|
const folder: TFolder = {
|
2024-04-27 19:12:25 -04:00
|
|
|
id: path, // todo: wrong id. for example, folder "src" ID is: projects/a7vgttfqbgy403ratp7du3ln/src/App.css
|
2024-04-26 02:10:37 -04:00
|
|
|
type: "folder",
|
|
|
|
name: part,
|
|
|
|
children: [],
|
|
|
|
}
|
|
|
|
current.children.push(folder)
|
|
|
|
current = folder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-04-26 21:57:30 -04:00
|
|
|
await Promise.all(
|
|
|
|
fileData.map(async (file) => {
|
|
|
|
const data = await fetchFileContent(file.id)
|
|
|
|
file.data = data
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
files: root.children,
|
|
|
|
fileData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fetchFileContent = async (fileId: string): Promise<string> => {
|
|
|
|
try {
|
|
|
|
const fileRes = await fetch(
|
|
|
|
`https://storage.ishaan1013.workers.dev/api?fileId=${fileId}`
|
|
|
|
)
|
|
|
|
return await fileRes.text()
|
|
|
|
} catch (error) {
|
|
|
|
console.error("ERROR fetching file:", error)
|
|
|
|
return ""
|
|
|
|
}
|
2024-04-26 02:10:37 -04:00
|
|
|
}
|
|
|
|
|
2024-04-27 14:23:09 -04:00
|
|
|
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
|
|
|
|
}
|
2024-04-27 19:12:25 -04:00
|
|
|
|
|
|
|
export const saveFile = async (fileId: string, data: string) => {
|
|
|
|
const res = await fetch(`https://storage.ishaan1013.workers.dev/api/save`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ fileId, data }),
|
|
|
|
})
|
|
|
|
return res.ok
|
|
|
|
}
|