2019-06-10 06:13:55 +12:00
|
|
|
const Service = require('../service.js');
|
|
|
|
|
|
|
|
class Storage extends Service {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List Files
|
|
|
|
*
|
2019-10-09 21:40:02 +13:00
|
|
|
* Get a list of all the user files. You can use the query params to filter
|
|
|
|
* your results. On admin mode, this endpoint will return a list of all of the
|
|
|
|
* project files. [Learn more about different API modes](/docs/modes).
|
2019-06-10 06:13:55 +12:00
|
|
|
*
|
|
|
|
* @param string search
|
|
|
|
* @param number limit
|
|
|
|
* @param number offset
|
|
|
|
* @param string orderType
|
|
|
|
* @throws Exception
|
|
|
|
* @return {}
|
|
|
|
*/
|
|
|
|
async listFiles(search = '', limit = 25, offset = 0, orderType = 'ASC') {
|
|
|
|
let path = '/storage/files';
|
|
|
|
|
2019-10-20 06:21:36 +13:00
|
|
|
return await this.client.call('get', path, {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
},
|
|
|
|
{
|
2019-06-10 06:13:55 +12:00
|
|
|
'search': search,
|
|
|
|
'limit': limit,
|
|
|
|
'offset': offset,
|
|
|
|
'orderType': orderType
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create File
|
|
|
|
*
|
2019-10-09 21:40:02 +13:00
|
|
|
* Create a new file. The user who creates the file will automatically be
|
|
|
|
* assigned to read and write access unless he has passed custom values for
|
|
|
|
* read and write arguments.
|
2019-06-10 06:13:55 +12:00
|
|
|
*
|
|
|
|
* @param File files
|
|
|
|
* @param array read
|
|
|
|
* @param array write
|
|
|
|
* @param string folderId
|
|
|
|
* @throws Exception
|
|
|
|
* @return {}
|
|
|
|
*/
|
2019-10-22 07:03:06 +13:00
|
|
|
async createFile(files, read, write, folderId = '') {
|
2019-06-10 06:13:55 +12:00
|
|
|
let path = '/storage/files';
|
|
|
|
|
2019-10-20 06:21:36 +13:00
|
|
|
return await this.client.call('post', path, {
|
|
|
|
'content-type': 'multipart/form-data',
|
|
|
|
},
|
|
|
|
{
|
2019-06-10 06:13:55 +12:00
|
|
|
'files': files,
|
|
|
|
'read': read,
|
|
|
|
'write': write,
|
|
|
|
'folderId': folderId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get File
|
|
|
|
*
|
2019-10-09 21:40:02 +13:00
|
|
|
* Get file by its unique ID. This endpoint response returns a JSON object
|
|
|
|
* with the file metadata.
|
2019-06-10 06:13:55 +12:00
|
|
|
*
|
|
|
|
* @param string fileId
|
|
|
|
* @throws Exception
|
|
|
|
* @return {}
|
|
|
|
*/
|
|
|
|
async getFile(fileId) {
|
|
|
|
let path = '/storage/files/{fileId}'.replace(new RegExp('{fileId}', 'g'), fileId);
|
|
|
|
|
2019-10-20 06:21:36 +13:00
|
|
|
return await this.client.call('get', path, {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
},
|
|
|
|
{
|
2019-06-10 06:13:55 +12:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-29 00:37:13 +12:00
|
|
|
/**
|
|
|
|
* Update File
|
|
|
|
*
|
2019-10-09 21:40:02 +13:00
|
|
|
* Update file by its unique ID. Only users with write permissions have access
|
|
|
|
* to update this resource.
|
2019-08-29 00:37:13 +12:00
|
|
|
*
|
|
|
|
* @param string fileId
|
|
|
|
* @param array read
|
|
|
|
* @param array write
|
|
|
|
* @param string folderId
|
|
|
|
* @throws Exception
|
|
|
|
* @return {}
|
|
|
|
*/
|
2019-10-22 06:15:27 +13:00
|
|
|
async updateFile(fileId, read, write, folderId = '') {
|
2019-08-29 00:37:13 +12:00
|
|
|
let path = '/storage/files/{fileId}'.replace(new RegExp('{fileId}', 'g'), fileId);
|
|
|
|
|
2019-10-20 06:21:36 +13:00
|
|
|
return await this.client.call('put', path, {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
},
|
|
|
|
{
|
2019-08-29 00:37:13 +12:00
|
|
|
'read': read,
|
|
|
|
'write': write,
|
|
|
|
'folderId': folderId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-10 06:13:55 +12:00
|
|
|
/**
|
|
|
|
* Delete File
|
|
|
|
*
|
2019-10-09 21:40:02 +13:00
|
|
|
* Delete a file by its unique ID. Only users with write permissions have
|
|
|
|
* access to delete this resource.
|
2019-06-10 06:13:55 +12:00
|
|
|
*
|
|
|
|
* @param string fileId
|
|
|
|
* @throws Exception
|
|
|
|
* @return {}
|
|
|
|
*/
|
|
|
|
async deleteFile(fileId) {
|
|
|
|
let path = '/storage/files/{fileId}'.replace(new RegExp('{fileId}', 'g'), fileId);
|
|
|
|
|
2019-10-20 06:21:36 +13:00
|
|
|
return await this.client.call('delete', path, {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
},
|
|
|
|
{
|
2019-06-10 06:13:55 +12:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-27 21:12:40 +12:00
|
|
|
* Get File for Download
|
2019-06-10 06:13:55 +12:00
|
|
|
*
|
2019-10-09 21:40:02 +13:00
|
|
|
* Get file content by its unique ID. The endpoint response return with a
|
|
|
|
* 'Content-Disposition: attachment' header that tells the browser to start
|
|
|
|
* downloading the file to user downloads directory.
|
2019-06-10 06:13:55 +12:00
|
|
|
*
|
|
|
|
* @param string fileId
|
|
|
|
* @throws Exception
|
|
|
|
* @return {}
|
|
|
|
*/
|
|
|
|
async getFileDownload(fileId) {
|
|
|
|
let path = '/storage/files/{fileId}/download'.replace(new RegExp('{fileId}', 'g'), fileId);
|
|
|
|
|
2019-10-20 06:21:36 +13:00
|
|
|
return await this.client.call('get', path, {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
},
|
|
|
|
{
|
2019-06-10 06:13:55 +12:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-27 21:12:40 +12:00
|
|
|
* Get File Preview
|
2019-06-10 06:13:55 +12:00
|
|
|
*
|
2019-10-09 21:40:02 +13:00
|
|
|
* Get file preview image. Currently, this method supports preview for image
|
|
|
|
* files (jpg, png, and gif), other supported formats, like pdf, docs, slides,
|
|
|
|
* and spreadsheets will return file icon image. You can also pass query
|
|
|
|
* string arguments for cutting and resizing your preview image.
|
2019-06-10 06:13:55 +12:00
|
|
|
*
|
|
|
|
* @param string fileId
|
|
|
|
* @param number width
|
|
|
|
* @param number height
|
|
|
|
* @param number quality
|
|
|
|
* @param string background
|
|
|
|
* @param string output
|
|
|
|
* @throws Exception
|
|
|
|
* @return {}
|
|
|
|
*/
|
|
|
|
async getFilePreview(fileId, width = 0, height = 0, quality = 100, background = '', output = '') {
|
|
|
|
let path = '/storage/files/{fileId}/preview'.replace(new RegExp('{fileId}', 'g'), fileId);
|
|
|
|
|
2019-10-20 06:21:36 +13:00
|
|
|
return await this.client.call('get', path, {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
},
|
|
|
|
{
|
2019-06-10 06:13:55 +12:00
|
|
|
'width': width,
|
|
|
|
'height': height,
|
|
|
|
'quality': quality,
|
|
|
|
'background': background,
|
|
|
|
'output': output
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-27 21:12:40 +12:00
|
|
|
* Get File for View
|
2019-06-10 06:13:55 +12:00
|
|
|
*
|
2019-10-09 21:40:02 +13:00
|
|
|
* Get file content by its unique ID. This endpoint is similar to the download
|
|
|
|
* method but returns with no 'Content-Disposition: attachment' header.
|
2019-06-10 06:13:55 +12:00
|
|
|
*
|
|
|
|
* @param string fileId
|
|
|
|
* @param string as
|
|
|
|
* @throws Exception
|
|
|
|
* @return {}
|
|
|
|
*/
|
|
|
|
async getFileView(fileId, as = '') {
|
|
|
|
let path = '/storage/files/{fileId}/view'.replace(new RegExp('{fileId}', 'g'), fileId);
|
|
|
|
|
2019-10-20 06:21:36 +13:00
|
|
|
return await this.client.call('get', path, {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
},
|
|
|
|
{
|
2019-06-10 06:13:55 +12:00
|
|
|
'as': as
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Storage;
|