111 lines
3.3 KiB
C++
111 lines
3.3 KiB
C++
#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 {
|
|
return m_fileMenuSize;
|
|
}
|
|
|
|
QByteArray const &LocalGitRepository::GetFileMenu() const {
|
|
if (m_fileMenuSize == -1) {
|
|
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)) {
|
|
qint64 bytesWritten = 0;
|
|
qint64 bytesToWrite = a.size();
|
|
while (bytesToWrite > 0 &&
|
|
(bytesWritten = fn.write(json.constData(), bytesToWrite)) != -1) {
|
|
bytesToWrite -= bytesWritten;
|
|
}
|
|
return (bytesToWrite == 0);
|
|
}
|
|
}
|
|
|
|
}
|
|
return false;
|
|
}
|