fix(slicer): token-based auth for Open in Slicer downloads (#433)

Slicer protocol handlers (bambustudio://, orcaslicer://) launch the
slicer app which fetches files via HTTP but cannot send auth headers.
The global auth middleware returned 401, causing "importing failed."

Add short-lived, single-use download tokens: the frontend fetches a
token via authenticated POST, then builds a /dl/{token}/{filename} URL
the slicer can access without auth headers. Tokens are validated
server-side (5-min expiry, single-use). Applies to archive files,
source 3MFs, and library files.

Also fix platform-specific URL formats to match slicer source code:
- macOS: bambustudioopen:// with encodeURIComponent
- Windows/Linux: bambustudio://open?file= (was wrongly using macOS scheme on Linux)
- OrcaSlicer: orcaslicer://open?file=
This commit is contained in:
maziggy
2026-02-18 16:37:35 +01:00
parent f13b385480
commit a903816b8a
11 changed files with 727 additions and 451 deletions
+12
View File
@@ -2786,6 +2786,10 @@ export const api = {
},
getSource3mfForSlicer: (archiveId: number, filename: string) =>
`${API_BASE}/archives/${archiveId}/source/${encodeURIComponent(filename.endsWith('.3mf') ? filename : filename + '.3mf')}`,
createSourceSlicerToken: (archiveId: number) =>
request<{ token: string }>(`/archives/${archiveId}/source-slicer-token`, { method: 'POST' }),
getSourceSlicerDownloadUrl: (archiveId: number, token: string, filename: string) =>
`${API_BASE}/archives/${archiveId}/source-dl/${token}/${encodeURIComponent(filename.endsWith('.3mf') ? filename : filename + '.3mf')}`,
uploadSource3mf: async (archiveId: number, file: File): Promise<{ status: string; filename: string }> => {
const formData = new FormData();
formData.append('file', file);
@@ -2908,6 +2912,10 @@ export const api = {
`${API_BASE}/archives/${archiveId}/project-image/${encodeURIComponent(imagePath)}`,
getArchiveForSlicer: (id: number, filename: string) =>
`${API_BASE}/archives/${id}/file/${encodeURIComponent(filename.endsWith('.3mf') ? filename : filename + '.3mf')}`,
createArchiveSlicerToken: (archiveId: number) =>
request<{ token: string }>(`/archives/${archiveId}/slicer-token`, { method: 'POST' }),
getArchiveSlicerDownloadUrl: (archiveId: number, token: string, filename: string) =>
`${API_BASE}/archives/${archiveId}/dl/${token}/${encodeURIComponent(filename.endsWith('.3mf') ? filename : filename + '.3mf')}`,
getArchivePlates: (archiveId: number) =>
request<ArchivePlatesResponse>(`/archives/${archiveId}/plates`),
getArchiveFilamentRequirements: (archiveId: number, plateId?: number) =>
@@ -3938,6 +3946,10 @@ export const api = {
deleteLibraryFile: (id: number) =>
request<{ status: string; message: string }>(`/library/files/${id}`, { method: 'DELETE' }),
getLibraryFileDownloadUrl: (id: number) => `${API_BASE}/library/files/${id}/download`,
createLibrarySlicerToken: (fileId: number) =>
request<{ token: string }>(`/library/files/${fileId}/slicer-token`, { method: 'POST' }),
getLibrarySlicerDownloadUrl: (fileId: number, token: string, filename: string) =>
`${API_BASE}/library/files/${fileId}/dl/${token}/${encodeURIComponent(filename)}`,
downloadLibraryFile: async (id: number, filename?: string): Promise<void> => {
const headers: Record<string, string> = {};
if (authToken) {