Links

File Handling

You can create, get, and delete files and attach them to messages and posts

File Model

Name
Data Type
Description
fileId
String
Root file key on cloud storage
fileUrl
String
HTTP link for file download
type
enum
File type
createdAt
String($date-time)
Date/time when a file is uploaded
updatedAt
String($date-time)
Date/time when a file is updated
attributes
Object
Information about the file

Create Files

Before you can attach files and images in messages, posts, user and community avatars, you need to create the file first using the createFile method.
import { FileRepository, LoadingStatus } from '@amityco/js-sdk';
const liveObject = FileRepository.createFile({
file, // https://developer.mozilla.org/en-US/docs/Web/API/File
onProgress: ({ currentFile, currentPercent }) => {
},
});
liveObject.on('loadingStatusChanged', ({ newValue }) => {
if (newValue === LoadingStatus.Loaded) {
console.log('The file is uploaded', liveObject.model);
}
});
liveObject.on('dataError', (error) => {
console.error('can not upload the file', error);
});
// upload video
const liveObject = FileRepository.createVideo({
file,
onProgress: ({ currentFile, currentPercent }) => {
},
});

Requirements for Images:

  • Supported image types are JPG and PNG.
  • The maximum file size of the image is 1 GB.
Refer to the file model for the description of the response after a successful file creation. If an error is encountered while creating the file, it will return the following errors:
//Attached file payload is too large
{
"status": "error",
"code": 500000,
"message": "Payload too large."
}
// Unexpected error
{
"status": "error",
"code": 500000,
"message": "Unexpected error"
}

Get Files

You can get the file information using the getFile method. Provide the fileId of the file that you want to get. The fileId is retrieved from the response after successfully creating the file.
import { FileRepository } from '@amityco/js-sdk';
let file;
const liveObject = FileRepository.getFile(fileId);
liveObject.on('dataUpdated', (updatedModel) => {
file = updatedModel;
});
liveObject.on('dataError', (error) => {
console.error('Can not get the file', error);
});
file = liveObject.model;
If a file is successfully retrieved, the response will contain the file details. Refer to the file model description for the description of the response after a successful file query. If the file is not found, it will return the following error:
//Resource Not Found error
{
"status": "error",
"code": 400400,
"message": "Resource Not Found."
}

Delete File

To delete a file, use the deleteFile method and pass the fileId of the file that you want to delete as the parameter. The fileId is retrieved from the response after successfully creating the file.
import { FileRepository } from '@amityco/js-sdk';
const success = await FileRepository.deleteFile(fileId);
The response will return true if the file deletion is successful.