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

@@ -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;