Skip to content

File Utilities ​

File service class for handling file operations FileService

Description ​

This class provides methods for managing file uploads, retrieval, and deletion. It handles physical file storage and database metadata management.

Methods ​

getFileLink(fileId): Promise<string>

Gets the public URL for a file

Example ​

typescript
import { fileService } from '@modular-rest/server';

const url = await fileService.getFileLink('file123');
// Returns: '/assets/jpeg/profile/1234567890.jpeg'

Parameters ​

ParameterTypeDescription
fileIdstringID of the file

Returns ​

Promise<string>

The public URL


getFilePath() ​

getFilePath(fileId): Promise<string>

Gets the physical path for a file

Example ​

typescript
import { fileService } from '@modular-rest/server';

const path = await fileService.getFilePath('file123');
// Returns: '/uploads/jpeg/profile/1234567890.jpeg'

Parameters ​

ParameterTypeDescription
fileIdstringID of the file

Returns ​

Promise<string>

The physical path


removeFile() ​

removeFile(fileId): Promise<boolean>

Deletes a file from disc and database

Example ​

typescript
import { fileService } from '@modular-rest/server';

await fileService.removeFile('file123');

Parameters ​

ParameterTypeDescription
fileIdstringID of the file to delete

Returns ​

Promise<boolean>

True if deletion was successful

Throws ​

If file is not found or deletion fails


storeFile() ​

storeFile(options): Promise<IFile>

Stores a file on disc and creates metadata in database

Example ​

typescript
import { fileService } from '@modular-rest/server';

const file = await fileService.storeFile({
  file: {
    path: '/tmp/upload.jpg',
    type: 'image/jpeg',
    name: 'profile.jpg',
    size: 1024
  },
  ownerId: 'user123',
  tag: 'profile',
  removeFileAfterStore: true
});

Parameters ​

ParameterTypeDescription
optionsStoreFileOptionsFile storage options

Returns ​

Promise<IFile>

The created file document

Throws ​

If upload directory is not set or storage fails