The object is used to work with files. It allows you to perform operations with files stored on the disk: to get links to download files, to save them, get access permissions, subscribe to app items, etc.

Type parameters

Hierarchy

Properties

Readonly code

code: string

App code of the URL’s target (the app’s code).

Readonly data

data: Based<Partial<FileData>>

Values of object’s fields.

Readonly fields

fields: Readonly<object>

Description of the object’s fields.

Readonly id

id: string

ID of the URL’s target.

Readonly namespace

namespace: string

Namespace of the URL target (the code of the workspace that the app belongs to).

Methods

addVersion

  • addVersion(name: string, body: ArrayBuffer, comment?: undefined | string): Promise<void>
  • The method creates a new version of a file.

    The method allows you to update the content of a file on the disk without creating a new file. The version created via this method is saved in the file’s history. The method is used in cases when the file’s body can be obtained from different sources or generated in a script. The comment field contains the description to the new version. In the example below you can see how to add a file version after uploading it from an external resource. As the comment, you can specify that the file is uploaded from an external resource. The context includes:

    • downloadUrl. A string that contains the URL from which the file is downloaded.
    • newName. The file’s new name.
    • file. The file stored on the disk.
      const  fileResponse = await fetch(Context.data.downloadUrl);
      const fileContent =  await fileResponse.arrayBuffer(); 
      const newName = Context.data.fileName;  
      await Context.data.file.addVersion(newName, fileContent, 'The file is  uploaded from an external resource'); 
      

    Parameters

    • name: string

      New name of the file.

    • body: ArrayBuffer

      File’s body.

    • Optional comment: undefined | string

      Comment for the new version.

    Returns Promise<void>

addVersionFromLink

  • addVersionFromLink(name: string, url: string, comment?: undefined | string): Promise<void>
  • The method creates a version by uploading a file via a link.

    The shortcut version of this method, FileItem.addVersion, uses a download link to add a new file version. The link allows uploading the file’ s content, and a new file version is created from it. You can add a comment to the new file version. In the example below, you can see how to add a new file version using a download link. The context includes:

    • downloadUrl. A String type variable storing the file’s download link.
    • newName. The file’s new name.
    • file. The file on the disk.
      const  newName = Context.data.fileName;
      await Context.data.file.addVersionFromLink( newName, Context.data.downloadUrl); 
      

    Parameters

    • name: string

      New name of the file.

    • url: string

      Public link to the file.

    • Optional comment: undefined | string

      Comment for the new version.

    Returns Promise<void>

delete

  • delete(): Promise<void>
  • Delete a file.

    The method moves the current file to the recycle bin for deletion.

        // Example of deleting all files created by a user (some  user_id) 
        const userID = '1a8286d3-a222-4b8b-a860-84f6505a708a'; 
         const searchFiles = await System.files.search(). 
             where(file => file .__createdBy.eq(userID)). 
             all(); 
       for (let i = 0; i <  searchFiles.length; i += 1) { 
         await searchFiles[i].delete(); 
       } 
    

    Returns Promise<void>

fetch

getDownloadUrl

  • The method returns a link to download a file’s content.

    The URL can be used to get the file’s content. Using this link, even anonymous users can download the file. The download link is active for one hour. You can set how the file will be displayed, as an attachment or inline. The default value of the dispositionType parameter is attachment .

    const url = await file.getDownloadUrl();
    

    Parameters

    Returns Promise<string>

getFileMD5Hash

  • getFileMD5Hash(): Promise<string>
  • The method gets the MD5 hash of the file.

    In the example, the context includes the file field. It’s a file from the system.

    const hash = await file.getFileMD5Hash();
    

    Returns Promise<string>

getPermissions

  • The method receives a file’s permissions.

    After a successful permissions request, you will get a TPermissions object. This object can be modified or analyzed for various permission checks.

    const perm = await file.getPermissions();
    

    Returns Promise<TPermissions>

getStaticDownloadUrl

  • The method returns a permanent link to download a file’s content.

    The returned link contains a redirect to the link with the file’s content. This link can be used only by authorized users who have sufficient permissions to download the file. The attachment type can be attachment orinline. By default, the dispositionType parameter is set to attachment. The link is permanent. It exists until the file is deleted in the system.

    // Get the file’s ID 
    const fileUUID = Context. data.fileID; 
    if(!fileUUID) { 
     return; 
    } 
    // Search for the file 
    const  file = await System.files.search().where(x => x.__id.eq(fileUUID)).first();  
    if(!file) { 
     return; 
    } 
    // Get a permanent link to the file 
    const url =  file.getStaticDownloadUrl(); 
    

    Parameters

    Returns string

getVersions

  • getVersions(offset?: undefined | number, limit?: undefined | number): Promise<FileVersionItem[]>
  • The method gets a list of a file’s versions.

    The example shows how to search for the first file version. The file’s ID is retrieved from the context. To get all versions, use the offset parameter. Versions are found in the descending order of the version number, from the most recent to older ones. The first version’s number is 1.

    // Get file ID 
    const fileUUID = Context.data.fileID;  
    if(!fileUUID) { 
     return; 
    } 
    // Search for a file 
    const file = await  System.files.search().where(x => x.__id.eq(fileUUID)).first(); 
    if(!file) {  
     return; 
    } 
    // Get all file versions; if there are no versions, an error  is returned 
    let allFileVersions: FileVersionItem[] = []; 
    let verCount = 0;  
    let offset = 0; 
    const limit = 20; 
    do { 
     const versions = await file. getVersions(limit, offset); 
     verCount = versions.length; 
     offset = offset +  verCount; 
     allFileVersions.push(...versions); 
    } while (verCount === limit)  
    if(!allFileVersions.length === 0) { 
     throw new Error('The file has no  versions'); 
    } 
    const firstVersion = allFileVersions.find(ver => ver.version  === 1); 
    // Get the file’s body 
    const url = await firstVersion. getDownloadUrl(); 
    const content = fetch(url); 
    

    Parameters

    • Optional offset: undefined | number

      Offset from the original search. The default value is 0.

    • Optional limit: undefined | number

      Maximum amount of retrieved entries. The default amount is 10. The maximum possible value is 10000.

    Returns Promise<FileVersionItem[]>

hasPermission

  • hasPermission(group: TPermissionOrgunit, type: PermissionType): Promise<boolean>
  • The method checks access permissions PermissionType.

    The method allows you to easily check whether it is possible to perform a PermissionType operation with a file. The method returns a flag. If its value is true, the user can perform the requested operation with the file. It is also possible to check access permissions for an org chart item.

    const user = Context.data.__createdBy;
    const canUpdate = await  file.hasPermission(user, PermissionType.UPDATE); 
    

    Parameters

    • group: TPermissionOrgunit

      The group or user whose permissions are checked.

    • type: PermissionType

      Operation type.

    Returns Promise<boolean>

move

  • move(directoryID: string): Promise<void>
  • Move a file to another directory.

    The method is used to move a file to the specified directory.

        // Example of searching for all .doc и .docx files in a directory and  moving them to another directory 
        const directoryID = '0520723d-5e32-4d37 -8eca-175853a2ec88'; 
        const searchFiles = await System.files.search().  
             where(file => file.directory.eq(directoryID)). 
             all();  
        // Create a new directory 
       const newDirectory = await System. directories.create('Documents', directoryID); 
       searchFiles!.forEach(async  file => { 
            let format = file.data.__name.split('.').reverse()[0];  
            if (format=='doc' || format=='docx') { 
                 await file. move(newDirectory.data.__id); 
            }; 
       }); 
    

    Parameters

    • directoryID: string

      ID of the directory that the file needs to be moved to. Pass the directory’s UUID. Otherwise, an error will occur.

    Returns Promise<void>

normalize

  • normalize(): void
  • Delete duplicate data in arrays.

    The method deletes duplicates in fields that store arrays of links to system objects (users, files, app items, or documents). For example, you can call this method after bulk editing data within an object.

    const  app1 = await Context.data.app1.fetch();
    const app2 = await Context.data.app2 .fetch(); 
    app1.data.executors.push(app2.data.executors); 
    app1.normalize();  
    // Now we need to go over the elements in the new array 
    app1.data.executors .forEach( ... ); 
    

    Returns void

rename

  • rename(newName: string): Promise<void>
  • Rename a file.

    The method is used to change the current file’s name.

      //  Example of renaming a file used in the context 
      const fileUUID = Context. data.fileID; 
      if(!fileUUID) { 
          return; 
      } 
      // Search for the  file 
      const file = await System.files.search().where(x => x.__id. eq(fileUUID)).first(); 
      if(!file) { 
          return; 
      } 
      await file. rename('New Name.txt'); 
    

    Parameters

    • newName: string

      New name of the file. Include the format in the name (newName.txt).

    Returns Promise<void>

setPermissions

  • The method sets access permissions for a file.

    This method is used when you need to change a file’s access settings by adding or deleting access permissions for a user, a group, or an org chart item. To add new access permissions, use the TPermissionValue object. To grant access to specific operations, use PermissionType. When a new TPermissions object is created, use the FileItem.setPermissions method to save the new access permissions.

    const user =  Context.data.__createdBy;
    const permissions = new Permissions([ 
     new  PermissionValue(user, [PermissionType.DELETE, PermissionType.READ]), 
    ]);  
    await file.setPermissions(perms); 
    

    Parameters

    Returns Promise<void>

subscribe

  • subscribe(...users: TUser[]): Promise<void>
  • The method allows you to subscribe users to a file.

    Subscribing to a file allows users to get notifications about changes made to the file, new versions, and other events.

    Parameters

    • Rest ...users: TUser[]

      Пользователи для подписки.

    Returns Promise<void>

unsubscribe

  • unsubscribe(...users: TUser[]): Promise<void>
  • The method allows you to unsubscribe users from a file.

    Unsubscribing from a file allows you to stop getting notifications about changes made to the file.

    Parameters

    • Rest ...users: TUser[]

      Пользователи для отписки.

    Returns Promise<void>