2024-04-26 02:10:37 -04:00
|
|
|
"use strict";
|
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2024-04-27 14:23:09 -04:00
|
|
|
exports.getSandboxFiles = void 0;
|
2024-04-26 02:10:37 -04:00
|
|
|
const getSandboxFiles = (id) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
|
const sandboxRes = yield fetch(`https://storage.ishaan1013.workers.dev/api?sandboxId=${id}`);
|
|
|
|
const sandboxData = yield sandboxRes.json();
|
|
|
|
const paths = sandboxData.objects.map((obj) => obj.key);
|
2024-04-26 21:57:30 -04:00
|
|
|
const processedFiles = yield processFiles(paths, id);
|
|
|
|
// console.log("processedFiles.fileData:", processedFiles.fileData)
|
|
|
|
return processedFiles;
|
2024-04-26 02:10:37 -04:00
|
|
|
});
|
2024-04-27 14:23:09 -04:00
|
|
|
exports.getSandboxFiles = getSandboxFiles;
|
2024-04-26 21:57:30 -04:00
|
|
|
const processFiles = (paths, id) => __awaiter(void 0, void 0, void 0, function* () {
|
2024-04-26 02:10:37 -04:00
|
|
|
const root = { id: "/", type: "folder", name: "/", children: [] };
|
2024-04-26 21:57:30 -04:00
|
|
|
const fileData = [];
|
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 = 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (isFile) {
|
|
|
|
const file = { 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 = {
|
|
|
|
id: path,
|
|
|
|
type: "folder",
|
|
|
|
name: part,
|
|
|
|
children: [],
|
|
|
|
};
|
|
|
|
current.children.push(folder);
|
|
|
|
current = folder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2024-04-26 21:57:30 -04:00
|
|
|
yield Promise.all(fileData.map((file) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
|
const data = yield fetchFileContent(file.id);
|
|
|
|
file.data = data;
|
|
|
|
})));
|
|
|
|
return {
|
|
|
|
files: root.children,
|
|
|
|
fileData,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
const fetchFileContent = (fileId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
|
try {
|
|
|
|
const fileRes = yield fetch(`https://storage.ishaan1013.workers.dev/api?fileId=${fileId}`);
|
|
|
|
return yield fileRes.text();
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
console.error("ERROR fetching file:", error);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
});
|