Skip to content

FileProvider Service Documentation

The FileProvider service is responsible for managing file operations such as uploading, removing, and retrieving file information. It provides methods to interact with files on the server and manage file metadata.

To use the FileProvider service, import it as follows:

javascript
import { fileProvider } from '@modular-rest/client'

uploadFile()

Uploads a file to the server.

Arguments

NameTypeDescription
filestring | BlobThe file to be uploaded
onProgress(progressEvent: ProgressEvent) => voidCallback function to track upload progress
tagstring (optional)Tag for the file, defaults to "untagged"

Return and Throw

TypeDescription
Promise<FileDocument>Resolves with the uploaded file document
ErrorThrows an error if the upload fails

Example

javascript
const file = new Blob(['Hello, World!'], { type: 'text/plain' });
const onProgress = (event) => console.log(`Upload progress: ${event.loaded / event.total * 100}%`);

fileProvider.uploadFile(file, onProgress, 'documents')
  .then(fileDoc => console.log('Uploaded file:', fileDoc))
  .catch(error => console.error('Upload failed:', error));

uploadFileToURL()

Uploads a file to a specific URL.

Arguments

NameTypeDescription
urlstringThe URL to upload the file to
filestringBlob
bodyany (optional)Additional data to be sent with the request
onProgress(progressEvent: ProgressEvent) => voidCallback function to track upload progress
tagstringTag for the file

Return and Throw

TypeDescription
Promise<any>Resolves with the response from the server
ErrorThrows an error if the upload fails

Example

javascript
const url = 'https://api.example.com/upload';
const file = new Blob(['Flower data'], { type: 'text/plain' });
const body = { category: 'flora' };
const onProgress = (event) => console.log(`Upload progress: ${event.loaded / event.total * 100}%`);

fileProvider.uploadFileToURL(url, file, body, onProgress, 'botanical')
  .then(response => console.log('Upload response:', response))
  .catch(error => console.error('Upload failed:', error));

removeFile()

Removes a file from the server.

Arguments

NameTypeDescription
idstringThe ID of the file to be removed

Return and Throw

TypeDescription
Promise<any>Resolves with the response from the server
ErrorThrows an error if the removal fails

Example

javascript
const fileId = '123456789';

fileProvider.removeFile(fileId)
  .then(response => console.log('File removed successfully:', response))
  .catch(error => console.error('File removal failed:', error));

Generates a URL for accessing a file.

Arguments

NameTypeDescription
fileDoc{ fileName: string; format: string; tag: string }File document object
overrideUrlstring (optional)Optional URL to override the default
rootPathstring (optional)Root path for the file, defaults to "assets"

Return and Throw

TypeDescription
stringThe generated URL for the file

Example

javascript
const fileDoc = {
  fileName: 'city_map.jpg',
  format: 'images',
  tag: 'maps'
};

const fileUrl = fileProvider.getFileLink(fileDoc);
console.log('File URL:', fileUrl);

getFileDoc()

Retrieves a file document by its ID and user ID.

Arguments

NameTypeDescription
idstringThe ID of the file
userIdstringThe ID of the user who owns the file

Return and Throw

TypeDescription
Promise<FileDocument>Resolves with the file document
ErrorThrows an error if the file document cannot be found

Example

javascript
const fileId = '987654321';
const userId = 'user123';

fileProvider.getFileDoc(fileId, userId)
  .then(fileDoc => console.log('File document:', fileDoc))
  .catch(error => console.error('Failed to retrieve file document:', error));

getFileDocsByTag()

Retrieves file documents by tag and user ID.

Arguments

NameTypeDescription
tagstringThe tag to search for
userIdstringThe ID of the user who owns the files

Return and Throw

TypeDescription
Promise<FileDocument[]>Resolves with an array of file documents
ErrorThrows an error if the file documents cannot be found

Example

javascript
const tag = 'flowers';
const userId = 'user456';

fileProvider.getFileDocsByTag(tag, userId)
  .then(fileDocs => console.log('File documents:', fileDocs))
  .catch(error => console.error('Failed to retrieve file documents:', error));