File Utilities
File service for handling file storage and retrieval.
This service provides functionality for storing, retrieving, and managing files. It handles file storage on disk and maintains file metadata in the database. Files are organized by format and tag in the upload directory.
Methods
getFile()
getFile(
fileId
):Promise
<IFile
>
Retrieves a file document from the database
Example
import { fileService } from '@modular-rest/server';
const fileDoc = await fileService.getFile('file123');
console.log('File details:', fileDoc);
Parameters
Parameter | Type | Description |
---|---|---|
fileId | string | File ID to retrieve |
Returns
Promise
<IFile
>
Promise resolving to file document
Throws
If collection model is not found or file is not found
getFileLink()
getFileLink(
fileId
):Promise
<string
>
Retrieves the public URL link for a file
Example
import { fileService } from '@modular-rest/server';
const link = await fileService.getFileLink('file123');
// Returns: '/static/jpeg/profile/1234567890.jpeg'
Parameters
Parameter | Type | Description |
---|---|---|
fileId | string | File ID to get link for |
Returns
Promise
<string
>
Promise resolving to file URL
Throws
If static path root is not defined or file is not found
getFilePath()
getFilePath(
fileId
):Promise
<string
>
Gets the full filesystem path for a file
Example
import { fileService } from '@modular-rest/server';
const path = await fileService.getFilePath('file123');
// Returns: '/uploads/jpeg/profile/1234567890.jpeg'
Parameters
Parameter | Type | Description |
---|---|---|
fileId | string | File ID to get path for |
Returns
Promise
<string
>
Promise resolving to full file path
Throws
If upload directory is not set or file is not found
removeFile()
removeFile(
fileId
):Promise
<void
>
Removes a file from both database and disk
Example
import { fileService } from '@modular-rest/server';
try {
await fileService.removeFile('file123');
console.log('File removed successfully');
} catch (error) {
console.error('Failed to remove file:', error);
}
Parameters
Parameter | Type | Description |
---|---|---|
fileId | string | File ID to remove |
Returns
Promise
<void
>
Promise resolving when file is removed
Throws
If file is not found or removal fails