libraryFiles.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { isApiSliceableFilename, isSliceableFilename, type SlicerType } from './slicer';
  2. /**
  3. * Is a library file sliced — does it carry printer-executable G-code?
  4. *
  5. * The name alone is not evidence (#2993). A plate exported from Studio, or a
  6. * print dispatched through the cloud, is a fully sliced 3MF called `Foo.3mf`,
  7. * and deciding from the extension filed those as source-only projects with no
  8. * Print button — while the Archives card, which looks inside the zip, showed
  9. * the same file's green GCODE badge. The backend now classifies on content, so
  10. * `file_type` answers where the name cannot.
  11. *
  12. * Either signal is enough, which mirrors `classify_file_type` on the backend:
  13. * it returns `gcode.3mf` for a `.gcode.3mf` name without opening the file at
  14. * all, and only consults the zip when the name has not already settled it. A
  15. * row stored before the backfill therefore keeps its Print button on the
  16. * strength of its name.
  17. */
  18. export function isSlicedLibraryFile(file: {
  19. filename: string;
  20. file_type?: string | null;
  21. }): boolean {
  22. const fileType = (file.file_type || '').toLowerCase();
  23. if (fileType === 'gcode' || fileType === 'gcode.3mf') return true;
  24. const lower = (file.filename || '').toLowerCase();
  25. return lower.endsWith('.gcode') || lower.endsWith('.gcode.3mf');
  26. }
  27. /**
  28. * Can a file of this name be a sliced one, before it is uploaded and read?
  29. *
  30. * `.gcode` and `.gcode.3mf` are, by name. A plain `.3mf` may be too (a plate
  31. * exported from Studio), which only its content tells: check
  32. * `isSlicedLibraryFile` on the upload result.
  33. */
  34. export function mayBeSlicedFileName(filename: string): boolean {
  35. const lower = filename.toLowerCase();
  36. return lower.endsWith('.gcode') || lower.includes('.gcode.') || lower.endsWith('.3mf');
  37. }
  38. /**
  39. * Is a library file something a slicer can take as *input*?
  40. *
  41. * `isSliceableFilename` already refuses a `.gcode` / `.gcode.3mf` name — the
  42. * point being that a sliced file is an output, not an input. That refusal was
  43. * as name-bound as the Print gate it sits beside (#2993), so once a sliced
  44. * `Foo.3mf` correctly gained a Print button it would have been offered a
  45. * Slice one as well: re-slicing its own G-code.
  46. *
  47. * `desktopSlicer` is required even when `useSlicerApi` is true, where it is not
  48. * consulted: the desktop answer depends on which slicer the URL is handed to
  49. * (#3029), and a caller that cannot name one has not yet decided what its
  50. * button will do.
  51. */
  52. export function isSliceableLibraryFile(
  53. file: { filename: string; file_type?: string | null },
  54. useSlicerApi: boolean,
  55. desktopSlicer: SlicerType,
  56. ): boolean {
  57. if (isSlicedLibraryFile(file)) return false;
  58. return useSlicerApi
  59. ? isApiSliceableFilename(file.filename)
  60. : isSliceableFilename(file.filename, desktopSlicer);
  61. }