Function Signature
Description
Returns the absolute file path for a file in the specified directory. This function validates the path to prevent directory traversal attacks and ensures the resolved path is within the configured mount point.Parameters
The name of the file to locate. This should be the filename returned by
storeFileLocally.- Must be a safe basename (no path separators)
- Example:
"aBcD1234.png"
The folder path where the file is located, relative to the configured mount point.
- Use forward slashes for subdirectories (e.g.,
/userFilesor/uploads/images) - Defaults to the root of the mount directory if not provided
- Path is automatically normalized and validated for security
Return Value
The absolute file path in the format:
{mount}/{filelocation}/{filename}Example: /home/user/storage/userFiles/aBcD1234.pngPath Resolution Behavior
The function performs several security checks:- Validates filename - Ensures it’s a safe basename without path separators
- Normalizes file location - Converts the path to a safe relative path
- Resolves absolute path - Combines mount, location, and filename
- Validates mount boundary - Ensures the resolved path is within the configured mount point
Examples
Basic Usage
Check if File Exists
Read File Contents
Use with Image Processing
Serve File with Custom Logic
Compare with Root Directory Files
Error Handling
Common Errors
Mount not configured:fileStorage.mount to your nuxt.config.ts
Path outside mount:
Security Considerations
This function includes built-in security measures:
- Validates filename is a safe basename (no
../or similar) - Ensures resolved path stays within the mount directory
- Prevents directory traversal attacks
- Normalizes paths to prevent bypass attempts
Use Cases
- File operations - Use with Node.js fs operations to read, modify, or analyze files
- Custom streaming - Get the path to implement custom streaming logic
- File validation - Check if files exist before performing operations
- Metadata retrieval - Use with fs.stat to get file information
- Integration - Pass file paths to third-party libraries (image processing, PDF generation, etc.)
See Also
- storeFileLocally - Store a file and get its filename
- retrieveFileLocally - Stream a file directly to the client
- getFilesLocally - Get all files in a directory
- deleteFile - Delete a file from storage