136 lines
4.0 KiB
C++
136 lines
4.0 KiB
C++
|
#include "FileHelper.h"
|
|||
|
|
|||
|
#include <QDir>
|
|||
|
#include <QApplication>
|
|||
|
#include <QDebug>
|
|||
|
#include <QJsonDocument>
|
|||
|
#include <QJsonObject>
|
|||
|
#include <QJsonArray>
|
|||
|
#include <QFile>
|
|||
|
|
|||
|
const QString kConfigName = "/config.json";
|
|||
|
const QString kResouce = "/resouce";
|
|||
|
|
|||
|
constexpr qint16 kDefualtPort = 9527;
|
|||
|
|
|||
|
QStringList FileHelper::GetAllMediaFiles(const QString& path, bool* success) {
|
|||
|
QDir dir(path);
|
|||
|
if (!dir.exists()) {
|
|||
|
if (success) { *success = false; }
|
|||
|
return QStringList();
|
|||
|
}
|
|||
|
|
|||
|
QStringList nameFilters;
|
|||
|
nameFilters << "*.png" << "*.jpg" << "*.jpeg" << "*.gif" << "*.bmp"
|
|||
|
<< "*.mp4" << "*.avi" << "*.mkv" << "*.mov" << "*.wmv";
|
|||
|
|
|||
|
QStringList files = dir.entryList(nameFilters, QDir::Files);
|
|||
|
if (success) { *success = true; }
|
|||
|
return files;
|
|||
|
}
|
|||
|
|
|||
|
bool FileHelper::GeneratJsonConfig() {
|
|||
|
QString resourcePath = GetResoucePath();
|
|||
|
bool success = false;
|
|||
|
QStringList mediaFiles = GetAllMediaFiles(resourcePath, &success);
|
|||
|
if (!success) {
|
|||
|
qDebug() << "GetAllMediaFiles failes";
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
QJsonArray jsonArray;
|
|||
|
qint32 index = 0;
|
|||
|
for (const auto& path : mediaFiles) {
|
|||
|
QJsonObject jsonObject;
|
|||
|
jsonObject["index"] = index++;
|
|||
|
jsonObject["path"] = path;
|
|||
|
jsonArray.append(jsonObject);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
QJsonObject jsonObject;
|
|||
|
jsonObject["data"] = jsonArray;
|
|||
|
jsonObject["port"] = kDefualtPort;
|
|||
|
|
|||
|
// 创建 JSON 文档
|
|||
|
|
|||
|
const QString configPath = QApplication::applicationDirPath() + kConfigName;
|
|||
|
QFile outputFile(configPath);
|
|||
|
if (outputFile.open(QIODevice::WriteOnly)) {
|
|||
|
QJsonDocument jsonDocument(jsonObject);
|
|||
|
outputFile.write(jsonDocument.toJson());
|
|||
|
outputFile.close();
|
|||
|
qDebug() << "JSON file saved at:" << configPath;
|
|||
|
return true;
|
|||
|
}
|
|||
|
else {
|
|||
|
qDebug() << "Failed to open file for writing. path=" << configPath;
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
bool FileHelper::GetItemFromJsonConfig(qint16& port, QMap<qint32, QString>* items) {
|
|||
|
if (nullptr == items) {
|
|||
|
qDebug() << "items is nullptr";
|
|||
|
return false;
|
|||
|
}
|
|||
|
const QString configPath = QApplication::applicationDirPath() + kConfigName;
|
|||
|
QFile inputFile(configPath);
|
|||
|
if (!inputFile.open(QIODevice::ReadOnly))
|
|||
|
{
|
|||
|
qDebug() << "Failed to open file for reading. config=" << configPath;
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// 读取文件内容并关闭文件
|
|||
|
QByteArray fileData = inputFile.readAll();
|
|||
|
inputFile.close();
|
|||
|
|
|||
|
// 解析 JSON 数据
|
|||
|
QJsonParseError jsonError;
|
|||
|
QJsonDocument jsonDocument = QJsonDocument::fromJson(fileData, &jsonError);
|
|||
|
if (jsonError.error != QJsonParseError::NoError)
|
|||
|
{
|
|||
|
qDebug() << "Failed to parse JSON data:" << jsonError.errorString() << " config=" << configPath;
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// 将 JSON 数据转换为 JSON 对象
|
|||
|
QJsonObject jsonObject = jsonDocument.object();
|
|||
|
if (jsonObject.contains("port") && jsonObject["port"].isDouble()) {
|
|||
|
port = jsonObject["port"].toInt();
|
|||
|
}
|
|||
|
else {
|
|||
|
port = kDefualtPort;
|
|||
|
}
|
|||
|
|
|||
|
if (!jsonObject.contains("data") || !jsonObject["data"].isArray()) {
|
|||
|
qDebug() << "Failed to parse JSON not find key of data, or data value is not array config=" << configPath;
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
QJsonArray jsonArray = jsonObject["data"].toArray();
|
|||
|
for (qint32 i = 0; i < jsonArray.size(); ++i) {
|
|||
|
if (!jsonArray.at(i).isObject()) {
|
|||
|
qDebug() << "jsonArray is not object config=" << configPath;
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
QJsonObject item = jsonArray.at(i).toObject();
|
|||
|
if (!item.contains("index") || !item["index"].isDouble() ||
|
|||
|
!item.contains("path") || !item["path"].isString()) {
|
|||
|
qDebug() << "not find index / path in item config config=" << configPath;
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
items->insert(item["index"].toInt(), item["path"].toString());
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
QString FileHelper::GetResoucePath() {
|
|||
|
const QString configPath = QApplication::applicationDirPath() + kResouce;
|
|||
|
return configPath;
|
|||
|
}
|