Backend Controllers
Relevant source files
The following files were used as context for generating this wiki page:
Backend Controllers in the Nextcloud CAD Viewer application facilitate the bridge between the Nextcloud server environment and the frontend Vue.js application. They are responsible for rendering the main application interface and providing secure access to CAD file metadata and raw content. These controllers leverage the Nextcloud App Framework to handle request routing, user session validation, and filesystem interactions.
The architecture is divided into two primary controllers: ViewController, which handles the initial page load and routing, and FileController, which serves as a REST-like API for retrieving CAD file data. All controllers are located within the OCA\CadViewer\Controller namespace.
Sources: lib/Controller/FileController.php, lib/Controller/ViewController.php, composer.json:30-32
ViewController
The ViewController is responsible for delivering the HTML and JavaScript entry point for the application. It maps specific web routes to the main template, allowing the frontend Vue.js application to take over.
Architecture and Data Flow
The ViewController inherits from the standard Nextcloud Controller class and uses TemplateResponse to render the user interface. It specifically handles requests to the main application page and can accept optional file identifiers to automatically open specific drawings upon load.
The diagram shows how a user request triggers the ViewController to render the base template where the Vue.js frontend is mounted.
Sources: lib/Controller/ViewController.php:14-36
Key Methods
| Method | Parameters | Returns | Description |
|---|---|---|---|
index() | None | TemplateResponse | Renders the default main application view. |
view() | string $fileIds | TemplateResponse | Renders the application view with pre-selected file IDs passed to the frontend. |
Sources: lib/Controller/ViewController.php:26-36
FileController
The FileController manages interactions with the Nextcloud filesystem. It provides endpoints for the frontend to fetch file details, stream raw binary data (DWG/DXF), and retrieve previews.
Security and Validation
This controller implements strict security checks:
- Session Validation: Uses
IUserSessionto ensure only logged-in users can access files. - Permissions: Verifies file readability via
IRootFolderandgetUserFolder. - MIME Type Whitelisting: Restricts access to a specific list of supported CAD formats to prevent unauthorized file execution or data leakage.
- Attributes: Methods use
#[NoAdminRequired]and#[NoCSRFRequired]to allow standard user access and API-style interactions.
Sources: lib/Controller/FileController.php:24-60
Supported CAD Formats
The controller explicitly supports the following MIME types for processing:
| Category | MIME Types |
|---|---|
| DWG | application/acad, application/autocad_dwg, application/dwg, application/x-autocad, application/x-dwg, image/vnd.dwg |
| DXF | image/vnd.dxf, application/dxf, application/x-dxf, image/x-dxf |
Sources: lib/Controller/FileController.php:24-35
Data Retrieval Flow
This sequence illustrates the secure streaming of CAD content from the server to the browser-based viewer.
Sources: lib/Controller/FileController.php:103-138, src/App.vue:53-54
Endpoint Summary
| Function | Method | Data Structure / Response |
|---|---|---|
getFile | int $fileId | DataResponse: JSON containing id, name, size, mimeType, and path. |
getFileContent | int $fileId | StreamResponse: The raw binary stream of the CAD file with appropriate Content-Type and inline disposition. |
preview | int $fileId | StreamResponse: Currently streams the original file as a fallback/preview mechanism. |
Sources: lib/Controller/FileController.php:65-180
Implementation Details
Dependency Injection
Both controllers utilize Nextcloud's dependency injection container. The FileController requires access to the root folder and user session to manage file access safely.
public function __construct(
string $appName,
IRequest $request,
IRootFolder $rootFolder,
IUserSession $userSession
) {
parent::__construct($appName, $request);
$this->rootFolder = $rootFolder;
$this->userSession = $userSession;
}
Sources: lib/Controller/FileController.php:41-49
Error Handling
The controllers return specific HTTP status codes based on the outcome of the request, ensuring the frontend can handle errors gracefully:
Http::STATUS_UNAUTHORIZED(401): User not logged in.Http::STATUS_NOT_FOUND(404): File ID does not exist.Http::STATUS_FORBIDDEN(403): User lacks read permissions.Http::STATUS_UNSUPPORTED_MEDIA_TYPE(415): File is not a supported DWG/DXF format.
Sources: lib/Controller/FileController.php:88-97, lib/Controller/FileController.php:131-137
The backend controllers provide a robust foundation for the CAD Viewer by ensuring that the intensive browser-based rendering of the @mlightcad/cad-viewer library is supported by a secure and efficient file delivery system within the Nextcloud ecosystem.
Sources: README.md:86-98, appinfo/info.xml:10-15