2024-05-13 22:04:00 -07:00
|
|
|
import e from "cors";
|
2024-04-26 21:57:30 -04:00
|
|
|
import {
|
|
|
|
R2FileBody,
|
|
|
|
R2Files,
|
|
|
|
Sandbox,
|
|
|
|
TFile,
|
|
|
|
TFileData,
|
|
|
|
TFolder,
|
|
|
|
User,
|
2024-05-13 22:04:00 -07:00
|
|
|
} 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-05-11 17:23:45 -07:00
|
|
|
const res = await fetch(
|
2024-04-26 02:10:37 -04:00
|
|
|
`https://storage.ishaan1013.workers.dev/api?sandboxId=${id}`
|
2024-05-13 22:04:00 -07:00
|
|
|
);
|
|
|
|
const data: R2Files = await res.json();
|
2024-04-26 02:10:37 -04:00
|
|
|
|
2024-05-13 22:04:00 -07:00
|
|
|
const paths = data.objects.map((obj) => obj.key);
|
|
|
|
const processedFiles = await processFiles(paths, id);
|
|
|
|
return processedFiles;
|
|
|
|
};
|
2024-04-26 02:10:37 -04:00
|
|
|
|
2024-05-11 17:23:45 -07:00
|
|
|
export const getFolder = async (folderId: string) => {
|
|
|
|
const res = await fetch(
|
|
|
|
`https://storage.ishaan1013.workers.dev/api?folderId=${folderId}`
|
2024-05-13 22:04:00 -07:00
|
|
|
);
|
|
|
|
const data: R2Files = await res.json();
|
2024-05-11 17:23:45 -07:00
|
|
|
|
2024-05-13 22:04:00 -07:00
|
|
|
return data.objects.map((obj) => obj.key);
|
|
|
|
};
|
2024-05-11 17:23:45 -07:00
|
|
|
|
2024-04-26 21:57:30 -04:00
|
|
|
const processFiles = async (paths: string[], id: string) => {
|
2024-05-13 22:04:00 -07:00
|
|
|
const root: TFolder = { id: "/", type: "folder", name: "/", children: [] };
|
|
|
|
const fileData: TFileData[] = [];
|
2024-04-26 02:10:37 -04:00
|
|
|
|
|
|
|
paths.forEach((path) => {
|
2024-05-13 22:04:00 -07:00
|
|
|
const allParts = path.split("/");
|
2024-04-26 02:10:37 -04:00
|
|
|
if (allParts[1] !== id) {
|
2024-05-13 22:04:00 -07:00
|
|
|
return;
|
2024-04-26 02:10:37 -04:00
|
|
|
}
|
|
|
|
|
2024-05-13 22:04:00 -07:00
|
|
|
const parts = allParts.slice(2);
|
|
|
|
let current: TFolder = root;
|
2024-04-26 02:10:37 -04:00
|
|
|
|
|
|
|
for (let i = 0; i < parts.length; i++) {
|
2024-05-13 22:04:00 -07:00
|
|
|
const part = parts[i];
|
|
|
|
const isFile = i === parts.length - 1 && part.includes(".");
|
|
|
|
const existing = current.children.find((child) => child.name === part);
|
2024-04-26 02:10:37 -04:00
|
|
|
|
|
|
|
if (existing) {
|
|
|
|
if (!isFile) {
|
2024-05-13 22:04:00 -07:00
|
|
|
current = existing as TFolder;
|
2024-04-26 02:10:37 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isFile) {
|
2024-05-13 22:04:00 -07:00
|
|
|
const file: TFile = { id: path, type: "file", name: part };
|
|
|
|
current.children.push(file);
|
|
|
|
fileData.push({ id: path, data: "" });
|
2024-04-26 02:10:37 -04:00
|
|
|
} else {
|
|
|
|
const folder: TFolder = {
|
2024-05-09 00:45:10 -07:00
|
|
|
// id: path, // todo: wrong id. for example, folder "src" ID is: projects/a7vgttfqbgy403ratp7du3ln/src/App.css
|
|
|
|
id: `projects/${id}/${parts.slice(0, i + 1).join("/")}`,
|
2024-04-26 02:10:37 -04:00
|
|
|
type: "folder",
|
|
|
|
name: part,
|
|
|
|
children: [],
|
2024-05-13 22:04:00 -07:00
|
|
|
};
|
|
|
|
current.children.push(folder);
|
|
|
|
current = folder;
|
2024-04-26 02:10:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-13 22:04:00 -07:00
|
|
|
});
|
2024-04-26 02:10:37 -04:00
|
|
|
|
2024-04-26 21:57:30 -04:00
|
|
|
await Promise.all(
|
|
|
|
fileData.map(async (file) => {
|
2024-05-13 22:04:00 -07:00
|
|
|
const data = await fetchFileContent(file.id);
|
|
|
|
file.data = data;
|
2024-04-26 21:57:30 -04:00
|
|
|
})
|
2024-05-13 22:04:00 -07:00
|
|
|
);
|
2024-04-26 21:57:30 -04:00
|
|
|
|
|
|
|
return {
|
|
|
|
files: root.children,
|
|
|
|
fileData,
|
2024-05-13 22:04:00 -07:00
|
|
|
};
|
|
|
|
};
|
2024-04-26 21:57:30 -04:00
|
|
|
|
|
|
|
const fetchFileContent = async (fileId: string): Promise<string> => {
|
|
|
|
try {
|
|
|
|
const fileRes = await fetch(
|
|
|
|
`https://storage.ishaan1013.workers.dev/api?fileId=${fileId}`
|
2024-05-13 22:04:00 -07:00
|
|
|
);
|
|
|
|
return await fileRes.text();
|
2024-04-26 21:57:30 -04:00
|
|
|
} catch (error) {
|
2024-05-13 22:04:00 -07:00
|
|
|
console.error("ERROR fetching file:", error);
|
|
|
|
return "";
|
2024-04-26 21:57:30 -04:00
|
|
|
}
|
2024-05-13 22:04:00 -07:00
|
|
|
};
|
2024-04-26 02:10:37 -04:00
|
|
|
|
2024-04-29 00:50:25 -04:00
|
|
|
export const createFile = async (fileId: string) => {
|
|
|
|
const res = await fetch(`https://storage.ishaan1013.workers.dev/api`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ fileId }),
|
2024-05-13 22:04:00 -07:00
|
|
|
});
|
|
|
|
return res.ok;
|
|
|
|
};
|
2024-04-29 00:50:25 -04:00
|
|
|
|
2024-04-27 14:23:09 -04:00
|
|
|
export const renameFile = async (
|
|
|
|
fileId: string,
|
2024-04-29 00:50:25 -04:00
|
|
|
newFileId: string,
|
2024-04-27 14:23:09 -04:00
|
|
|
data: string
|
|
|
|
) => {
|
|
|
|
const res = await fetch(`https://storage.ishaan1013.workers.dev/api/rename`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ fileId, newFileId, data }),
|
2024-05-13 22:04:00 -07:00
|
|
|
});
|
|
|
|
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 }),
|
2024-05-13 22:04:00 -07:00
|
|
|
});
|
|
|
|
return res.ok;
|
|
|
|
};
|
2024-04-30 01:56:43 -04:00
|
|
|
|
|
|
|
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 }),
|
2024-05-13 22:04:00 -07:00
|
|
|
});
|
|
|
|
return res.ok;
|
|
|
|
};
|
2024-05-05 16:51:30 -07:00
|
|
|
|
2024-05-09 22:32:21 -07:00
|
|
|
export const getProjectSize = async (id: string) => {
|
|
|
|
const res = await fetch(
|
|
|
|
`https://storage.ishaan1013.workers.dev/api/size?sandboxId=${id}`
|
2024-05-13 22:04:00 -07:00
|
|
|
);
|
|
|
|
return (await res.json()).size;
|
|
|
|
};
|
2024-05-09 22:32:21 -07:00
|
|
|
|
2024-05-12 02:10:31 -07:00
|
|
|
export const stopServer = async (sandboxId: string, userId: string) => {
|
2024-05-13 22:04:00 -07:00
|
|
|
const res = await fetch("http://localhost:4001/stop", {
|
2024-05-12 02:10:31 -07:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
sandboxId,
|
2024-05-13 22:04:00 -07:00
|
|
|
userId,
|
2024-05-12 02:10:31 -07:00
|
|
|
}),
|
2024-05-13 22:04:00 -07:00
|
|
|
});
|
|
|
|
const data = await res.json();
|
2024-05-12 22:06:11 -07:00
|
|
|
|
2024-05-13 22:04:00 -07:00
|
|
|
return data;
|
|
|
|
};
|