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

Model Description

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 in messages and posts, you need to create the file first using the createFile method.
1
import { createFile, createQuery, runQuery } from '@amityco/ts-sdk';
2
3
const data = new FormData();
4
data.append(‘file’, fileObject);
5
6
// add this line only if you're using React Native
7
// data.getAll = data.getParts;
8
9
const query = createQuery(createFile, data);
10
11
runQuery(query);
Copied!
For React Native, getAll function is not available in FormData polyfill, thus, you need to call the getParts function as a workaround. You may refer to our sample code.
Refer to the file model for the structure and information of the response after a successful file creation.
If an error is encountered while creating the file, it will return the following errors:
1
//Attached file payload is too large
2
{
3
"status": "error",
4
"code": 500000,
5
"message": "Payload too large."
6
}
7
8
// Unexpected error
9
{
10
"status": "error",
11
"code": 500000,
12
"message": "Unexpected error"
13
}
Copied!

Get files

You can get the file information using the getFile method. Provide the fileId of the file that you want to get.
1
import { getFile, createQuery, runQuery } from '@amityco/ts-sdk';
2
3
const query = createQuery(getFile, 'fileId');
4
5
runQuery(query);
Copied!
If a file is successfully retrieved, the response will contain the file details. Refer to the file model for the structure and information of the response after a successful file query. If the file is not found, it will return the following error:
1
//Resource Not Found error
2
{
3
"status": "error",
4
"code": 400400,
5
"message": "Resource Not Found."
6
}
Copied!

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.
1
import { deleteFile, createQuery, runQuery } from '@amityco/ts-sdk';
2
3
const query = createQuery(deleteFile, 'fileId');
4
5
runQuery(query);
Copied!
The response will return true if the file deletion is successful.
1
"data": {
2
"success": true
3
}
Copied!
Otherwise, if an error is encountered during the deletion, it will return the following errors:
1
//Permission denied error
2
{
3
"status": "error",
4
"code": 400301,
5
"message": "Permission denied"
6
}
7
8
Resource Not Found error
9
{
10
"status": "error",
11
"code": 400400,
12
"message": "Resource Not Found."
13
}
14
15
//Passed the wrong parameters error
16
{
17
"status": "error",
18
"code": 500000,
19
"message": "Parameters error."
20
}
Copied!
Last modified 1mo ago