aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSaturneric <[email protected]>2022-02-13 08:40:11 +0000
committerSaturneric <[email protected]>2022-02-13 08:40:11 +0000
commit3c889bd876460b37daddb98b2816e85495ed4426 (patch)
tree24e539a14a84cfc10fa39f0aa79a56286ff64632 /src
parent<fix>(ci): Minor bug in modifying OS labels in ci files (diff)
downloadGpgFrontend-3c889bd876460b37daddb98b2816e85495ed4426.tar.gz
GpgFrontend-3c889bd876460b37daddb98b2816e85495ed4426.zip
<feat, refactor>(ui, core): Transfer file hash calculation logic to core
1. show file size
Diffstat (limited to 'src')
-rw-r--r--src/core/function/FileOperator.cpp52
-rw-r--r--src/core/function/FileOperator.h13
-rw-r--r--src/ui/widgets/FilePage.cpp49
3 files changed, 65 insertions, 49 deletions
diff --git a/src/core/function/FileOperator.cpp b/src/core/function/FileOperator.cpp
index e721fc7e..d0a3df23 100644
--- a/src/core/function/FileOperator.cpp
+++ b/src/core/function/FileOperator.cpp
@@ -65,3 +65,55 @@ bool GpgFrontend::FileOperator::WriteFileStd(
return WriteFile(QString::fromStdString(file_name.string()),
QByteArray::fromStdString(data));
}
+
+std::string GpgFrontend::FileOperator::CalculateHash(
+ const std::filesystem::path& file_path) {
+ // Returns empty QByteArray() on failure.
+ QFileInfo info(QString::fromStdString(file_path.string()));
+ std::stringstream ss;
+
+ if (info.isFile() && info.isReadable()) {
+ ss << "[#] " << _("File Hash Information") << std::endl;
+ ss << " " << _("filename") << _(": ")
+ << file_path.filename().string().c_str() << std::endl;
+
+
+ QFile f(info.filePath());
+ f.open(QFile::ReadOnly);
+ auto buffer = f.readAll();
+ ss << " " << _("file size(bytes)") << _(": ")
+ << buffer.size() << std::endl;
+ f.close();
+ if (f.open(QFile::ReadOnly)) {
+ auto hash_md5 = QCryptographicHash(QCryptographicHash::Md5);
+ // md5
+ hash_md5.addData(buffer);
+ auto md5 = hash_md5.result().toHex().toStdString();
+ LOG(INFO) << "md5" << md5;
+ ss << " "
+ << "md5" << _(": ") << md5 << std::endl;
+
+ auto hash_sha1 = QCryptographicHash(QCryptographicHash::Sha1);
+ // sha1
+ hash_sha1.addData(buffer);
+ auto sha1 = hash_sha1.result().toHex().toStdString();
+ LOG(INFO) << "sha1" << sha1;
+ ss << " "
+ << "sha1" << _(": ") << sha1 << std::endl;
+
+ auto hash_sha256 = QCryptographicHash(QCryptographicHash::Sha256);
+ // sha1
+ hash_sha256.addData(buffer);
+ auto sha256 = hash_sha256.result().toHex().toStdString();
+ LOG(INFO) << "sha256" << sha256;
+ ss << " "
+ << "sha256" << _(": ") << sha256 << std::endl;
+
+ ss << std::endl;
+ }
+ } else {
+ ss << "[#] " << _("Error in Calculating File Hash ") << std::endl;
+ }
+
+ return ss.str();
+}
diff --git a/src/core/function/FileOperator.h b/src/core/function/FileOperator.h
index de195a96..aa2c3b73 100644
--- a/src/core/function/FileOperator.h
+++ b/src/core/function/FileOperator.h
@@ -38,7 +38,7 @@ namespace GpgFrontend {
*
*/
class FileOperator {
-public:
+ public:
/**
* @brief read file content using std struct
*
@@ -79,7 +79,14 @@ public:
* @return false if failed
*/
static bool WriteFile(const QString &file_name, const QByteArray &data);
+
+ /**
+ * calculate the hash of a file
+ * @param file_path
+ * @return
+ */
+ static std::string CalculateHash(const std::filesystem::path &file_path);
};
-} // namespace GpgFrontend
+} // namespace GpgFrontend
-#endif // GPGFRONTEND_FILEOPERATOR_H
+#endif // GPGFRONTEND_FILEOPERATOR_H
diff --git a/src/ui/widgets/FilePage.cpp b/src/ui/widgets/FilePage.cpp
index 7615c05f..bbe5e548 100644
--- a/src/ui/widgets/FilePage.cpp
+++ b/src/ui/widgets/FilePage.cpp
@@ -347,52 +347,9 @@ void FilePage::slot_delete_item() {
}
void FilePage::slot_calculate_hash() {
- // Returns empty QByteArray() on failure.
- QFileInfo info(QString::fromStdString(selected_path_.string()));
-
- if (info.isFile() && info.isReadable()) {
- std::stringstream ss;
-
- ss << "[#] " << _("File Hash Information") << std::endl;
- ss << " " << _("filename") << _(": ")
- << selected_path_.filename().string().c_str() << std::endl;
-
- QFile f(info.filePath());
- f.open(QFile::ReadOnly);
- auto buffer = f.readAll();
- LOG(INFO) << "buffer size" << buffer.size();
- f.close();
- if (f.open(QFile::ReadOnly)) {
- auto hash_md5 = QCryptographicHash(QCryptographicHash::Md5);
- // md5
- hash_md5.addData(buffer);
- auto md5 = hash_md5.result().toHex().toStdString();
- LOG(INFO) << "md5" << md5;
- ss << " "
- << "md5" << _(": ") << md5 << std::endl;
-
- auto hash_sha1 = QCryptographicHash(QCryptographicHash::Sha1);
- // sha1
- hash_sha1.addData(buffer);
- auto sha1 = hash_sha1.result().toHex().toStdString();
- LOG(INFO) << "sha1" << sha1;
- ss << " "
- << "sha1" << _(": ") << sha1 << std::endl;
-
- auto hash_sha256 = QCryptographicHash(QCryptographicHash::Sha256);
- // sha1
- hash_sha256.addData(buffer);
- auto sha256 = hash_sha256.result().toHex().toStdString();
- LOG(INFO) << "sha256" << sha256;
- ss << " "
- << "sha256" << _(": ") << sha256 << std::endl;
-
- ss << std::endl;
-
- emit SignalRefreshInfoBoard(ss.str().c_str(),
- InfoBoardStatus::INFO_ERROR_OK);
- }
- }
+ auto info_str = FileOperator::CalculateHash(selected_path_);
+ emit SignalRefreshInfoBoard(info_str.c_str(),
+ InfoBoardStatus::INFO_ERROR_OK);
}
void FilePage::slot_mkdir() {