File Upload & Download Handlers
Relevant source files
The following files were used as context for generating this wiki page:
File Upload and Download Handlers in the Duplicator project manage the lifecycle of documents (primarily PDF and PNG) within the application's ecosystem. These handlers facilitate the ingestion of files into the centralized "Bibliothèque" (Library) and provide specialized support for machine-specific help documentation. The system is designed to operate within an Electron-packaged environment, utilizing a PHP backend to process requests routed through a Caddy web server.
The architecture ensures secure storage by resolving paths dynamically based on the environment—whether running as a standard web application, a packaged Electron app, or a Linux AppImage. The handlers support various operations including drag-and-drop uploads, directory indexing, authenticated downloads for printing, and permanent storage for administrative tutorials.
Sources: app/view/bibliotheque.html.php:1-40, app/api/upload_aide_pdf.php:1-25, main-caddy.js:1037-1055
Library Asset Management
The Library (Bibliothèque) serves as the primary repository for user-uploaded print assets. It supports two distinct methods of file ingestion: direct upload and directory indexing.
Upload Workflow
Files uploaded through the UI are handled by upload_bibliotheque.php. The system validates file types (PDF and PNG) and enforces size constraints. The workflow involves moving temporary PHP uploads to a structured storage directory and updating the metadata in the SQLite database.
The UI utilizes a drag-and-drop zone with a DataTransfer object to handle multiple file inputs.
Sources: app/view/bibliotheque.html.php:115-150, app/public/index.php:178-182, app/api/upload_bibliotheque.php
File Operations
The application provides endpoints for downloading and deleting managed assets. Downloads are often initiated for printing purposes, while deletions remove both the physical file and its database record.
| Endpoint | Parameter | Description |
|---|---|---|
?download_pdf | id | Triggers a PDF download using download_pdf.php. |
?delete_bibliotheque_file | id (JSON) | Deletes the file and database entry via delete_bibliotheque_file.php. |
?get_bibliotheque_file | id | Retrieves a file for the internal viewer. |
Sources: app/public/index.php:154-205, app/view/bibliotheque.html.php:258-275
Specialized Help Documentation Uploads
Administrative users can upload PDF documentation specifically for machine help and tutorials. This is managed by a dedicated handler that ensures files are stored in a persistent configuration directory outside the temporary application path.
Path Resolution Logic
To maintain persistence across application updates in the Electron/AppImage environment, the resolveAidePdfDir() function implements a priority-based path resolution:
- Environment Variable:
DUPLI_AIDE_PDF_DIR. - Electron Database Path: Derived from
DUPLICATOR_DB_PATH(usually inuserData). - AppImage Detection: Stores in
$HOME/.config/Duplicator/aide_pdfs. - XDG Config: Standard Linux configuration paths.
- Fallback: Local
public/uploads/aide_pdfs.
Sources: app/api/upload_aide_pdf.php:64-105
Administrative API Actions
The upload_aide_pdf.php script handles three distinct actions for help files:
- Upload: Sanitizes filenames, validates
application/pdfMIME types, and saves to the resolved directory. - List: Returns a JSON array of all help PDFs including file size and formatted upload dates.
- Delete: Removes specific files from the filesystem after a security check on the filename.
Sources: app/api/upload_aide_pdf.php:130-220
Temporary and Processed File Handling
The application generates temporary files during PDF imposition, "unimposition," and image processing. These are managed via the system's temporary directory to avoid bloating the main installation.
Processing Download Endpoints
Specialized endpoints serve processed files:
?download_unimposed: Serves PDFs from theduplicator_unimposetemporary directory.?download_processed: Serves images/PDFs from theduplicator_image_processordirectory, supporting MIME types for PDF, PNG, JPG, and GIF.?download_png: Specifically handles ZIP or PNG outputs from the PDF-to-Image extraction tool.
Sources: app/public/index.php:218-270
Cleanup Mechanism
Electron's main process triggers a cleanup of temporary files stored in the public/tmp directory upon application startup and shutdown to maintain system hygiene.
function cleanupTmpFiles() {
// Resolved path based on ASAR packaging
const tmpPath = fs.existsSync(noAsarTmpPath) ? noAsarTmpPath : asarTmpPath;
if (fs.existsSync(tmpPath)) {
const files = fs.readdirSync(tmpPath);
files.forEach(file => fs.unlinkSync(path.join(tmpPath, file)));
}
}
Sources: main-caddy.js:452-475, main-caddy.js:1066-1075
Conclusion
The file handling system of Duplicator provides a robust bridge between the web-based UI and the underlying filesystem. By centralizing logic in scripts like upload_bibliotheque.php and upload_aide_pdf.php, the application maintains a clear separation between user assets and administrative documentation. The dynamic path resolution and temporary file management ensure that the application remains portable and persistent across different operating systems and packaging formats.