bug/loadimage-dump

This commit is contained in:
2025-03-26 18:37:05 +08:00
parent c69a6899cb
commit 8b8b70c671
28 changed files with 816 additions and 363 deletions

View File

@@ -24,8 +24,7 @@ FileConfigDecode *FileConfigDecode::getInstance()
FileConfigDecode::~FileConfigDecode()
{
// updateFile();
// qDebug() << "write:" << autoSetup << rename << timeRename << autoCopy;
updateFile();
}
QString FileConfigDecode::getAddress()
@@ -73,23 +72,27 @@ RenameType FileConfigDecode::getRenameType()
return renameType;
}
bool FileConfigDecode::getAutoLogin()
{
return autoLogin;
}
void FileConfigDecode::setAddress(QString value)
{
this->address = value;
// updateFile();
updateFile();
}
void FileConfigDecode::setPort(quint16 value)
{
this->port = value;
// updateFile();
updateFile();
}
void FileConfigDecode::setAutoSetup(bool value)
{
this->autoSetup = value;
updateFile();
// updateFile();
updateFile();
}
void FileConfigDecode::setRename(bool value)
@@ -101,13 +104,13 @@ void FileConfigDecode::setRename(bool value)
else {
renameType = (renameType == RENAME_TYPE_TIME) ? RENAME_TYPE_TIME : RENAME_TYPE_NORMAL;
}
qDebug() << renameType;
updateFile();
// updateFile();
// qDebug() << renameType;
updateFile();
}
void FileConfigDecode::setTimeRename(bool value, int flag)
{
qDebug() << value;
this->timeRename = value;
if (value) {
renameType = RENAME_TYPE_TIME;
@@ -120,16 +123,14 @@ void FileConfigDecode::setTimeRename(bool value, int flag)
if (!value && flag == 0) {
renameType = RENAME_TYPE_NONE;
}
qDebug() << renameType;
updateFile();
// updateFile();
// qDebug() << renameType;
updateFile();
}
void FileConfigDecode::setAutoCopy(bool value)
{
this->autoCopy = value;
updateFile();
// updateFile();
updateFile();
}
void FileConfigDecode::setUserName(QString username)
@@ -144,8 +145,14 @@ void FileConfigDecode::setPassword(QString password)
updateFile();
}
void FileConfigDecode::fileWrite(QString path, QString address, bool autosetup,
bool rename, bool timerename, bool autocopy, QString username, QString password)
void FileConfigDecode::setAutoLogin(bool value)
{
autoLogin = value;
updateFile();
}
void FileConfigDecode::fileWrite(QString path)
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
QSettings* config = new QSettings(path, QSettings::IniFormat);
@@ -153,7 +160,6 @@ void FileConfigDecode::fileWrite(QString path, QString address, bool autosetup,
QString section = QString("config");
config->beginGroup(section);
config->setValue("address", address);
config->setValue("port", port);
config->setValue("autosetup", autosetup);
config->setValue("rename", rename);
config->setValue("timerename", timerename);
@@ -166,14 +172,13 @@ void FileConfigDecode::fileWrite(QString path, QString address, bool autosetup,
QString section = QString("config");
config->beginGroup(section);
config->setValue("address", address);
config->setValue("port", port);
config->setValue("autosetup", autosetup);
config->setValue("autosetup", autoSetup);
config->setValue("rename", rename);
config->setValue("timerename", timerename);
config->setValue("autocopy", autocopy);
config->setValue("username", username);
config->setValue("timerename", timeRename);
config->setValue("autocopy", autoCopy);
config->setValue("username", userName);
config->setValue("password", password);
config->endGroup();
config->setValue("autologin", autoLogin);
delete config;
#endif
}
@@ -196,12 +201,14 @@ void FileConfigDecode::initFile()
this->autoCopy = config->value(section + "autocopy").toBool();
this->userName = config->value(section+"username").toString();
this->password = config->value(section+"password").toString();
this->autoLogin = config->value(section+"autologin").toBool();
checkConfig();
this->firstCreate = false;
}
else {
qDebug() << "file not exists";
this->firstCreate = true;
this->address = "127.0.0.1:8080";
@@ -211,8 +218,9 @@ void FileConfigDecode::initFile()
this->autoCopy = false;
this->userName = "admin";
this->password = "admin";
this->autoLogin = false;
fileWrite(addFilePath, address, autoSetup, rename, timeRename, autoCopy, userName, password);
fileWrite(addFilePath);
}
}
@@ -235,7 +243,7 @@ void FileConfigDecode::updateFile()
QString addDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
QString addFilePath = addDataPath + "/config.ini";
qDebug() << "write" << autoSetup << rename << timeRename << autoCopy;
fileWrite(addFilePath, address, autoSetup, rename, timeRename, autoCopy, userName, password);
// qDebug() << "write" << this->autoSetup << this->rename << this->timeRename << this->autoCopy;
fileWrite(addFilePath);
}

View File

@@ -32,6 +32,7 @@ public:
QString getUserName();
QString getPassword();
RenameType getRenameType();
bool getAutoLogin();
void setAddress(QString value);
void setPort(quint16 value);
@@ -41,6 +42,7 @@ public:
void setAutoCopy(bool value);
void setUserName(QString username);
void setPassword(QString password);
void setAutoLogin(bool value);
void initFile();
void updateFile();
@@ -54,12 +56,13 @@ private:
bool autoCopy;
QString userName;
QString password;
bool autoLogin;
bool firstCreate;
RenameType renameType = RENAME_TYPE_NONE;
void fileWrite(QString path, QString address, bool autosetup, bool rename, bool timerename, bool autocopy, QString username, QString password);
void fileWrite(QString path);
static QScopedPointer<FileConfigDecode> m_instance;

View File

@@ -50,6 +50,10 @@ void TCHttpService::apiLogin()
qDebug() << this->m_token;
m_isOnline = true;
ImageManager::instance()->addDomainUser(m_domain, m_userName);
QString stdPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
ImageManager::instance()->initialize(stdPath + "/picpanel.db");
emit signal_loginSuc();
}
@@ -57,119 +61,6 @@ void TCHttpService::apiLogin()
}
#if 0
void TCHttpService::apiMyfileCount()
{
QString urlStr;
if (m_enableSsl)
urlStr = "https://" + m_domain + "/api/myfiles";
else
urlStr = "http://" + m_domain + "/api/myfiles";
QMap<QString, QString> headers;
headers.insert("Content-Type", "application/json");
QMap<QString, QString> params;
params.insert("cmd", "count");
QUrl url = encodeUrl(urlStr, params);
QJsonObject jsonObj;
jsonObj["token"] = m_token;
jsonObj["user"] = m_userName;
QJsonDocument jsonDoc(jsonObj);
QByteArray jsonData = jsonDoc.toJson();
QNetworkRequest request(url);
for (auto ite = headers.constBegin(); ite != headers.constEnd(); ite++){
request.setRawHeader(ite.key().toUtf8(), ite.value().toUtf8());
}
QNetworkReply* reply = nullptr;
reply = m_manager.post(request, jsonData);
QObject::connect(reply, &QNetworkReply::finished, [this, reply](){
emit requestFinished(reply, "myfilecount");
});
}
void TCHttpService::apiMyfileNormal(int start, int count)
{
QString urlStr;
if (m_enableSsl)
urlStr = "https://" + m_domain + "/api/myfiles";
else
urlStr = "http://" + m_domain + "/api/myfiles";
QMap<QString, QString> headers;
headers.insert("Content-Type", "application/json");
QMap<QString, QString> params;
params.insert("cmd", "normal");
QUrl url = encodeUrl(urlStr, params);
QNetworkRequest request(url);
for (auto ite = headers.constBegin(); ite != headers.constEnd(); ite++){
request.setRawHeader(ite.key().toUtf8(), ite.value().toUtf8());
}
QJsonObject jsonObj;
jsonObj["token"] = m_token;
jsonObj["user"] = m_userName;
jsonObj["count"] = count;
jsonObj["start"] = start;
QJsonDocument jsonDoc(jsonObj);
QByteArray jsonData = jsonDoc.toJson();
QNetworkReply* reply = nullptr;
reply = m_manager.post(request, jsonData);
connect(reply, &QNetworkReply::finished, [this, reply](){
emit requestFinished(reply, "myfilenormal");
});
}
void TCHttpService::apiMd5(const QString& filePath)
{
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
return;
}
QByteArray fileMd5;
QCryptographicHash hash(QCryptographicHash::Md5);
if (hash.addData(&file)) {
fileMd5 = hash.result().toHex();
}
else
return;
QString urlStr;
if (m_enableSsl)
urlStr = "https://" + m_domain + "/api/md5";
else
urlStr = "http://" + m_domain + "/api/md5";
QMap<QString, QString> headers;
headers.insert("Content-Type", "application/json");
QString md5Str = QString::fromUtf8(fileMd5);
QJsonObject jsonObj;
jsonObj["token"] = m_token;
jsonObj["md5"] = md5Str;
jsonObj["filename"] = QFileInfo(file).fileName();
jsonObj["user"] = m_userName;
file.close();
QJsonDocument jsonDoc(jsonObj);
QByteArray jsonData = jsonDoc.toJson();
QNetworkReply* reply = nullptr;
QNetworkRequest request(urlStr);
reply = m_manager.post(request, jsonData);
QObject::connect(reply, &QNetworkReply::finished, [this, reply](){
emit requestFinished(reply, "md5");
});
}
#endif
void TCHttpService::apiUpload(const QString &filePath)
{
@@ -246,6 +137,7 @@ void TCHttpService::apiUpload(const QString &filePath)
QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll());
QJsonObject jsonObj = jsonDoc.object();
QString fileUrl = jsonObj["url"].toString();
ImageManager::instance()->addImageUrl(m_domain, m_userName, fileUrl);
emit signal_uploadFileSec(fileUrl);
} else {
qDebug() << "上传失败:" << reply->errorString();
@@ -255,9 +147,10 @@ void TCHttpService::apiUpload(const QString &filePath)
multiPart->deleteLater();
});
connect(reply, &QNetworkReply::uploadProgress, this, [](qint64 bytesSent, qint64 bytesTotal) {
connect(reply, &QNetworkReply::uploadProgress, this, [this](qint64 bytesSent, qint64 bytesTotal) {
if (bytesTotal > 0) {
int progress = (bytesSent * 100) / bytesTotal;
emit signal_progressUpdate(progress);
qDebug() << "上传进度:" << progress << "%";
}
});
@@ -290,118 +183,6 @@ void TCHttpService::setConfiguration(QString userName, QString firstPwd, QString
this->m_domain = domain;
this->m_userName = userName;
this->m_firstPwd = firstPwd;
#if 0
connect(this, &TCHttpService::requestFinished, [this](QNetworkReply* reply, QString api){
QByteArray rawData = reply->readAll();
if (api == "login") {
QJsonDocument jsonDoc = QJsonDocument::fromJson(rawData);
if (jsonDoc.isEmpty())
return;
QJsonObject jsonObj = jsonDoc.object();
int code = jsonObj["code"].toInt();
if (code == 0) {
this->m_token = jsonObj["token"].toString();
qDebug() << this->m_token;
m_isOnline = true;
emit signal_loginSuc();
// apiMyfileCount();
}
}
#if 0
if (api == "myfilecount") {
QJsonDocument jsonDoc = QJsonDocument::fromJson(rawData);
if (jsonDoc.isEmpty())
return;
QJsonObject jsonObj = jsonDoc.object();
int code = jsonObj["code"].toInt();
if (code == 0) {
int total = jsonObj["total"].toInt();
this->m_total = total;
int n = total / 10 + 1;
int pendingRequests = n;
for (int i = 0; i < n; i++) {
int count = (total - i * 10 >= 10) ? 10 : (total - i * 10);
apiMyfileNormal(i*10, count);
}
}
}
if (api == "myfilenormal") {
static int loginCnt = 0;
QJsonDocument jsonDoc = QJsonDocument::fromJson(rawData);
if (jsonDoc.isEmpty())
return;
QJsonObject jsonObj = jsonDoc.object();
int code = jsonObj["code"].toInt();
if (code != 0)
return;
QJsonArray jsonArray = jsonObj["files"].toArray();
for (const QJsonValue& value : jsonArray) {
QJsonObject jsonObj = value.toObject();
QString file_name = jsonObj["file_name"].toString();
int share_status = jsonObj["share_status"].toInt();
QString url = jsonObj["url"].toString();
qint64 size = jsonObj["size"].toInt();
QString md5 = jsonObj["md5"].toString();
QMap<QString, file_info_t>::iterator ite;
ite = m_fileMap.find(md5);
if (ite == m_fileMap.end()) {
qDebug() << file_name;
file_info_t info;
info.share_status = share_status;
info.size = size;
info.url = url;
info.file_name = file_name;
if (m_isOnline) {
qDebug() << "now upload";
emit signal_uploadFileSec(url);
}
m_fileMap.insert(md5, info);
// if (m_fileMap.size() == m_total && m_isOnline == false)
}
#if !DEBUG
// for(auto ite = m_fileMap.constBegin(); ite != m_fileMap.constEnd(); ite++) {
// qDebug() << "fileMap debug:" << ite.value().file_name;
// }
qDebug() << "fileMap size:" << m_fileMap.size();
#endif
}
// if (!m_isOnline) {
// loginCnt++;
// qDebug() << "loginCnt:" << loginCnt;
// if (loginCnt == m_total/10+1) {
// m_isOnline = true;
// qDebug() << "login success";
// }
// }
}
// if (api == "upload") {
// qDebug() <<"upload";
// QJsonDocument jsondoc = QJsonDocument::fromJson(rawData);
// QJsonObject jsonObj = jsondoc.object();
// int code = jsonObj["code"].toInt();
// if (code == 0)
// this->apiMyfileCount();
// }
#endif
reply->deleteLater();
});
#endif
apiLogin();
}
@@ -422,6 +203,19 @@ void TCHttpService::setUploadNum(int nb)
void TCHttpService::downloadImage(const QString &requestId, const QUrl &imageUrl)
{
#if 0
QPixmap cachedPixmap;
if (QPixmapCache::find(imageUrl.toString(), &cachedPixmap)) {
emit signal_imageDownloaded(requestId, cachedPixmap);
return;
}
if (getCachedImage(imageUrl, cachedPixmap)) {
QPixmapCache::insert(imageUrl.toString(), cachedPixmap);
emit signal_imageDownloaded(requestId, cachedPixmap);
return;
}
#endif
QNetworkRequest request(imageUrl);
QNetworkReply* reply = m_manager.get(request);
@@ -460,6 +254,66 @@ QUrl TCHttpService::encodeUrl(QString urlStr, QMap<QString, QString> params)
return url;
}
void TCHttpService::cleanOldCacheFiles()
{
QDir cacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
cacheDir.setNameFilters(QStringList() << "*.png" << "*.jpg" << "*.jpeg" << "*.svg");
cacheDir.setFilter(QDir::Files);
foreach (QString dirFile, cacheDir.entryList()) {
QFileInfo fileInfo(cacheDir.absoluteFilePath(dirFile));
if (fileInfo.lastModified().daysTo(QDateTime::currentDateTime()) > m_max_cache_days) {
cacheDir.remove(dirFile);
}
}
qint64 totalSize = 0;
QFileInfoList files = cacheDir.entryInfoList(QDir::Files, QDir::Time | QDir::Reversed);
foreach(QFileInfo file, files) {
totalSize += file.size();
}
if (totalSize > m_disk_cache_size_mb * 1024 * 1024) {
foreach (QFileInfo file, files) {
if (totalSize <= m_disk_cache_size_mb * 1024 * 1024 * 0.9)
break;
totalSize -= file.size();
QFile::remove(file.absoluteFilePath());
}
}
}
bool TCHttpService::getCachedImage(const QUrl &imageUrl, QPixmap &pixmap)
{
QString filePath = getCacheFilePath(imageUrl);
if (QFile::exists(filePath)) {
QFileInfo info(filePath);
if (info.lastModified().daysTo(QDateTime::currentDateTime()) > m_max_cache_days) {
QFile::remove(filePath);
return false;
}
if (pixmap.load(filePath)) {
return true;
}
}
return false;
}
QString TCHttpService::getCacheFilePath(const QUrl &imageUrl)
{
QCryptographicHash hash(QCryptographicHash::Md5);
hash.addData(imageUrl.toString().toUtf8());
QString fileName = hash.result().toHex() + ".png";
return QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" + fileName;
}
TCHttpService::TCHttpService(QObject *parent) : QObject(parent)
{
@@ -482,5 +336,6 @@ void TCHttpService::slot_downloadFinished(QNetworkReply *reply)
}
}
reply->deleteLater();
}

View File

@@ -19,8 +19,11 @@
#include <QHttpPart>
#include <QHttpMultiPart>
#include <QHash>
#include <QPixmapCache>
#include <QDir>
#include "urldatabase.h"
class TCHttpService : public QObject
{
Q_OBJECT
@@ -29,6 +32,8 @@ private:
public:
static TCHttpService* getInstance();
void apiLogin();
#if 0
void apiMyfileCount();
@@ -47,6 +52,9 @@ public:
private:
QUrl encodeUrl(QString url, QMap<QString, QString> params);
void cleanOldCacheFiles();
bool getCachedImage(const QUrl& imageUrl, QPixmap& pixmap);
QString getCacheFilePath(const QUrl& imageUrl);
signals:
void signal_loginSuc();
@@ -56,6 +64,8 @@ signals:
void signal_imageDownloaded(const QString& requestId, const QPixmap& pixmap);
void signal_downloadFailed(const QString& requestId, const QString& error);
void signal_progressUpdate(int);
private:
explicit TCHttpService(QObject *parent = nullptr);
@@ -67,6 +77,11 @@ private:
QString m_domain;
QString m_firstPwd;
QString m_userName;
int m_memory_cache_size_kb = 10240;
int m_disk_cache_size_mb = 100;
int m_max_cache_days = 30;
int m_total;
bool m_enableSsl = true;