own class for git-repo

This commit is contained in:
Gerhard Hoffmann 2024-03-21 16:57:02 +01:00
parent 90853294da
commit 65f72eca79
2 changed files with 143 additions and 0 deletions

View File

@ -0,0 +1,113 @@
#include "local_git_repository.h"
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QJsonDocument>
#include <QByteArray>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QFile>
LocalGitRepository::LocalGitRepository(QString const &localRepository)
: m_localRepository(localRepository)
, m_fileMenu("{}")
, m_fileMenuSize(-1) {
}
int32_t LocalGitRepository::GetFileMenuSize() const {
if (m_fileMenuSize == -1) {
GetFileMenu();
}
return m_fileMenuSize;
}
QByteArray const &LocalGitRepository::GetFileMenu() const {
if (m_fileMenu.toStdString() == "{}") {
QFile f(QDir::cleanPath(QString(baseDir) + m_localRepository + QDir::separator()
+ "etc/psa_webinterface/menu_config.json"));
if (f.exists()) {
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
m_fileMenu = QTextStream(&f).readAll().toUtf8();
if (m_fileMenu.isValidUtf8()) {
QString const &s = QString::fromUtf8(m_fileMenu);
m_fileMenuSize = s.toLocal8Bit().size();
}
}
}
}
return m_fileMenu;
}
QByteArray LocalGitRepository::GetFileName(QString const &fId) {
QRegularExpressionMatch match;
static const QRegularExpression re("(master|[0-9]+)");
if (fId.lastIndexOf(re, -1, &match) != -1) {
int idx = fId.indexOf("/");
if (idx != -1) {
QString path = fId.mid(idx);
idx = path.indexOf(":");
if (idx != -1) {
path = path.mid(0, idx);
QString s = match.captured(match.lastCapturedIndex());
if (s != "master") {
if (fId.contains("psa_tariff")) {
QString fn(QDir::cleanPath(
QString(baseDir) + QDir::separator() +
QString(m_localRepository) + QDir::separator()
+ path + QDir::separator()
+ QString("tariff%1.json").arg(s.toUInt(), 2, 10, QChar('0'))));
return fn.toUtf8();
}
}
}
}
}
return QByteArray();
}
QByteArray LocalGitRepository::GetFile(QString const &fId) {
QByteArray a = GetFileName(fId);
if (a.isValidUtf8()) {
QFile fn(a);
if (fn.exists()) {
if (fn.open(QIODevice::ReadOnly)) {
return fn.readAll();
}
}
}
return QByteArray("{}");
}
int32_t LocalGitRepository::GetFileSize(QString const &fId) {
QByteArray a = GetFileName(fId);
if (a.isValidUtf8()) {
QFile fn(a);
if (fn.exists()) {
return fn.size();
}
}
return -1;
}
bool LocalGitRepository::SetFile(QString const &fId, QByteArray const &json) {
QByteArray a = GetFileName(fId);
if (a.isValidUtf8()) {
QFile fn(a);
if (fn.exists()) {
if (fn.open(QIODevice::WriteOnly)) {
quint64 bytesWritten = 0;
qint64 bytesToWrite = a.size();
while (bytesToWrite > 0 &&
(bytesWritten = fn.write(json.constData(), bytesToWrite)) != -1) {
bytesToWrite -= bytesWritten;
}
return (bytesToWrite == 0);
}
}
}
return false;
}

View File

@ -0,0 +1,30 @@
#ifndef LOCAL_GIT_REPOSITORY_H_INCLUDED
#define LOCAL_GIT_REPOSITORY_H_INCLUDED
#include <QByteArray>
#include <QString>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
class LocalGitRepository {
QString m_localRepository;
mutable QByteArray m_fileMenu;
mutable int32_t m_fileMenuSize;
public:
LocalGitRepository(QString const &localRepository);
static char const constexpr *baseDir = "H:\\";
QString gitRepository() const { return m_localRepository; }
QByteArray const &GetFileMenu() const;
int32_t GetFileMenuSize() const;
QByteArray GetFileName(QString const &fileId);
int32_t GetFileSize(QString const &fileId);
QByteArray GetFile(QString const &fileId);
bool SetFile(QString const &fileId, QByteArray const &json);
};
#endif // LOCAL_GIT_REPOSITORY_H_INCLUDED