slicer.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Utility for opening files in Bambu Studio slicer
  3. *
  4. * The URL protocol handler is OS-specific:
  5. * - Windows: bambustudio://
  6. * - macOS/Linux: bambustudioopen://
  7. */
  8. type Platform = 'windows' | 'macos' | 'linux' | 'unknown';
  9. /**
  10. * Detect the user's operating system
  11. */
  12. export function detectPlatform(): Platform {
  13. const userAgent = navigator.userAgent.toLowerCase();
  14. const platform = navigator.platform?.toLowerCase() || '';
  15. if (userAgent.includes('win') || platform.includes('win')) {
  16. return 'windows';
  17. }
  18. if (userAgent.includes('mac') || platform.includes('mac')) {
  19. return 'macos';
  20. }
  21. if (userAgent.includes('linux') || platform.includes('linux')) {
  22. return 'linux';
  23. }
  24. return 'unknown';
  25. }
  26. /**
  27. * Get the appropriate slicer protocol for the current OS
  28. */
  29. export function getSlicerProtocol(): string {
  30. const platform = detectPlatform();
  31. switch (platform) {
  32. case 'windows':
  33. return 'bambustudio://';
  34. case 'macos':
  35. case 'linux':
  36. default:
  37. return 'bambustudioopen://';
  38. }
  39. }
  40. /**
  41. * Open a URL in Bambu Studio slicer
  42. * @param downloadUrl - The URL to the file to open (will be encoded)
  43. */
  44. export function openInSlicer(downloadUrl: string): void {
  45. const protocol = getSlicerProtocol();
  46. window.location.href = `${protocol}${encodeURIComponent(downloadUrl)}`;
  47. }
  48. /**
  49. * Build a full download URL for a file
  50. * @param path - The API path (e.g., from api.getArchiveForSlicer())
  51. */
  52. export function buildDownloadUrl(path: string): string {
  53. return `${window.location.origin}${path}`;
  54. }
  55. /**
  56. * Convenience function to open an archive in the slicer
  57. * @param path - The API path to the archive
  58. */
  59. export function openArchiveInSlicer(path: string): void {
  60. const downloadUrl = buildDownloadUrl(path);
  61. openInSlicer(downloadUrl);
  62. }