Spaces:
Running
Running
File size: 12,807 Bytes
16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 6bc7874 16ab111 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
/**
* Urdf Drag and Drop Utility
*
* This file provides functionality for handling drag and drop of Urdf folders.
* It converts the dropped files into accessible blobs for visualization.
*/
/**
* Converts a DataTransfer structure into an object with all paths and files.
* @param dataTransfer The DataTransfer object from the drop event
* @returns A promise that resolves with the file structure object
*/
export function dataTransferToFiles(
dataTransfer: DataTransfer
): Promise<Record<string, File>> {
if (!(dataTransfer instanceof DataTransfer)) {
throw new Error('Data must be of type "DataTransfer"');
}
const files: Record<string, File> = {};
/**
* Recursively processes a directory entry to extract all files
* Using type 'unknown' and then type checking for safety with WebKit's non-standard API
*/
function recurseDirectory(item: unknown): Promise<void> {
// Type guard for file entries
const isFileEntry = (
entry: unknown
): entry is {
isFile: boolean;
fullPath: string;
file: (callback: (file: File) => void) => void;
} =>
entry !== null &&
typeof entry === "object" &&
"isFile" in entry &&
typeof (entry as Record<string, unknown>).file === "function" &&
"fullPath" in entry;
// Type guard for directory entries
const isDirEntry = (
entry: unknown
): entry is {
isFile: boolean;
createReader: () => {
readEntries: (callback: (entries: unknown[]) => void) => void;
};
} =>
entry !== null &&
typeof entry === "object" &&
"isFile" in entry &&
typeof (entry as Record<string, unknown>).createReader === "function";
if (isFileEntry(item) && item.isFile) {
return new Promise((resolve) => {
item.file((file: File) => {
files[item.fullPath] = file;
resolve();
});
});
} else if (isDirEntry(item) && !item.isFile) {
const reader = item.createReader();
return new Promise((resolve) => {
const promises: Promise<void>[] = [];
// Exhaustively read all directory entries
function readNextEntries() {
reader.readEntries((entries: unknown[]) => {
if (entries.length === 0) {
Promise.all(promises).then(() => resolve());
} else {
entries.forEach((entry) => {
promises.push(recurseDirectory(entry));
});
readNextEntries();
}
});
}
readNextEntries();
});
}
return Promise.resolve();
}
return new Promise((resolve) => {
// Process dropped items
const dtitems = dataTransfer.items && Array.from(dataTransfer.items);
const dtfiles = Array.from(dataTransfer.files);
if (dtitems && dtitems.length && "webkitGetAsEntry" in dtitems[0]) {
const promises: Promise<void>[] = [];
for (let i = 0; i < dtitems.length; i++) {
const item = dtitems[i] as unknown as {
webkitGetAsEntry: () => unknown;
};
if (typeof item.webkitGetAsEntry === "function") {
const entry = item.webkitGetAsEntry();
if (entry) {
promises.push(recurseDirectory(entry));
}
}
}
Promise.all(promises).then(() => resolve(files));
} else {
// Add a '/' prefix to match the file directory entry on webkit browsers
dtfiles
.filter((f) => f.size !== 0)
.forEach((f) => (files["/" + f.name] = f));
resolve(files);
}
});
}
/**
* Cleans a file path by removing '..' and '.' tokens and normalizing slashes
*/
export function cleanFilePath(path: string): string {
return path
.replace(/\\/g, "/")
.split(/\//g)
.reduce((acc, el) => {
if (el === "..") acc.pop();
else if (el !== ".") acc.push(el);
return acc;
}, [] as string[])
.join("/");
}
/**
* Interface representing the structure of an Urdf processor
*/
export interface UrdfProcessor {
loadUrdf: (path: string) => void;
setUrlModifierFunc: (func: (url: string) => string) => void;
getPackage: () => string;
}
// Reference to hold the package path
const packageRef = { current: "" };
/**
* Reads the content of a Urdf file
* @param file The Urdf file object
* @returns A promise that resolves with the content of the file as a string
*/
export function readUrdfFileContent(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (event) => {
if (event.target && event.target.result) {
resolve(event.target.result as string);
} else {
reject(new Error("Failed to read Urdf file content"));
}
};
reader.onerror = () => reject(new Error("Error reading Urdf file"));
reader.readAsText(file);
});
}
/**
* Downloads a zip file from a URL and extracts its contents
* @param zipUrl URL of the zip file to download
* @param urdfProcessor The Urdf processor to use for loading
* @returns A promise that resolves with the extraction results
*/
export async function downloadAndExtractZip(
zipUrl: string,
urdfProcessor: UrdfProcessor
): Promise<{
files: Record<string, File>;
availableModels: string[];
blobUrls: Record<string, string>;
}> {
console.log("🔄 Downloading zip file from:", zipUrl);
try {
// Download the zip file
const response = await fetch(zipUrl);
if (!response.ok) {
throw new Error(`Failed to download zip: ${response.statusText}`);
}
const zipBlob = await response.blob();
// Load JSZip dynamically since it's much easier to work with than manual Blob handling
// We use dynamic import to avoid adding a dependency
const JSZip = (await import("jszip")).default;
const zip = new JSZip();
// Load the zip content
const contents = await zip.loadAsync(zipBlob);
// Convert zip contents to files
const files: Record<string, File> = {};
const filePromises: Promise<void>[] = [];
// Process each file in the zip
contents.forEach((relativePath, zipEntry) => {
if (!zipEntry.dir) {
const promise = zipEntry.async("blob").then((blob) => {
// Create a file with the proper name and path
const path = "/" + relativePath;
files[path] = new File(
[blob],
relativePath.split("/").pop() || "unknown",
{
type: getMimeType(relativePath.split(".").pop() || ""),
}
);
});
filePromises.push(promise);
}
});
// Wait for all files to be processed
await Promise.all(filePromises);
// Get all file paths and clean them
const fileNames = Object.keys(files).map((n) => cleanFilePath(n));
// Filter all files ending in Urdf
const availableModels = fileNames.filter((n) => /urdf$/i.test(n));
// Create blob URLs for Urdf files
const blobUrls: Record<string, string> = {};
availableModels.forEach((path) => {
blobUrls[path] = URL.createObjectURL(files[path]);
});
// Extract the package base path from the first Urdf model for reference
let packageBasePath = "";
if (availableModels.length > 0) {
// Extract the main directory path (e.g., '/cassie_description/')
const firstModel = availableModels[0];
const packageMatch = firstModel.match(/^(\/[^/]+\/)/);
if (packageMatch && packageMatch[1]) {
packageBasePath = packageMatch[1];
}
}
// Store the package path for future reference
const packagePathRef = packageBasePath;
urdfProcessor.setUrlModifierFunc((url) => {
// Find the matching file given the requested URL
// Store package reference for future use
if (packagePathRef) {
packageRef.current = packagePathRef;
}
// Simple approach: just find the first file that matches the end of the URL
const cleaned = cleanFilePath(url);
// Get the filename from the URL
const urlFilename = cleaned.split("/").pop() || "";
// Find the first file that ends with this filename
let fileName = fileNames.find((name) => name.endsWith(urlFilename));
// If no match found, just take the first file with a similar extension
if (!fileName && urlFilename.includes(".")) {
const extension = "." + urlFilename.split(".").pop();
fileName = fileNames.find((name) => name.endsWith(extension));
}
if (fileName !== undefined && fileName !== null) {
// Extract file extension for content type
const fileExtension = fileName.split(".").pop()?.toLowerCase() || "";
// Create blob URL with extension in the searchParams to help with format detection
const blob = new Blob([files[fileName]], {
type: getMimeType(fileExtension),
});
const blobUrl = URL.createObjectURL(blob) + "#." + fileExtension;
// Don't revoke immediately, wait for the mesh to be loaded
setTimeout(() => URL.revokeObjectURL(blobUrl), 5000);
return blobUrl;
}
console.warn(`No matching file found for: ${url}`);
return url;
});
return {
files,
availableModels,
blobUrls,
};
} catch (error) {
console.error("❌ Error downloading or extracting zip:", error);
throw error;
}
}
/**
* Processes dropped files and returns information about available Urdf models
*/
export async function processDroppedFiles(
dataTransfer: DataTransfer,
urdfProcessor: UrdfProcessor
): Promise<{
files: Record<string, File>;
availableModels: string[];
blobUrls: Record<string, string>;
}> {
// Reset the package reference
packageRef.current = "";
// Convert dropped files into a structured format
const files = await dataTransferToFiles(dataTransfer);
// Get all file paths and clean them
const fileNames = Object.keys(files).map((n) => cleanFilePath(n));
// Filter all files ending in Urdf
const availableModels = fileNames.filter((n) => /urdf$/i.test(n));
// Create blob URLs for Urdf files
const blobUrls: Record<string, string> = {};
availableModels.forEach((path) => {
blobUrls[path] = URL.createObjectURL(files[path]);
});
// Extract the package base path from the first Urdf model for reference
let packageBasePath = "";
if (availableModels.length > 0) {
// Extract the main directory path (e.g., '/cassie_description/')
const firstModel = availableModels[0];
const packageMatch = firstModel.match(/^(\/[^/]+\/)/);
if (packageMatch && packageMatch[1]) {
packageBasePath = packageMatch[1];
}
}
// Store the package path for future reference
const packagePathRef = packageBasePath;
urdfProcessor.setUrlModifierFunc((url) => {
// Find the matching file given the requested URL
// Store package reference for future use
if (packagePathRef) {
packageRef.current = packagePathRef;
}
// Simple approach: just find the first file that matches the end of the URL
const cleaned = cleanFilePath(url);
// Get the filename from the URL
const urlFilename = cleaned.split("/").pop() || "";
// Find the first file that ends with this filename
let fileName = fileNames.find((name) => name.endsWith(urlFilename));
// If no match found, just take the first file with a similar extension
if (!fileName && urlFilename.includes(".")) {
const extension = "." + urlFilename.split(".").pop();
fileName = fileNames.find((name) => name.endsWith(extension));
}
if (fileName !== undefined && fileName !== null) {
// Extract file extension for content type
const fileExtension = fileName.split(".").pop()?.toLowerCase() || "";
// Create blob URL with extension in the searchParams to help with format detection
const blob = new Blob([files[fileName]], {
type: getMimeType(fileExtension),
});
const blobUrl = URL.createObjectURL(blob) + "#." + fileExtension;
// Don't revoke immediately, wait for the mesh to be loaded
setTimeout(() => URL.revokeObjectURL(blobUrl), 5000);
return blobUrl;
}
console.warn(`No matching file found for: ${url}`);
return url;
});
return {
files,
availableModels,
blobUrls,
};
}
/**
* Get the MIME type for a file extension
*/
function getMimeType(extension: string): string {
switch (extension.toLowerCase()) {
case "stl":
return "model/stl";
case "obj":
return "model/obj";
case "gltf":
case "glb":
return "model/gltf+json";
case "dae":
return "model/vnd.collada+xml";
case "urdf":
return "application/xml";
default:
return "application/octet-stream";
}
}
|