Last active
March 31, 2025 06:19
-
-
Save t-book/83488fcf3ff8a97d5cba70846d36e799 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ===> In fileutils.h | |
/** | |
* Reads the entire content of a file and returns it as a byte array. | |
* @param filePath The path to the file to be read | |
* @return The file content as a QByteArray | |
*/ | |
Q_INVOKABLE static QByteArray readFileContent(const QString &filePath); | |
/** | |
* Writes content to a file. | |
* @param filePath The path to the file to be written | |
* @param content The content to write to the file | |
* @return True if the write operation was successful, false otherwise | |
*/ | |
Q_INVOKABLE static bool writeFileContent(const QString &filePath, const QByteArray &content); | |
/** | |
* Gets detailed information about a file including content, MD5 hash and metadata. | |
* This is useful for file validation, caching, and efficient file handling in QML. | |
* @param filePath The path to the file | |
* @return A map containing file metadata and optionally its content | |
*/ | |
Q_INVOKABLE static QVariantMap getFileInfo(const QString &filePath); | |
// ===> In fileutils.cpp | |
QByteArray FileUtils::readFileContent(const QString &filePath) | |
{ | |
QByteArray content; | |
QFile file(filePath); | |
if (file.exists()) | |
{ | |
if (file.open(QIODevice::ReadOnly)) | |
{ | |
content = file.readAll(); | |
file.close(); | |
} | |
else | |
{ | |
qDebug() << QStringLiteral("Failed to read file content: %1").arg(file.errorString()); | |
} | |
} | |
else | |
{ | |
qDebug() << QStringLiteral("File does not exist: %1").arg(filePath); | |
} | |
return content; | |
} | |
bool FileUtils::writeFileContent(const QString &filePath, const QByteArray &content) | |
{ | |
QFile file(filePath); | |
// Ensure the directory exists | |
QFileInfo fileInfo(filePath); | |
QDir directory = fileInfo.dir(); | |
if (!directory.exists()) | |
{ | |
if (!directory.mkpath(".")) | |
{ | |
qDebug() << QStringLiteral("Failed to create directory for file: %1").arg(filePath); | |
return false; | |
} | |
} | |
if (file.open(QIODevice::WriteOnly)) | |
{ | |
qint64 bytesWritten = file.write(content); | |
file.close(); | |
if (bytesWritten != content.size()) | |
{ | |
qDebug() << QStringLiteral("Failed to write all data to file: %1").arg(filePath); | |
return false; | |
} | |
return true; | |
} | |
else | |
{ | |
qDebug() << QStringLiteral("Failed to open file for writing: %1").arg(file.errorString()); | |
return false; | |
} | |
} | |
QVariantMap FileUtils::getFileInfo(const QString &filePath) | |
{ | |
QVariantMap info; | |
QFile file(filePath); | |
if (file.exists()) | |
{ | |
QFileInfo fileInfo(filePath); | |
QMimeDatabase db; | |
info["exists"] = true; | |
info["fileName"] = fileInfo.fileName(); | |
info["filePath"] = fileInfo.absoluteFilePath(); | |
info["fileSize"] = fileInfo.size(); | |
info["lastModified"] = fileInfo.lastModified(); | |
info["suffix"] = fileInfo.suffix(); | |
info["mimeType"] = db.mimeTypeForFile(filePath).name(); | |
info["md5"] = fileChecksum(filePath, QCryptographicHash::Md5).toHex(); | |
if (file.open(QIODevice::ReadOnly)) | |
{ | |
info["content"] = file.readAll(); | |
file.close(); | |
} | |
} | |
else | |
{ | |
info["exists"] = false; | |
} | |
return info; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment