Class QfFileUtils
Inherits the following classes: QObject
Public Functions
| Type | Name |
|---|---|
| QfFileUtils (QObject * parent=nullptr) |
|
| ~QfFileUtils () = default |
Public Static Functions
| Type | Name |
|---|---|
| Q_INVOKABLE QString | absolutePath (const QString & filePath) Returns the absolute path of the folder containing the filePath . |
| Q_INVOKABLE void | addImageMetadata (const QString & imagePath, const QfGnssPositionInformation & positionInformation) |
| Q_INVOKABLE void | addImageStamp (const QString & imagePath, const QString & text, const QString & textFormat=QString(), Qgis::TextHorizontalAlignment horizontalAlignment=Qgis::TextHorizontalAlignment::Left, const QString & imageDecoration=QString()) |
| Q_INVOKABLE bool | copyFile (const QString & filePath, const QString & destinationFilePath, bool overwrite=false) |
| bool | copyRecursively (const QString & sourceFolder, const QString & destFolder, QgsFeedback * feedback=nullptr, bool wipeDestFolder=true) |
| Q_INVOKABLE QVariantMap | deleteFiles (const QStringList & filePaths) |
| Q_INVOKABLE QByteArray | fileChecksum (const QString & fileName, const QCryptographicHash::Algorithm hashAlgorithm) |
| Q_INVOKABLE QString | fileEtag (const QString & fileName, int partSize=8 *1024 *1024) |
| Q_INVOKABLE bool | fileExists (const QString & filePath) Returns true if the filePath exists (false if it's a directory) |
| Q_INVOKABLE QString | fileName (const QString & filePath, bool includeSuffix=true) Returns the filename of a filePath - if no file name exists it's empty. |
| Q_INVOKABLE qint64 | fileSize (const QString & filePath) Returns the size in bytes of the filePath , or -1 if it does not exist. |
| Q_INVOKABLE QString | fileSuffix (const QString & filePath) Returns the suffix (extension) |
| Q_INVOKABLE QVariantMap | getFileInfo (const QString & filePath, bool fetchContent=false) |
| Q_INVOKABLE bool | isDeletable (const QString & filePath) |
| Q_INVOKABLE bool | isImageMimeTypeSupported (const QString & mimeType) Returns TRUE if the provided mimetype is a supported image. |
| Q_INVOKABLE bool | isWithinProjectDirectory (const QString & filePath) |
| Q_INVOKABLE QString | mimeTypeName (const QString & filePath) Returns the mimetype of a filepath as string. |
| Q_INVOKABLE QString | nativeSeparatorsPath (const QString & filePath) Returns the filePath with '/' separators converted to separators that are appropriate for the underlying operating system. |
| Q_INVOKABLE QByteArray | readFileContent (const QString & filePath) |
| Q_INVOKABLE QString | representFileSize (qint64 bytes, bool decimalRepresentation=false) Returns a human-friendly size from bytes. |
| Q_INVOKABLE void | restrictImageSize (const QString & imagePath, int maximumWidthHeight) |
| Q_INVOKABLE QStringList | safeUnzip (const QString & zipFilename, const QString & dir, bool checkConsistency) |
| Q_INVOKABLE bool | safeZip (const QString & zipFilename, const QStringList & files, const QString & filesRoot, bool overwrite) |
| Q_INVOKABLE QString | sanitizeFilePath (const QString & filePath, const QString & replacement=QLatin1String("_")) Tries to convert the given filePath with path to a cross OS valid file name with path. |
| Q_INVOKABLE QString | sanitizeFilePathPart (const QString & filePathPart, const QString & replacement=QLatin1String("_")) Tries to convert the given filePathPart to a cross OS valid file name part. |
| QSet< QString > | sidecarFilesForPath (const QString & filePath) |
| bool | unzip (const QString & zipFilename, const QString & dir, QStringList & files, bool checkConsistency) |
| Q_INVOKABLE bool | writeFileContent (const QString & filePath, const QByteArray & content) |
| bool | zip (const QString & zipFilename, const QStringList & files, const QString & filesRoot, bool overwrite) |
Public Functions Documentation
function QfFileUtils
explicit QfFileUtils::QfFileUtils (
QObject * parent=nullptr
)
function ~QfFileUtils
QfFileUtils::~QfFileUtils () = default
Public Static Functions Documentation
function absolutePath
Returns the absolute path of the folder containing the filePath .
static Q_INVOKABLE QString QfFileUtils::absolutePath (
const QString & filePath
)
function addImageMetadata
static Q_INVOKABLE void QfFileUtils::addImageMetadata (
const QString & imagePath,
const QfGnssPositionInformation & positionInformation
)
Adds positioning metadata to a given image.
Parameters:
imagePaththe image pathpositionInformationthe GNSS position information used to add metadata details
function addImageStamp
static Q_INVOKABLE void QfFileUtils::addImageStamp (
const QString & imagePath,
const QString & text,
const QString & textFormat=QString(),
Qgis::TextHorizontalAlignment horizontalAlignment=Qgis::TextHorizontalAlignment::Left,
const QString & imageDecoration=QString()
)
Prints details to a given image.
Parameters:
imagePaththe image pathtextthe details textprojectan optional project from which custom stamping settings will be retrieved
function copyFile
static Q_INVOKABLE bool QfFileUtils::copyFile (
const QString & filePath,
const QString & destinationFilePath,
bool overwrite=false
)
Copies the file named filePath to destinationFilePath. Use the optional overwrite parameter to allow for the copying process to overwrite an already present destination file.
Returns:
True if the copying operation was successful
Note:
Platform restrictions apply: * On Android: Writing is only permitted within the app's internal storage or properly requested scoped storage locations. * On iOS: Writing is restricted to the app's sandbox. * Use QfPlatformUtilities.applicationDirectory() to get a safe write location.
function copyRecursively
static bool QfFileUtils::copyRecursively (
const QString & sourceFolder,
const QString & destFolder,
QgsFeedback * feedback=nullptr,
bool wipeDestFolder=true
)
function deleteFiles
static Q_INVOKABLE QVariantMap QfFileUtils::deleteFiles (
const QStringList & filePaths
)
Deletes a list of files with security validation. Only deletes files that pass isDeletable() checks and are within the project or cloud directories.
Parameters:
filePathsList of file paths to delete
Returns:
A map with file paths as keys and boolean success status as values
function fileChecksum
static Q_INVOKABLE QByteArray QfFileUtils::fileChecksum (
const QString & fileName,
const QCryptographicHash::Algorithm hashAlgorithm
)
Returns the checksum of a file. An empty QByteArray will be returned if it cannot be calculated.
Parameters:
fileNamefile name to get checksum ofhashAlgorithmhash algorithm (md5, sha1, sha256 etc)
Returns:
QByteArray checksum value
function fileEtag
static Q_INVOKABLE QString QfFileUtils::fileEtag (
const QString & fileName,
int partSize=8 *1024 *1024
)
Returns an Object Storage (S3) ETag of a file. An empty string will be returned if it cannot be calculated.
Parameters:
fileNamefile name to get checksum ofpartSizemaximum size used to divide the file content into parts
Returns:
QString Etag value
function fileExists
Returns true if the filePath exists (false if it's a directory)
static Q_INVOKABLE bool QfFileUtils::fileExists (
const QString & filePath
)
function fileName
Returns the filename of a filePath - if no file name exists it's empty.
static Q_INVOKABLE QString QfFileUtils::fileName (
const QString & filePath,
bool includeSuffix=true
)
function fileSize
Returns the size in bytes of the filePath , or -1 if it does not exist.
static Q_INVOKABLE qint64 QfFileUtils::fileSize (
const QString & filePath
)
function fileSuffix
Returns the suffix (extension)
static Q_INVOKABLE QString QfFileUtils::fileSuffix (
const QString & filePath
)
function getFileInfo
static Q_INVOKABLE QVariantMap QfFileUtils::getFileInfo (
const QString & filePath,
bool fetchContent=false
)
Gets detailed information about a file including MD5 hash and metadata. Optionally includes the file content when fetchContent is true. This is useful for file validation, caching, and efficient file handling in QML.
Parameters:
filePathThe path to the filefetchContentWhether to include the file content in the returned information (default: false)
Returns:
A map containing file metadata and optionally its content
function isDeletable
static Q_INVOKABLE bool QfFileUtils::isDeletable (
const QString & filePath
)
Checks if a file can be safely deleted. Security checks: * File must be within application or cloud directories * Only allows deletion of specific file types * Prevents deletion of folders, project files
Parameters:
filePathThe path to check for deletion eligibility
Returns:
True if the file can be safely deleted, false otherwise
function isImageMimeTypeSupported
Returns TRUE if the provided mimetype is a supported image.
static Q_INVOKABLE bool QfFileUtils::isImageMimeTypeSupported (
const QString & mimeType
)
function isWithinProjectDirectory
static Q_INVOKABLE bool QfFileUtils::isWithinProjectDirectory (
const QString & filePath
)
Checks if a file path is securely within the current project directory. Security measures: * Prevents directory traversal attacks using path normalization * Handles symbolic links that could escape project boundaries * Supports cross-platform path comparisons (Windows, macOS, Linux, Android, iOS) * Validates non-existent files safely for write operations * Prevents partial directory matching by enforcing complete path containment
Parameters:
filePathThe path to check
Returns:
True if the path is safely within the current project directory
function mimeTypeName
Returns the mimetype of a filepath as string.
static Q_INVOKABLE QString QfFileUtils::mimeTypeName (
const QString & filePath
)
function nativeSeparatorsPath
Returns the filePath with '/' separators converted to separators that are appropriate for the underlying operating system.
static Q_INVOKABLE QString QfFileUtils::nativeSeparatorsPath (
const QString & filePath
)
function readFileContent
static Q_INVOKABLE QByteArray QfFileUtils::readFileContent (
const QString & filePath
)
Reads the entire content of a file and returns it as a byte array.
Parameters:
filePathThe path to the file to be read
Returns:
The file content as a QByteArray
function representFileSize
Returns a human-friendly size from bytes.
static Q_INVOKABLE QString QfFileUtils::representFileSize (
qint64 bytes,
bool decimalRepresentation=false
)
function restrictImageSize
static Q_INVOKABLE void QfFileUtils::restrictImageSize (
const QString & imagePath,
int maximumWidthHeight
)
Insures that a given image's width and height are restricted to a maximum size.
Parameters:
imagePaththe image file pathmaximumWidthHeightthe maximum width and height size
function safeUnzip
static Q_INVOKABLE QStringList QfFileUtils::safeUnzip (
const QString & zipFilename,
const QString & dir,
bool checkConsistency
)
function safeZip
static Q_INVOKABLE bool QfFileUtils::safeZip (
const QString & zipFilename,
const QStringList & files,
const QString & filesRoot,
bool overwrite
)
function sanitizeFilePath
Tries to convert the given filePath with path to a cross OS valid file name with path.
static Q_INVOKABLE QString QfFileUtils::sanitizeFilePath (
const QString & filePath,
const QString & replacement=QLatin1String("_")
)
function sanitizeFilePathPart
Tries to convert the given filePathPart to a cross OS valid file name part.
static Q_INVOKABLE QString QfFileUtils::sanitizeFilePathPart (
const QString & filePathPart,
const QString & replacement=QLatin1String("_")
)
function sidecarFilesForPath
static QSet< QString > QfFileUtils::sidecarFilesForPath (
const QString & filePath
)
Returns the existing files accompanying filePath which carry no meaning on their own and must therefore follow it wherever it goes - when shared, exported, moved into an attachment folder or deleted.
This extends QgsFileUtils::sidecarFilesForPath(), which resolves companion files by asking the registered data providers and so covers datasets only, with additional files that are relevant to QField.
function unzip
static bool QfFileUtils::unzip (
const QString & zipFilename,
const QString & dir,
QStringList & files,
bool checkConsistency
)
Unzip a zip file in an output directory.
Parameters:
zipThe zip filenamedirThe output directoryfilesThe absolute path of unzipped filescheckConsistencyPerform additional stricter consistency checks on the archive, and error if they fail (since QGIS 3.30)restricted
Returns:
FALSE if the zip filename does not exist, the output directory does not exist or is not writable.
function writeFileContent
static Q_INVOKABLE bool QfFileUtils::writeFileContent (
const QString & filePath,
const QByteArray & content
)
Writes content to a file.
Parameters:
filePathThe path to the file to be writtencontentThe content to write to the file
Returns:
True if the write operation was successful, false otherwise
Note:
Platform restrictions apply: * On Android: Writing is only permitted within the app's internal storage or properly requested scoped storage locations. * On iOS: Writing is restricted to the app's sandbox. * Use QfPlatformUtilities.applicationDirectory() to get a safe write location.
function zip
static bool QfFileUtils::zip (
const QString & zipFilename,
const QStringList & files,
const QString & filesRoot,
bool overwrite
)
Zip the list of files in the zip file. If the zip file already exists (and overwrite is false) or is empty, an error is returned. If an input file does not exist, an error is also returned.
Parameters:
zipThe zip filenamefilesThe absolute path to files to embed within the zipoverwriteSet to true to allow overwriting existing files
Returns:
True if the operation was successful
The documentation for this class was generated from the following file src/core/utils/qffileutils.h