Skip to main content

Working With Files

This guide describes methods for working with files.

Files operations#

This chapter describes methods for creating and modifying files.

Initialization#

import CLFiles from '@cloudike/web_files';
let filesApp;
const options = {  logLevel: 'error' // 'trace' | 'debug' | 'info' | 'warn' | 'error'};
const config = {  authToken: '', // user auth token optional  baseUrl: '', // api base url required  userAgent: '', // user agent name required  wsConfig: { //    sockjs: '', // sockjs url optional    sockjs_for_shares: "", // sockjs for shares url optional      websocket_event_groups: [      "fs",      "photos",      "family"    ], // websocket event groups optional, there are three groups: fs, photos, family    jwt_token: '', // jwt token optional    share_id: '', // share id optional    auth_by_first_message: true // auth by first message optional  }}
try {  filesApp = CLFiles.init(options, config);} catch (error) {  console.log('error', error);}

Get folder content#

The following code example demonstrates how to get a list of all nodes.

const params: IGetFsNodeChildrenSchema = {};const defaultPageSize: number = 100;const delay: number = 300;const opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp  .folderOperationsService  .getRootDirAllContent(params, defaultPageSize, delay, opts)  .then((nodes) => {    console.log('nodes', nodes); // IFsNodeSchema[]  })  .catch((error) => {  });

The code example provided below demonstrates the procedure to retrieve the paginator for folder content as well as to fetch the contents of a particular page.

const pageSize: number = 20;const params: IGetFsNodesSchema = {};const opts: CustomRequestConfig = { signal: '<AbortSignal>' };folderContentPaginator = filesApp  .folderOperationsService  .getFolderContentPaginator(pageSize, params, opts);// To get new page of nodesfolderContentPaginator  .next()  .then(({ data }) => {    const { _embedded: { nodes } } = data;    return folderContentPaginator.next();  })  .catch((error) => {  });

The following code example demonstrates how to retrieve the data from the root folder.

const opts: CustomRequestConfig = { signal: '<AbortSignal>' };const folderContentPaginator = filesApp.folderOperationsService  .getRootNode(opts)  .then(({ data }) => {    console.log('data', data); // IFsNodeSchema  })  .catch((error) => {  });

The following code example demonstrates how to get a list of root-level nodes.

const params: IGetFsNodeChildrenSchema = {};const opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp  .folderOperationsService  .getRootContent(params, opts)  .then(({ data }) => {    const {      _embedded: { nodes },    } = data;  })  .catch((error) => {  });

The following code example demonstrates how to get a list of nodes.

const params: IGetFsNodesSchema = {};const opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp  .folderOperationsService  .getFolderContent(params, opts)  .then(({ data }) => {    const {      _embedded: { nodes },    } = data;  })  .catch((error) => {  });

The following code example demonstrates how to get a list of all nodes in a specific folder.

const params: IGetFsNodesSchema = {  parent_id: '', // parent id of the folder};const defaultPageSize: number = 100;const delay: number = 300;const opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp  .folderOperationsService  .getAllFolderContent(params, deffaultPageSize, delay, opts)  .then((nodes) => {    console.log('nodes', nodes); // IFsNodeSchema[]  })  .catch((error) => {  });

Create new nodes#

The following code example demonstrates how to create a new folder.

const name = 'new folder';const parentId = '';const opts: CustomRequestConfig = { signal: '<AbortSignal>' };const extraParams: ICreateFsNodesSchema = {};filesApp.folderOperationsService  .createNewFolder(name, parentId, extraParams, opts)  .then(({ data }) => {    const newNode = data; //IFsNodeSchema  })  .catch((error) => {  });

Rename node#

The code below demonstrates how to rename a node using a given nodeId.

const nodeId = '';const name = 'new name';const opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp  .folderOperationsService  .renameNode(nodeId, name, opts)  .then(({ data }) => {    const updatedNode = data; //IFsNodeSchema  })  .catch((error) => {  });

Moving nodes#

The following code example demonstrates how to move a node with a given nodeId and parentId.

const nodeId = '';const parentId = '';const extraParams: UpdateNodeExtraParamsType = {};const opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp  .folderOperationsService  .moveNode(nodeId, parentId, extraParams, opts)  .then(({ data }) => {    const updatedNode = data; //IFsNodeSchema  })  .catch((error) => {  });

The following code example demonstrates how to move several nodes with given ids to the trash bin.

const nodeIds = ['node1', 'node2'];const opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp  .folderOperationsService  .moveNodesToTrash(nodeIds, opts)  .then(({ data: IFsNodeSchema }) => {    const updatedNode = data;  })  .catch((error) => {  });

The following code example demonstrates how to move several nodes with given IDs to the trash bin using the batching API.

const pageSize: number = '<number>';const delay: number = '<delay>';const opts: CustomRequestConfig = { signal: '<AbortSignal>' };const nodeIds = ['node1', 'node2'];filesApp  .folderOperationsService  .batchMovingNodesToTrash(nodeIds, pageSize, delay, opts)  .then((opsResult: IBatchedOpsSchema[]) => {    const result = opsResult;  })  .catch((error) => {  });

Copying nodes#

The following code example demonstrates the copying of a directory.

const params: IDestinationParams = {  source: '', // link to source folder  destination: {    parent_id: '',  },};const checkStatusDelay: number = 300;const opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp  .folderOperationsService  .copyDir(params, checkStatusDelay, opts)  .then(({ data }) => {    console.log('data', data); //IDirCopyTaskSchema  })  .catch((error) => {  });

The following code example demonstrates the copying of a file using its file_object.

const srcNodeId: string = '';const parentId: string = '';const extraParams: CopyFileExtraParamsType = {};const opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp.folderOperationsService  .copyFileByFileObject(srcNodeId, parentId, extraParams, opts)  .then(({ data }) => {    console.log('data', data); //IFsNodeSchema  })  .catch((error) => {  });

The following code example demonstrates the copying of a file by its source.

const srcNodeId: string = '';const parentId: string = '';const extraParams: CopyFileExtraParamsType = {};const opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp  .folderOperationsService  .copyFileByFileSource(srcNodeId, parentId, extraParams, opts)  .then(({ data }) => {    console.log('data', data); //IFsNodeSchema  })  .catch((error) => {  });

Uploading nodes#

The following code example demonstrates how to upload a new version of the source file with a given id.

const distNodeId: string = ''; // node id of source fileconst file: IFileObject = {};const opts: CustomRequestConfig = { signal: '<AbortSignal>' };const params: IUploderProxyParams = {};filesApp  .folderOperationsService  .uploadNewFileVersion(nodeIds, file, params, opts)  .then(({ data }) => {    return data; //IVersionSchema  })  .catch((error) => {  });

The following code example demonstrates how to change the current version of the source file with the given ID.

const distNodeId: string = '<node_id>'; // node id of source fileconst newVersionId: string = '<version_id>'; // versionId of an old file versionconst opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp  .folderOperationsService  .changeCurrentVersionByVersionId(distNodeId, newVersionId, opts)  .then(({ data }) => {    return data; //IVersionSchema  })  .catch((error) => {  });

The following code example demonstrates how to change the current version of the source file to a given version.

const distNodeId: string = '<node_id>'; // node id of source fileconst newVersion: string = '<version>'; // IVersionSchemaconst opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp  .folderOperationsService  .changeCurrentVersionByVersion(distNodeId, newVersion, opts)  .then(({ data: IVersionSchema }) => {    return data;  })  .catch((error) => {  });

The following code example demonstrates the uploading of files.

const fileList: IFileObject[] = [fileObject1, fileObject2];const params: IUploderProxyParams = { parent_id: '' };const opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp  .folderOperationsService  .uploadFileItems(fileList, params, opts)  .then((uploadedFiles) => {    console.log('uploadedFiles', uploadedFiles); //IFsNodeSchema[]  })  .catch((error) => {  });

The following code example demonstrates how to upload files using UploaderList. There are two types of events for UploaderList:

  • update
  • error The callback for the 'update' event is called with an object {list, state}, where 'list' is the content of the UploaderList and 'state' is the state of the UploaderList instance.
// if you have to upload a new version of a node with specific nodeId// add target_node_id as the nodeId to FileObject instanceconst fileList: IFileObject[] = [fileObject1, fileObject2];const params: IUploaderProxyParams = { parent_id: '' };
async function uploadAndManageFiles() {  const uploaderList = await filesApp.folderOperationsService(fileList, {}, params);
  // to get list updates you need to subscribe to 'update' event  uploaderList.on('update', async ({ list, state }) => {    // get list of files    const items: Array<File | UploaderBase> = list;    // state of file list    const listState: IUploaderListState = state;    if (items && items.length > 0) {      // to remove item from item list      const item = items[0];      await uploaderList.remove(item);      // to restart uploading of any item which haven't been uploaded successfully.      await uploaderList.restart(item);      // to resume uploading of photos      await uploaderList.resume();      // to stop uploading photos      uploaderList.cancel();      // to clear file list      uploaderList.clear();    }  });
  // to catch list updates error you need to subscribe to 'error' event  uploaderList.on('error', (error) => {    const uploaderListError = error; // UploaderListError  });}
try {  uploadAndManageFiles();} catch (error) {  console.log('error', error);}

Downloading nodes#

The following code example demonstrates how to get the download link for a file.

const nodeId: string = '';const opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp  .folderOperationsService  .getDownloadLink(nodeId, opts)  .then((link) => {    console.log('link', link); // link to the file  })  .catch((error) => {  });

The following code example demonstrates how to get the download link for a set of nodes.

const nodeIds = ['node1', 'node2'];const opts: CustomRequestConfig = { signal: '<AbortSignal>' };filesApp  .folderOperationsService  .getZipStreamLink(nodeIds, opts)  .then((link) => {    console.log('link', link); // link to the zip archive  })  .catch((error) => {  });

The following code example demonstrates how to get the content of a search pagination.

const pageSize: number = 20;const params: IPostSearchFiles = {};const opts: CustomRequestConfig = { signal: '<AbortSignal>' };const searchContentPaginator =  filesApp.folderOperationsService.getSearchContentPaginator(pageSize, params, opts);// To get new pagereturn searchContentPaginator.next()  .then(({ data }) => {    const { _embedded: { nodes } } = data;    return folderContentPaginator.next()  })