CAD Loader Core Engine
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The CAD Loader Core Engine is the central architectural component of the Nextcloud CAD Viewer application. It orchestrates the retrieval of DWG and DXF files from the Nextcloud filesystem and manages their rendering within the browser using the @mlightcad/cad-viewer library. The engine serves as a bridge between the PHP-based backend, which handles file access permissions, and the Vue 3 frontend, which provides the interactive viewing experience.
This system is designed to run entirely in the browser without requiring specialized backend CAD processing servers. It leverages WebGL technology through the underlying library to provide high-performance panning, zooming, and layer management for technical drawings.
Sources: appinfo/info.xml:8-15, package.json:20-22, src/App.vue:1-12
Architectural Overview
The engine follows a decoupled architecture where the backend acts as a secure file provider and the frontend acts as the execution environment for CAD rendering.
Backend File Provisioning
The FileController handles the secure streaming of CAD data. It validates user sessions, ensures the requested file ID exists within the user's root folder, and checks for read permissions before serving the content.
Frontend Rendering Pipeline
The frontend initialization involves extracting file metadata from the Nextcloud environment, generating a secure API URL, and mounting the MlCadViewer component into the DOM.
Core Components and Logic
Initialization Utility (cadLoader.ts)
The loadCADViewer function is the primary entry point for the engine's frontend logic. It accepts a CADViewerOptions object to configure the environment.
| Option | Type | Default | Description |
|---|---|---|---|
locale | string | 'en' | Interface language |
theme | 'light' | 'dark' | 'dark' | Visual theme |
background | number | 0x1e1e1e | Canvas background color |
useMainThreadDraw | boolean | false | Rendering thread strategy |
Sources: src/utils/cadLoader.ts:9-24, src/utils/cadLoader.ts:31-40
Format Validation
The engine supports a specific set of MIME types for AutoCAD files. Validation occurs both on the backend (to prevent unauthorized streaming) and the frontend (to ensure the viewer can parse the input).
export function isSupportedCADFormat(mimeType: string): boolean {
return [
'application/acad',
'application/autocad_dwg',
'application/dwg',
'application/x-autocad',
'application/x-dwg',
'image/vnd.dwg',
'image/vnd.dxf',
'application/dxf',
'application/x-dxf',
'image/x-dxf',
].includes(mimeType)
}
Sources: src/utils/cadLoader.ts:95-108, lib/Controller/FileController.php:24-35, tests/unit/cadLoader.spec.ts:6-25
Backend API Endpoints
The FileController implements three primary endpoints to support the engine.
| Endpoint | Method | Parameter | Description |
|---|---|---|---|
/api/file/{fileId} | GET | int $fileId | Retrieves file metadata (name, size, type). |
/api/file/{fileId}/content | GET | int $fileId | Streams the raw file content with appropriate MIME headers. |
/api/file/{fileId}/preview | GET | int $fileId | Provides file content specifically for preview generation. |
Sources: lib/Controller/FileController.php:68, lib/Controller/FileController.php:121, lib/Controller/FileController.php:164
Lifecycle Management
The engine ensures memory efficiency by managing the lifecycle of the viewer instance. In App.vue, the onBeforeUnmount hook calls the dispose() method of the ViewerInstance, which unmounts the Vue application and clears references to the canvas.
onBeforeUnmount(() => {
viewerInstance.value?.dispose()
viewerInstance.value = null
})
Sources: src/App.vue:74-77, src/utils/cadLoader.ts:84-90
Conclusion
The CAD Loader Core Engine provides a robust framework for integrating CAD viewing capabilities into Nextcloud. By isolating the heavy lifting of CAD parsing into a browser-based library and utilizing Nextcloud's native file handling APIs, the engine maintains high security standards and provides a responsive user experience for engineering and architectural documents.