FMPFile

Description

The FMPFile class is used to work with files.

class FMPFile

Properties

Property Description

remotePath

The path to server file or directory.
var remotePath: String { get }
localPath The path to file or directory in application local directory relative to the Documents folder.
var localPath: String { get }
mount Name of storage connection on server.
var mount: String { get }
type Type - file or directory.
var type: FMPFile.Type { get }
name File or directory name.
var name: String { get }
depth Directory depth.
var depth: Int { get }
children List of child elements.
var children: [FMPFile]? { get }

Methods

Method Description

ls(completion:)

Get directory contents. It checks recursively the directory contents and refreshes the list of children child elements. It returns the list of child elements for the directory, and the empty list for the file.
func ls(completion: @escaping FMPFilesHandler)

Parameters:

  • completion. Closing expression containing method execution result.

download(completion:) Load file or directory contents from server to device. It downloads file or directory contents from server with the specified depth and saves it at the specified path in the application local directory on the device. If the key property of the FMPFile instance is not empty, files are encrypted with this key. It is recommended to refresh the list of children child elements using the ls(completion:) method before calling the method.
func download(completion: @escaping FMPRequestResponseHandler)

Parameters:

  • completion. Closing expression containing method execution result.

upload(completion:) Upload file from device to server. It uploads the file from the specified application local directory on the device to server. If the key property of the FMPFile instance is not empty, files are decrypted with this key before upload. It returns error for the directory.
func upload(completion: @escaping FMPRequestResponseHandler)

Parameters:

  • completion. Closing expression containing method execution result.

deleteRemote(completion:) Delete file on server. It returns error for the directory.
func deleteRemote(completion: @escaping FMPRequestResponseHandler)

Parameters:

  • completion. Closing expression containing method execution result.

loadMeta(completion:) Request file metadata from server. It returns error for the directory.
func loadMeta(completion: @escaping FMPFileMetaHandler)

Parameters:

  • completion. Closing expression containing method execution result.

isExist(completion:) Request to check if file exists on server. It returns error for the directory.
func isExist(completion: @escaping FMPRequestResponseHandler)

Parameters:

  • completion. Closing expression containing method execution result.

search(_:caseInsensitive:) Recursive search of files or directories by name. It executes recursive search in child elements by file or directory name. The search condition is whether the name parameter is contained in the file or directory name. It is recommended to refresh the list of children child elements using the ls(completion:) method before calling this method.
func search(_ name: String, caseInsensitive: Bool = false) -> [FMPFile]

Parameters:

  • name. Condition of search by file or directory name.

  • caseInsensitive. If the value is true, the search is case-insensitive. If the value is false, the search is case-sensitive.

search(_:) Recursive search of files or directories by name. It executes recursive search in child elements by file or directory name. The search condition is whether file or directory name corresponds to a regular expression. It is recommended to refresh the list of children child elements using the ls(completion:) method before calling this method.
func search(_ regex: NSRegularExpression) -> [FMPFile]

Parameters:

  • regex. A regular expression for searching by file or directory name.

encryptToFile(at:) Encryption of application local directory file to file.
func encryptToFile(at path: String) -> Bool

Parameters:

  • path. The path to the encrypted file in the application local directory relative to the Documents folder.

decryptToFile(at:) Decrypt application local directory file to file.
func decryptToFile(at path: String)

Parameters:

  • path. The path to the decrypted file in the application local directory relative to the Documents folder.

encryptToData() Encryption of application local directory file to byte buffer. It returns the encrypted byte buffer.
func encryptToData() -> Data
decryptToData() Decryption of application local directory file to byte buffer. It returns the decrypted byte buffer.
func decryptToData() -> Data
 
decryptToString() Decryption of application local directory file to string. It returns the decrypted string.
func decryptToString() -> String
deleteLocal() Delete file or folder from the application local directory.
func deleteLocal() throws
copy() Get builder class instance for copying FMPFile. It returns the instance of the FMPFile.Builder builder class.
func copy() -> FMPFile.Builder

Examples

Get directory contents:

// Build an instance of the FMPFile class. For details see examples for FMPFile.Builder.
let folder: FMPFile = fmp.file.isDirectory().build()
  
// Get list of child elements
file.ls() { (success, error, files) in
    if success {
        // Display the obtained list of child element names
        print(files.map {$0.name})
    } else {
        // Display error code and description
        print(error?.code, error?.description)
    }
}

Load file or directory contents from server to device:

// Build an instance of the FMPFile class. For details see examples for FMPFile.Builder.
let file: FMPFile = fmp.file.build()
// Download file
file.download { (response) in
    if response.success {
        // Display server response
        print(response.result)
    } else {
        // Display error code and description
        print(response.error?.code, response.error?.description)
    }
}

Upload file from device to server:

// Build an instance of the FMPFile class. For details see examples for FMPFile.Builder.
let file: FMPFile = fmp.file.build()
  
// Upload file
file.upload { (response) in
    if response.success {
        // Display server response
        print(response.result)
    } else {
        // Display error code and description
        print(response.error?.code, response.error?.description)
    }
}

Delete file from server:

// Build an instance of the FMPFile class. For details see examples for FMPFile.Builder.
let file: FMPFile = fmp.file.build()
  
// Upload file
file.deleteRemote(completion: { (response) in
    if response.success {
        // Display server response
        print(response.result)
    } else {
        // Display error code and description
        print(response.error?.code, response.error?.description)
    }
}

Request file metadata from server:

// Build an instance of the FMPFile class. For details see examples for FMPFile.Builder.
let file: FMPFile = fmp.file.build()
  
// Download file metadata
file.loadMeta(completion: { (success, error, meta) in
    if success {
        // Display file size
        print(meta.size)
    } else {
        // Display error code and description
        print(error?.code, error?.description)
    }
}

Check if file exists on server:

// Build an instance of the FMPFile class. For details see examples for FMPFile.Builder.
let file: FMPFile = fmp.file.build()
 
// Check is file exists on server
file.isExist() { (response) in
    if response.success {
        // Display server response
        print(response.result)
    } else {
        // Display error code and description
        print(response.error?.code, response.error?.description)
    }
}

Recursive search of files or directories by name:

// Build an instance of the FMPFile class. For details see examples for FMPFile.Builder.
let folder: FMPFile = fmp.file.isDirectory().build()
  
// Search for elements by the "name" name without taking case into account
let files: [FMPFile] = folder.search("name")
  
// Build an instance of the NSRegularExpression class that corresponds to regular expression
let regex = try! NSRegularExpression(pattern: "[a-z0-9]name[a-z0-9]", options: [])
  
// Search for elements by name with the use of regular expression
let results: [FMPFile] = folder.search(regex)

Encrypt file in local directory to file:

// Build an instance of the FMPFile class. For details see examples for FMPFile.Builder.
let file: FMPFile = fmp.file.build()
  
// Encrypt file in the local directory to the specified file
let encryptToFileSuccess: Bool = file.encryptToFile(at: "encryptedLocalPath")

Decrypt file in local directory to file:

// Build an instance of the FMPFile class. For details see examples for FMPFile.Builder.
let file: FMPFile = fmp.file.build()
  
// Decrypt file in the local directory to the specified file
file.decryptToFile(at: "decryptedLocalPath")

Encrypt file in local directory to byte buffer:

// Build an instance of the FMPFile class. For details see examples for FMPFile.Builder.
let file: FMPFile = fmp.file.build()
  
// Encrypt file in the local directory to byte buffer
let encryptedData: Data = file.encryptToData()

Decrypt file in local directory to byte buffer:

// Build an instance of the FMPFile class. For details see examples for FMPFile.Builder.
let file: FMPFile = fmp.file.build()
  
// Decrypt file in the local directory to byte buffer
let decryptedData: Data = file.decryptToData()

Decrypt file in local directory to string:

// Build an instance of the FMPFile class. For details see examples for FMPFile.Builder.
let file: FMPFile = fmp.file.build()
  
// Decrypt file in the local directory to string
let decryptedString: String = file.decryptToString()

Delete file or folder from local directory:

// Build an instance of the FMPFile class. For details see examples for FMPFile.Builder.
let file: FMPFile = fmp.file.build()
  
// Delete file from the application local directory
 do {
    try file.deleteLocal()
} catch let error {
    // Display error description
    print(error.localizedDescription)
}

Copy FMPFile:

// Build an instance of the FMPFile class. For details see examples for FMPFile.Builder.
let file: FMPFile = fmp.file.build()
  
// Copy instance of the FMPUser class.
let fileCopy: FMPFile = file.copy().build()

See also:

FMPWrapper Framework | Classes